<template>
  <a-modal
    centered
    class="addSubsidy-modal"
    :footer="null"
    :maskClosable="false"
    title="新增"
    v-model="isShow"
    @cancel="isShow=false"
    :width="700">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="addSubsidyModal-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <div class="formList" style="margin-bottom:15px;">
            <a-radio-group id="addSubsidyModal-defaultFlag" v-model="form.defaultFlag">
              <a-radio :value="0" id="addSubsidyModal-defaultFlag0">
                指定客户
              </a-radio>
              <a-radio :value="1" id="addSubsidyModal-defaultFlag1">
                默认
              </a-radio>
            </a-radio-group>
          </div>
          <a-form-model-item label="客户名称" prop="dealerSn" v-if="form.defaultFlag==0">
            <dealerSubareaScopeList style="width:90%;" ref="dealerSubareaScopeList" id="addSubsidyModal-dealerName" @change="custChange" />
          </a-form-model-item>
          <a-form-model-item label="增量补贴（季度）" prop="ruleTarget1">
            <div>
              大于等于<a-input-number
                v-model="form.ruleTarget1"
                :min="1"
                style="width:80px;margin:0 6px;"
                :max="99999999"
                id="addSubsidyModal-ruleTarget1"
                :precision="0"
                placeholder="请输入"/>条,每条补贴
              <a-input-number
                v-model="form.ruleValue1"
                :min="0.01"
                id="addSubsidyModal-ruleValue1"
                style="width:80px;margin:0 6px;"
                :max="99999999"
                :precision="2"
                placeholder="请输入"/>元
            </div>
            <div>
              大于等于<a-input-number
                v-model="form.ruleTarget2"
                :min="form.ruleTarget1*1+1"
                style="width:80px;margin:0 6px;"
                :max="99999999"
                id="addSubsidyModal-ruleTarget2"
                :precision="0"
                placeholder="请输入"/>条,每条补贴
              <a-input-number
                v-model="form.ruleValue2"
                :min="form.ruleValue1*1+0.01"
                id="addSubsidyModal-ruleValue2"
                style="width:80px;margin:0 6px;"
                :max="99999999"
                :precision="2"
                placeholder="请输入"/>元
            </div>
            <div>
              大于等于<a-input-number
                v-model="form.ruleTarget3"
                :min="form.ruleTarget2*1+1"
                style="width:80px;margin:0 6px;"
                :max="99999999"
                id="addSubsidyModal-ruleTarget3"
                :precision="0"
                placeholder="请输入"/>条,每条补贴
              <a-input-number
                v-model="form.ruleValue3"
                :min="form.ruleValue2*1+0.01"
                id="addSubsidyModal-ruleValue3"
                style="width:80px;margin:0 6px;"
                :max="99999999"
                :precision="2"
                placeholder="请输入"/>元
            </div>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="addSubsidyModal-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="addSubsidyModal-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
// 组件
import { VSelect } from '@/components'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
// 接口
import { subsidyRuleSave, createSubsidyRule } from '@/api/subsidyRule'
export default {
  name: 'AddSubsidyModal',
  components: { VSelect, dealerSubareaScopeList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    pageType: { //  页面类型
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 17 }
      },
      // 提交数据
      form: {
        defaultFlag: 0, // 0指定客户  1默认
        dealerSn: undefined, // 客户名称
        ruleTarget1: undefined, // 增量补贴
        ruleValue1: undefined,
        ruleTarget2: undefined,
        ruleValue2: undefined,
        ruleTarget3: undefined,
        ruleValue3: undefined
      },
      // 必填项判断
      rules: {
        dealerSn: [{ required: true, message: '请选择客户名称', trigger: 'change' }],
        ruleTarget1: [{ required: true, message: '增量补贴不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 客户名称 change
    custChange (val) {
      if (val && val.key) {
        this.form.dealerSn = val.key
      } else {
        this.form.dealerSn = undefined
      }
    },
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 为空判断
          const newForm = JSON.parse(JSON.stringify(_this.form))
          delete newForm.defaultFlag
          delete newForm.dealerSn
          const flagArr = []
          for (const i in newForm) { flagArr.push(newForm[i]) }
          if (flagArr && flagArr.length < 6) {
            _this.$message.error('增量补贴不能为空！')
            return
          }
          _this.form.ruleType = _this.pageType
          _this.spinning = true
          subsidyRuleSave(this.form).then(res => {
            if (res.status == 200) {
              if (!res.data) {
                this.isShow = false
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                _this.openTip(res.data)
              }
            }
            _this.spinning = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    openTip (code) {
      const _this = this
      if (code === 'IN_USE') {
        this.$confirm({
          title: '提示',
          content: '将重新生成新的运费补贴，并结束当前运费补贴。',
          centered: true,
          onOk () {
            createSubsidyRule(_this.form).then(res => {
              if (res.status == 200) {
                _this.isShow = false
                _this.$message.success(res.message)
                _this.$emit('ok')
              }
            })
          }
        })
      } else {
        this.$confirm({
          title: '提示',
          content: '已存在【未开始】的运费补贴',
          centered: true,
          okText: '知道了',
          cancelButtonProps: {
            style: {
              display: 'none' // 通过设置样式隐藏取消按钮
            }
          },
          onOk () {
            console.log('知道了')
          }
        })
      }
    },
    // 重置
    resetFormData () {
      this.form.defaultFlag = 0
      this.form.dealerSn = undefined
      this.form.ruleTarget1 = undefined
      this.form.ruleValue1 = undefined
      this.form.ruleTarget2 = undefined
      this.form.ruleValue2 = undefined
      this.form.ruleTarget3 = undefined
      this.form.ruleValue3 = undefined
      this.$refs.dealerSubareaScopeList.resetForm()
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetFormData()
      }
    }
  }
}
</script>

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