<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="inventoryChecking-print-type-modal"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="inventoryChecking-print-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="盘点单号">{{ itemData&&itemData.checkWarehouseNo || '--' }}</a-form-model-item>
        <a-form-model-item label="盘点类型">
          {{ itemData&&itemData.checkTypeDictValue || '--' }}
          <span v-if="itemData&&itemData.checkType=='SELECT'">({{ itemData.warehouseFlag=='1' ? '区分仓库':'不区分仓库' }})</span>
        </a-form-model-item>
        <a-form-model-item label="仓库范围" prop="warehouseSn" v-if="itemData&&itemData.checkType=='SELECT'&&itemData.warehouseFlag=='1'">
          <a-select id="inventoryChecking-print-form" v-model="form.warehouseSn" placeholder="请选择仓库范围">
            <a-select-option v-for="(item,index) in warehouseList" :value="item.warehouseSn" :key="index">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="库存数量" prop="stockQtyFlag" v-if="nowType == 'pddy'">
          <a-radio-group v-model="form.stockQtyFlag">
            <a-radio :value="true">打印</a-radio>
            <a-radio :value="false">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="产品范围" prop="productScopeFlag" v-if="nowType == 'shdy'">
          <a-radio-group v-model="form.productScopeFlag">
            <a-radio :value="true">所有盘点产品</a-radio>
            <a-radio :value="false">有盈亏的产品</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="inventoryChecking-print-save" @click="handleSave()">打印预览</a-button>
        <a-button id="inventoryChecking-print-back" @click="handleCancel" style="margin-left: 15px;">快捷打印</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
export default {
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemData: {
      type: Object,
      default: () => {
        return null
      }
    },
    nowType: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      form: {
        stockQtyFlag: true,
        productScopeFlag: true,
        warehouseSn: 'all'
      },
      rules: {
        stockQtyFlag: [{ required: true, message: '请选择库存数量', trigger: 'change' }],
        warehouseSn: [{ required: true, message: '请选择仓库范围', trigger: 'change' }],
        productScopeFlag: [{ required: true, message: '请选择产品范围', trigger: 'change' }]
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      spinning: false,
      warehouseList: []
    }
  },
  computed: {
    modalTit () {
      let title = ''
      if (this.nowType == 'pddy') {
        title = '盘点打印'
      } else if (this.nowType == 'shdy') {
        title = '审核打印'
      }
      return title
    }
  },
  methods: {
    // 确认
    handleSave (isPrint) {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const obj = {
            checkWarehouseSn: _this.itemData.checkWarehouseSn,
            stockQtyFlag: _this.nowType == 'pddy' ? _this.form.stockQtyFlag : undefined,
            productScopeFlag: _this.nowType == 'shdy' ? _this.form.productScopeFlag : undefined,
            checkPrintFlag: _this.nowType == 'pddy',
            type: isPrint || 'preview'
          }
          if (_this.itemData && _this.itemData.checkType == 'SELECT' && _this.itemData.warehouseFlag == '1') { //  自选盘点且区分仓库
            obj.warehouseSn = _this.form.warehouseSn
          }
          _this.$emit('ok', obj)
          _this.isShow = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 快捷打印
    handleCancel () {
      this.handleSave('print')
    },
    // 仓库下拉数据
    getWarehouseList () {
      this.warehouseList = [{ warehouseSn: 'all', name: '所有仓库' }]
      if (this.itemData && this.itemData.warehouseList && this.itemData.warehouseList.length > 0) {
        this.warehouseList = [...this.warehouseList, ...this.itemData.warehouseList]
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        this.getWarehouseList()
      }
    }
  }
}
</script>

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