<template>
  <a-modal
    centered
    class="transferOutDs-modal"
    :footer="null"
    :maskClosable="false"
    title="提交调拨单"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="transferOutDs-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="收货客户名称" v-if="dealerLevel != 'OTHER'">
            <dealerSubareaScopeList id="transferOutDs-custList" ref="custList" defValKey="buyerSn" @change="custChange" v-model="form.receiverSn" />
          </a-form-model-item>
          <a-form-model-item label="收货客户名称" v-else>
            <a-input
              id="transferOutDs-receiverName"
              :maxLength="30"
              v-model.trim="form.receiverName"
              placeholder="请输入收货客户名称"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="发货编号" prop="sendNo">
            <a-input
              id="transferOutDs-sendNo"
              :maxLength="10"
              v-model.trim="form.sendNo"
              placeholder="请输入发货编号"
              allowClear />
            <div style="height: 24px;line-height: normal;">
              常用：<a-button size="small" v-for="i in 5" @click="setSendNo(i)" type="link">{{ i }}</a-button>
            </div>
          </a-form-model-item>
          <a-form-model-item label="发货说明">
            <a-input
              id="transferOutDs-explainInfo"
              type="textarea"
              :maxLength="500"
              v-model.trim="form.explainInfo"
              placeholder="请输入发货说明(最多500个字符)"/>
          </a-form-model-item>
          <a-form-model-item label="备货打印" prop="printState" v-if="showPrint">
            <a-radio-group id="transferOutDs-printState" v-model="form.printState">
              <a-radio value="NO_PRINT" id="transferOutDs-printState1">
                允许打印
              </a-radio>
              <a-radio value="UNABLE_PRINT" id="transferOutDs-printState2">
                暂不打印
              </a-radio>
              <a-radio value="CANCEL_PRINT" id="transferOutDs-printState3">
                取消打印
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="transferOutDs-save" @click="handleSave">确定</a-button>
          <a-button id="transferOutDs-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
export default {
  name: 'TransferOutDsModal',
  mixins: [commonMixin],
  components: { dealerSubareaScopeList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    dealerLevel: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      // form表单label布局
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      // 提交参数
      form: {
        sendNo: '', // 发货编号
        printState: '', // 备货打印状态
        receiverSn: '', // 收货客户sn
        receiverName: '', // 收货客户名称
        explainInfo: ''// 发货说明
      },
      // 表单验证规则
      rules: {
        sendNo: [{ required: true, message: '请输入发货编号', trigger: 'change' }],
        printState: [{ required: true, message: '请选择备货打印', trigger: 'change' }]
      },
      detailData: null, //  详情数据
      showPrint: false
    }
  },
  methods: {
    // 初始化
    pageInit (data) {
      this.showPrint = !data.sendNo
      this.form.sendNo = data.sendNo
      this.form.printState = this.showPrint ? data.printState : undefined
      this.form.receiverSn = data.receiverSn
      this.form.receiverName = data.receiverName
      this.form.explainInfo = data.explainInfo
    },
    //  弹框关闭
    handleCancel () {
      this.isShow = false
    },
    // 获取常用发货编号
    setSendNo (i) {
      this.form.sendNo = i
      this.$refs.ruleForm.validateField('sendNo')
    },
    // 收货客户名称
    custChange (val) {
      this.form.receiverSn = val.key || ''
      this.form.receiverName = val.name || ''
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          _this.$emit('ok', formData)
        } else {
          return false
        }
      })
    },
    //  获取详情
    setDetail (data) {
      this.detailData = data
    },
    // 重置表单
    resetForm () {
      this.detailData = null
      this.$refs.ruleForm.resetFields()
      this.form = {
        sendNo: '',
        printState: '',
        receiverSn: '',
        receiverName: ''
      }
      this.$refs.custList.resetForm()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      } else {
        if (this.form.receiverName && this.dealerLevel != 'OTHER') {
          this.$nextTick(() => {
            this.$refs.custList.setDufaultVal(this.form.receiverName)
          })
        }
      }
    }
  }
}
</script>

<style lang="less">
  .transferOutDs-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
