<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="sales-print-type-modal"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="sales-print-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="dataScope">
          <a-radio-group v-model="form.dataScope">
            <a-radio value="all">全部</a-radio>
            <a-radio value="ENOUGH">有库存</a-radio>
            <a-radio value="LESS">缺货</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="原厂编码" prop="orgCode">
          <a-radio-group v-model="form.orgCode">
            <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="sales-print-back" @click="handleCancel">取消</a-button>
        <a-button type="primary" style="margin-left: 15px;" id="sales-print-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
      }
    },
    nowType: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      form: {
        id: 'all',
        dataScope: 'all',
        priceType: undefined,
        orgCode: "1"
      },
      rules: {
        id: [{ required: true, message: '请选择产品分类', trigger: 'change' }],
        dataScope: [{ required: true, message: '请选择是否缺货产品', trigger: 'change' }],
        orgCode: [{ required: true, message: '请选择原厂编码', trigger: 'change' }]
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      spinning: false,
      typeList: []
    }
  },
  computed: {
    modalTit () {
      return '导出Excel'
    }
  },
  methods: {
    // 确认
    handleSave (isPrint) {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
           _this.form.priceType = _this.form.dataScope == 'ENOUGH' ? 'SALES_BILL_NOT_LACK':'SALES_BILL'
           _this.form.priceType += _this.form.orgCode == "1" ? '_ORIG_CODE':''
           const obj = {
             salesBillSn: _this.itemData.salesBillSn,
             priceType: _this.form.priceType,
             dataScope: _this.form.dataScope == 'all' ? '' : _this.form.dataScope,
             type: isPrint || 'preview'
           }
           _this.$emit('ok', obj)
           _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.form = {
          id: 'all',
          dataScope: 'all',
          priceType: undefined,
          orgCode: "1"
        }
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

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