<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-row :gutter="15">
          <a-col :span="20">
            <a-form-model-item label="运费" prop="tireSubsidyYf">
              <a-radio-group name="radioGroup" v-model="form.tireSubsidyYf">
                <a-radio value="1">剔除</a-radio>
                <a-radio value="0">不剔除</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="15">
          <a-col :span="20">
            <a-form-model-item label="服务费" prop="tireSubsidyFwf">
              <a-radio-group name="radioGroup" v-model="form.tireSubsidyFwf">
                <a-radio value="1">剔除</a-radio>
                <a-radio value="0">不剔除</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="15">
          <a-col :span="20">
            <a-form-model-item label="增量补贴" prop="tireSubsidyZl">
              <a-radio-group name="radioGroup1" v-model="form.tireSubsidyZl">
                <a-radio value="1">剔除</a-radio>
                <a-radio value="0">不剔除</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="15">
          <a-col :span="20">
            <a-form-model-item label="供应商返利" prop="tireSubsidyGc">
              <a-radio-group name="radioGroup2" v-model="form.tireSubsidyGc">
                <a-radio value="1">剔除</a-radio>
                <a-radio value="0">不剔除</a-radio>
              </a-radio-group>
            </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: 'BaseModal',
  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: 'TIRE_SUBSIDY',
        bizSn: undefined, // bizsn
        tireSubsidyYf: undefined,
        tireSubsidyFwf: undefined,
        tireSubsidyZl: undefined,
        tireSubsidyGc: undefined
      },
      rules: {
        bizNo: [{ required: true, message: '请输入业务单号', trigger: ['change', 'blur'] }],
        dealerSn: [{ required: true, message: '请选择客户', trigger: ['change', 'blur'] }],
        tireSubsidyYf: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        tireSubsidyFwf: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        tireSubsidyZl: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        tireSubsidyGc: [{ 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: 'TIRE_SUBSIDY' }).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: 'TIRE_SUBSIDY',
        bizNo: '',
        dealerSn: undefined,
        bizSn: undefined, // bizsn
        tireSubsidyYf: undefined,
        tireSubsidyFwf: undefined,
        tireSubsidyZl: undefined,
        tireSubsidyGc: undefined
      }
      this.bizSn = undefined
      this.dealerData = null
      this.$refs.ruleForm.resetFields()
      this.$emit('cancel')
    },
    getDetail () {
      bizRemoveConfigDetail({ bizSn: this.bizSn, configType: 'TIRE_SUBSIDY' }).then(res => {
        const { bizSn, bizNo, dealerSn, dealerName, tireSubsidyYf, tireSubsidyFwf, tireSubsidyZl, tireSubsidyGc } = res.data
        this.dealerData = { dealerName: dealerName }
        this.form = Object.assign(this.form, {
          bizNo: bizNo,
          dealerSn: dealerSn,
          bizSn: bizSn,
          tireSubsidyYf: tireSubsidyYf,
          tireSubsidyFwf: tireSubsidyFwf,
          tireSubsidyZl: tireSubsidyZl,
          tireSubsidyGc: tireSubsidyGc
        })
      })
    },
    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>
