<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :width="560"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="chooseCustom-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-row :gutter="15">
          <a-col :span="20">
            <a-form-model-item label="业务单号" prop="bizNo">
              <a-input
                id="base-bizNo"
                :disabled="bizSn"
                v-model.trim="form.bizNo"
                @change="fetchUser"
                allowClear
                placeholder="请输入业务单号"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="15">
          <a-col :span="20">
            <a-form-model-item label="客户名称" prop="dealerSn">
              {{ dealerData&&dealerData.dealerName||'--' }}
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
          <a-button @click="cancel" style="margin-right: 15px" id="chooseCustom-btn-back">取消</a-button>
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="chooseCustom-btn-submit">保存</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import debounce from 'lodash/debounce'
import warehouse from '@/views/common/chooseWarehouse.js'
import { bizRemoveConfigCreate, bizRemoveConfigModify, bizRemoveConfigDetail, salesReturnDetailByNo } from '@/api/bizRemove'
export default {
  name: 'ErpBaseModal',
  mixins: [commonMixin],
  components: { VSelect, warehouse },
  props: {
    show: [Boolean]
  },
  data () {
    this.lastFetchId = 0
    this.fetchUser = debounce(this.fetchUser, 800)
    return {
      opened: this.show,
      spinning: false,
      title: '新增配置',
      confirmLoading: false,
      formItemLayout: {
        labelCol: { span: 8 },
        wrapperCol: { span: 16 }
      },
      form: {
        bizNo: '',
        dealerSn: undefined,
        configType: 'SALES_RETURN_SYNC',
        bizSn: undefined, // bizsn
        salesReturnSyncErp: '1'
      },
      rules: {
        bizNo: [{ required: true, message: '请输入业务单号', trigger: ['change', 'blur'] }],
        dealerSn: [{ required: true, message: '请选择客户', trigger: ['change', 'blur'] }],
        salesReturnSyncErp: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }]
      },
      fetching: false,
      dealerData: null,
      bizSn: undefined
    }
  },
  methods: {
    fetchUser () {
      if (!this.form.bizNo) return
      this.lastFetchId += 1
      const fetchId = this.lastFetchId
      this.dealerData = null
      this.spinning = true
      this.fetching = true
      salesReturnDetailByNo({ bizNo: this.form.bizNo, configType: 'SALES_RETURN_SYNC' }).then(res => {
        if (fetchId !== this.lastFetchId) {
          return
        }
        if (res.data) {
          this.dealerData = res.data
          this.form.dealerSn = res.data.dealerSn
          this.form.bizSn = res.data.bizSn
        } else {
          this.dealerData = null
          this.form.dealerSn = undefined
          this.form.bizSn = undefined
        }
        this.fetching = false
        this.spinning = false
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 保存
          _this.salesSaveFun()
        } else {
          return false
        }
      })
    },
    // 新建业务单
    salesSaveFun () {
      const _this = this
      const form = this.form
      const funs = !this.bizSn ? bizRemoveConfigCreate : bizRemoveConfigModify
      _this.spinning = true
      funs(form).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('ok', res.data)
          _this.cancel()
        }
        _this.spinning = false
      })
    },
    cancel () {
      this.opened = false
      this.form = {
        configType: 'SALES_RETURN_SYNC',
        bizNo: '',
        dealerSn: undefined,
        bizSn: undefined, // bizsn
        salesReturnSyncErp: '1'
      }
      this.bizSn = undefined
      this.dealerData = null
      this.$refs.ruleForm.resetFields()
      this.$emit('cancel')
    },
    getDetail () {
      bizRemoveConfigDetail({ bizSn: this.bizSn, configType: 'SALES_RETURN_SYNC' }).then(res => {
        const { bizSn, bizNo, dealerSn, dealerName } = res.data
        this.dealerData = { dealerName: dealerName }
        this.form = Object.assign(this.form, {
          bizNo: bizNo,
          dealerSn: dealerSn,
          bizSn: bizSn,
          salesReturnSyncErp: '1'
        })
      })
    },
    edit (row) {
      if (row && row.bizSn) {
        this.title = '编辑配置'
        this.bizSn = row.bizSn
        this.getDetail()
      } else {
        this.title = '新增配置'
        this.bizSn = undefined
      }
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
    }
  }
}
</script>
