<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :footer="null"
    @cancel="cancel"
    width="40%">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="chooseCustom-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="调拨单号:" prop="no">
          <span>{{ form.no }}{{ form.printState == 'UNABLE_PRINT' ? '（暂不打印）' : form.printState == 'CANCEL_PRINT' ? '（取消打印）' : '' }}</span>
        </a-form-model-item>
        <a-form-model-item label="调往对象:" prop="targetName">
          <span>{{ form.targetName }}</span>
        </a-form-model-item>
        <a-form-model-item label="调拨打印:" prop="printStateInfo">
          <a-radio-group v-model="form.printStateInfo">
            <a-radio value="NO_PRINT">允许打印</a-radio>
            <a-radio value="CANCEL_PRINT" v-if="form.printState == 'UNABLE_PRINT'">取消打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;margin-top: 50px;">
          <a-button @click="cancel" style="margin-right: 15px" id="chooseCustom-btn-back">取消</a-button>
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="chooseCustom-btn-submit">确定</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { updatePrintState } from '@/api/allocateBill.js'
export default {
  name: 'PrintModal',
  props: {
    show: [Boolean],
    info: {
      type: Object,
      default: () => {
        return null
      }
    }
  },
  data () {
    return {
      opened: this.show,
      spinning: false,
      title: '调拨打印状态',

      confirmLoading: false,
      formItemLayout: {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 18
        }
      },
      form: {
        no: undefined,
        payerName: undefined,
        printStatus: '',
        allocateSn: '',
        printStateInfo: ''
      },
      rules: { printStateInfo: [{ required: true, message: '请选择调拨打印状态', trigger: 'change' }] }
    }
  },
  methods: {
    //  保存
    handleSubmit () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.handleSave()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSave () {
      const _this = this
      const ajax_form = {
        allocateSn: _this.form.allocateSn,
        printState: _this.form.printStateInfo
      }
      _this.spinning = true
      _this.confirmLoading = true
      updatePrintState(ajax_form).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.cancel()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
        _this.confirmLoading = false
      })
    },
    cancel () {
      this.opened = false
      this.$emit('cancel')
      this.form.printStateInfo = ''
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (!newValue) {
        this.cancel()
      } else {
        this.form = this.info
      }
    }
  }
}
</script>
<style lang="less" scoped>
.ant-form-item {
  margin-bottom: 0px !important;
}
</style>
