<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="pushOrderManagement-print-type-modal"
    title="发货分类打印"
    v-model="isShow"
    @cancel="handleCancel"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="pushOrderManagement-print-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="销售单号">
          {{ detailData&&detailData.salesBillNo || '--' }}
        </a-form-model-item>
        <a-form-model-item label="备货单号">
          {{ detailData&&detailData.dispatchBillNo || '--' }}
        </a-form-model-item>
        <a-form-model-item label="发货编号">
          {{ detailData&&detailData.sendNo || '--' }}
        </a-form-model-item>
        <a-form-model-item label="客户名称">
          {{ detailData&&detailData.buyerName || '--' }}
        </a-form-model-item>
        <a-form-model-item label="收货客户名称">
          {{ detailData&&detailData.receiverName || '--' }}
        </a-form-model-item>
        <a-form-model-item label="产品分类" prop="id">
          <a-select id="pushOrderManagement-print-form" v-model="form.id" placeholder="请选择产品分类">
            <a-select-option v-for="item in typeList" :value="item.id" :key="item.id">
              <span v-if="item.id != 'all'">{{ item.productBrandName }} {{ item.productTypeName?('-'+item.productTypeName):'' }}</span>
              <span v-else>全部</span>
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="促销产品" prop="giftPrintType" v-if="nowType=='print'">
          <a-radio-group v-model="form.giftPrintType">
            <a-radio value="2">包含</a-radio>
            <a-radio value="0">不包含</a-radio>
            <a-radio value="1">仅有</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="货位编号" prop="orderBy" v-if="nowType=='print'">
          <a-radio-group v-model="form.orderBy">
            <a-radio value="dispDet.STACK_PLACE_CODE ASC">打印（↑升序）</a-radio>
            <a-radio value="dispDet.STACK_PLACE_CODE DESC">打印（↓降序）</a-radio>
            <a-radio value="-1">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="原厂编码" prop="origCode" v-if="nowType=='print'">
          <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-item label="装箱号" prop="packNo" v-if="nowType=='print'">
          <a-radio-group v-model="form.packNo">
            <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="pushOrderManagement-print-back" @click="handleCancel">取消</a-button>
        <a-button
          type="primary"
          class="button-info"
          id="pushOrderManagement-print-save"
          v-if="nowType=='print'&&$hasPermissions('B_sendTypePrint')"
          @click="handleSave('preview','B_sendTypePrint')"
          style="margin-left: 15px;">打印预览</a-button>
        <a-button
          type="primary"
          id="pushOrderManagement-print-save"
          v-if="!hasNoPrint&&nowType=='print'&&$hasPermissions('B_sendTypePrint')"
          @click="handleSave('print','B_sendTypePrint')"
          style="margin-left: 10px;">确定打印</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { dispatchDetailType } from '@/api/dispatch'
export default {
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    outBizSubSn: {
      type: [Number, String],
      default: ''
    },
    hasNoPrint: {
      type: Boolean,
      default: false
    },
    nowType: {
      type: String,
      default: 'print'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      form: {
        id: 'all',
        orderBy: undefined,
        giftPrintType: '2',
        origCode: '0',
        packNo: '1'
      },
      rules: {
        id: [{ required: true, message: '请选择产品分类', trigger: 'change' }],
        orderBy: [{ required: true, message: '请选择货位编号', trigger: 'change' }],
        origCode: [{ required: true, message: '请选择原厂编码', trigger: 'change' }],
        packNo: [{ required: true, message: '请选择装箱号', trigger: 'change' }],
        giftPrintType: [{ required: true, message: '请选择促销产品', trigger: 'change' }]
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      spinning: false,
      typeList: [],
      detailData: null
    }
  },
  methods: {
    // 确认
    handleSave (type, authCode) {
      const _this = this
      this.$store.state.app.curActionPermission = authCode
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const item = this.typeList.find(item => item.id == this.form.id)
          if (item) {
            const obj = {
              productBrandSn: item.productBrandSn,
              productTypeSn: item.productTypeSn,
              dispatchBillSn: this.detailData.dispatchBillSn,
              dispatchBillNo: this.detailData.dispatchBillNo
            }
            // 打印货位编号
            if (_this.form.orderBy != '-1') {
              obj.orderBy = _this.form.orderBy
            }
            obj.origCode = _this.form.origCode
            obj.packNo = _this.form.packNo
            obj.giftPrintType = _this.form.giftPrintType
            _this.$emit('ok', obj, '发货分类', type)
          }
          _this.isShow = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 取消选择分类
    handleCancel () {
      this.isShow = false
      this.$refs.ruleForm.resetFields()
      this.form.id = 'all'
      this.form.orderBy = undefined
      this.form.packNo = '1'
      this.form.giftPrintType = '2'
    },
    // 获取该销售单产品二级分类
    getTypeData (dispatchBillSn) {
      this.typeList = [{ id: 'all' }]
      dispatchDetailType({ dispatchBillSn: dispatchBillSn }).then(res => {
        if (res.status == 200) {
          if (res.data && res.data.length > 0) {
            this.typeList = [...this.typeList, ...res.data]
          }
        }
      })
    },
    setData (data, dispatchBillSn) {
      this.detailData = data
      this.getTypeData(dispatchBillSn)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

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