<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="allocateBill-print-type-modal"
    title="调拨退货打印"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="allocateBill-print-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item style="margin:0" label="调拨退货单号">{{ itemData&&itemData.allocateReturnNo || '--' }}</a-form-model-item>
        <a-form-model-item style="margin:0" label="调拨退货对象">{{ itemData&&itemData.targetName || '--' }}</a-form-model-item>
        <a-form-model-item label="产品价格" prop="priceType">
          <a-radio-group id="allocateBill-print-priceType" v-model="form.priceType">
            <a-radio value="thj" id="allocateBill-print-priceType1" v-if="$hasPermissions('B_transferReturnPrint_salesPrice')">退货价</a-radio>
            <a-radio value="cbj" id="allocateBill-print-priceType2" v-if="$hasPermissions('B_transferReturnPrint_costPrice')">成本价</a-radio>
            <a-radio value="bdy" id="allocateBill-print-priceType3">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="货位编号" prop="orderBy">
          <a-radio-group id="allocateBill-print-orderBy" v-model="form.orderBy">
            <a-radio value="ASC" id="allocateBill-print-orderBy1">打印（↑升序）</a-radio>
            <a-radio value="DESC" id="allocateBill-print-orderBy2">打印（↓降序）</a-radio>
            <a-radio value="-1" id="allocateBill-print-orderBy3">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="allocateBill-print-cancel" @click="handleSave(0)">取消</a-button>
        <a-button type="primary" id="allocateBill-print-save" @click="handleSave(1)" style="margin-left: 15px;">确定</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 {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 提交参数
      form: {
        id: 'all',
        priceType: undefined, // 产品价格
        orderBy: undefined// 货位编号
      },
      // 表单验证规则
      rules: {
        priceType: [{ required: true, message: '请选择产品价格', trigger: 'change' }],
        orderBy: [{ required: true, message: '请选择货位编号', trigger: 'change' }]
      },
      // form表单label布局
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      }
    }
  },
  methods: {
    // 确认
    handleSave (type) {
      const _this = this
      if (type == 0) {
        _this.$emit('close')
        return
      }
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 确定
          if (type == 1) {
            const obj = {
              allocateReturnSn: _this.itemData.allocateReturnSn,
              printType: _this.form.priceType,
              isPreview: this.nowType
            }
            if (_this.form.orderBy != '-1') {
              obj.orderBy = _this.form.orderBy
            }
            _this.$store.state.app.curActionPermission = 'B_transferReturnPrint'
            _this.$emit('ok', obj)
          }
          _this.isShow = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        if (!this.$hasPermissions('B_transferReturnPrint_salesPrice') && !this.$hasPermissions('B_transferReturnPrint_costPrice')) {
          this.form.priceType = 'bdy'
        }
      }
    }
  }
}
</script>

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