<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="salesStatsDealer">
              <a-radio-group name="radioGroup" v-model="form.salesStatsDealer">
                <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="salesStatsProvince">
              <a-radio-group name="radioGroup1" v-model="form.salesStatsProvince">
                <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: 'BILLING_REPORT',
        bizSn: undefined, // bizsn
        salesStatsProvince: undefined,
        salesStatsDealer: undefined
      },
      rules: {
        bizNo: [{ required: true, message: '请输入业务单号', trigger: ['change', 'blur'] }],
        dealerSn: [{ required: true, message: '请选择客户', trigger: ['change', 'blur'] }],
        salesStatsProvince: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        salesStatsDealer: [{ 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: 'BILLING_REPORT' }).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: 'BILLING_REPORT',
        bizNo: '',
        dealerSn: undefined,
        bizSn: undefined, // bizsn
        salesStatsProvince: undefined,
        salesStatsDealer: undefined
      }
      this.bizSn = undefined
      this.dealerData = null
      this.$refs.ruleForm.resetFields()
      this.$emit('cancel')
    },
    getDetail () {
      bizRemoveConfigDetail({ bizSn: this.bizSn, configType: 'BILLING_REPORT' }).then(res => {
        const { bizSn, bizNo, dealerSn, dealerName, salesStatsDealer, salesStatsProvince } = res.data
        this.dealerData = { dealerName: dealerName }
        this.form = Object.assign(this.form, {
          bizNo: bizNo,
          dealerSn: dealerSn,
          bizSn: bizSn,
          salesStatsProvince: salesStatsProvince,
          salesStatsDealer: salesStatsDealer
        })
      })
    },
    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>
