<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="export-excel-modal"
    title="导出Excel"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="export-excel-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="销售单号">{{ itemData&&itemData.salesBillNo || '--' }}</a-form-model-item>
        <a-form-model-item label="客户名称">{{ itemData&&itemData.buyerName || '--' }}</a-form-model-item>
        <a-form-model-item label="产品售价" prop="priceType" v-if="$hasPermissions('B_dispatchExport_salesPrice')">
          <a-radio-group v-model="form.priceType">
            <a-radio value="DISPATCH_BILL_PRICE">导出</a-radio>
            <a-radio value="DISPATCH_BILL">不导出</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="原厂编码" prop="origCode">
          <a-radio-group v-model="form.origCode">
            <a-radio value="1">导出</a-radio>
            <a-radio value="0">不导出</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="export-excel-back" @click="handleCancel" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="export-excel-save" @click="handleSave()">导出</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemData: {
      type: Object,
      default: () => {
        return null
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      form: {
        priceType: undefined,
        origCode: '0'
      },
      rules: {
        priceType: [{ required: true, message: '请选择产品售价', trigger: 'change' }],
        origCode: [{ required: true, message: '请选择原厂编码', trigger: 'change' }]
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      spinning: false
    }
  },
  methods: {
    // 确认
    handleSave (isPrint) {
      const _this = this
      _this.$store.state.app.curActionPermission = 'B_dispatchExport'
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (!this.$hasPermissions('B_dispatchExport_salesPrice')) {
            _this.form.priceType = 'DISPATCH_BILL'
          }
          _this.form.priceType += _this.form.origCode == 1 ? '_ORIG_CODE' : ''
          _this.$emit('ok', _this.form.priceType, {}, '下推产品')
          _this.isShow = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 取消选择分类
    handleCancel () {
      this.isShow = false
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

<style lang="less">
  .export-excel-modal{
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
