<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="printStatusModal-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 id="printStatusModal-printStateInfo" v-model="form.printStateInfo">
            <a-radio value="NO_PRINT" id="printStatusModal-printStateInfo1">允许打印</a-radio>
            <a-radio value="CANCEL_PRINT" v-if="form.printState == 'UNABLE_PRINT'" id="printStatusModal-printStateInfo0">取消打印</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="printStatusModal-cancel-btn">取消</a-button>
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="printStatusModal-submit-btn">确定</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, // 确定按钮加载状态
      // form表单label布局
      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>
