<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="countrySalesDay">
              <a-radio-group name="radioGroup" v-model="form.countrySalesDay">
                <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="项目销售日报1" prop="dailyPl">
              <a-radio-group name="radioGroup1" v-model="form.dailyPl">
                <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="项目销售日报2" prop="dailyPljl">
              <a-radio-group name="radioGroup2" v-model="form.dailyPljl">
                <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="areaSalesDay">
              <a-radio-group name="radioGroup3" v-model="form.areaSalesDay">
                <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: 6 },
        wrapperCol: { span: 18 }
      },
      form: {
        bizNo: '',
        dealerSn: undefined,
        configType: 'DAILY_REPORT',
        bizSn: undefined, // bizsn
        dailyPl: undefined,
        countrySalesDay: undefined,
        dailyPljl: undefined,
        areaSalesDay: undefined
      },
      rules: {
        bizNo: [{ required: true, message: '请输入业务单号', trigger: ['change', 'blur'] }],
        dealerSn: [{ required: true, message: '请选择客户', trigger: ['change', 'blur'] }],
        dailyPl: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        countrySalesDay: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        dailyPljl: [{ required: true, message: '请选择', trigger: ['change', 'blur'] }],
        areaSalesDay: [{ 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.fetching = true
      this.spinning = true
      salesReturnDetailByNo({ bizNo: this.form.bizNo, configType: 'DAILY_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.salesReturnBillSn
        } else {
          this.dealerData = null
          this.form.dealerSn = undefined
          this.form.bizSn = undefined
        }
        this.spinning = false
        this.fetching = 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: 'DAILY_REPORT',
        bizNo: '',
        dealerSn: undefined,
        bizSn: undefined, // bizsn
        dailyPl: undefined,
        countrySalesDay: undefined,
        dailyPljl: undefined,
        areaSalesDay: undefined
      }
      this.bizSn = undefined
      this.dealerData = null
      this.$refs.ruleForm.resetFields()
      this.$emit('cancel')
    },
    getDetail () {
      bizRemoveConfigDetail({ bizSn: this.bizSn, configType: 'DAILY_REPORT' }).then(res => {
        const { bizSn, bizNo, dealerSn, dealerName, areaSalesDay, countrySalesDay, dailyPl, dailyPljl } = res.data
        this.dealerData = { dealerName: dealerName }
        this.form = Object.assign(this.form, {
          bizNo: bizNo,
          dealerSn: dealerSn,
          bizSn: bizSn,
          dailyPl: dailyPl,
          countrySalesDay: countrySalesDay,
          dailyPljl: dailyPljl,
          areaSalesDay: areaSalesDay
        })
      })
    },
    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>
