<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.checkWarehouseNo || '--' }}</a-form-model-item>
        <a-form-model-item label="打印范围">{{ itemData&&itemData.printContent || '--' }}</a-form-model-item>
        <a-form-model-item label="货位编号" prop="orderBy">
          <a-radio-group v-model="form.orderBy">
            <a-radio value="ASC">打印（↑升序）</a-radio>
            <a-radio value="DESC">打印（↓降序）</a-radio>
            <a-radio value="-1">不打印</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" class="button-info" id="sales-print-orginPreview" @click="handleSave('orginPreview')" style="margin-left: 15px;">打印预览</a-button>
        <a-button type="primary" id="sales-print-save" @click="handleSave('orginPrint')" style="margin-left: 10px;">立即打印</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: {
        orderBy: undefined
      },
      rules: {
        orderBy: [{ required: true, message: '请选择货位编号', trigger: 'change' }]
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      spinning: false,
      typeList: []
    }
  },
  computed: {
    modalTit () {
      return this.nowType == 'CHECK_WAREHOUSE' ? '盘点打印' : '监盘打印'
    }
  },
  methods: {
    // 确认
    handleSave (type) {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const obj = {
            printType: _this.nowType
          }
          // 打印货位编号
          if (_this.form.orderBy != '-1') {
            obj.orderBy = _this.form.orderBy
            obj.printType = obj.printType + '_STACK_PLACE'
          }
          _this.$emit('ok', obj, type)
          _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">
  .sales-print-type-modal{
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
