<template>
  <a-modal
    centered
    class="editTireSetting-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="editTireSettingModal-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="客户名称" v-if="form.defaultFlag==0">
            {{ form.dealerName||'--' }}
          </a-form-model-item>
          <div v-if="pageType==='SERVICE_FEE' || pageType==='SHIPPING_FEE'">
            <a-form-model-item label="开始时间">
              {{ form.ruleStartDate||'--' }}
            </a-form-model-item>
            <a-form-model-item :label="pageType==='SERVICE_FEE'?'服务费比例':'运费补贴'" prop="ruleValue1">
              <a-input-number
                id="editTireSettingModal-ruleValue1"
                v-model="form.ruleValue1"
                :min="0"
                style="width:90%;"
                :max="pageType==='SERVICE_FEE'?100:99"
                :precision="2"
                :placeholder="'请输入'+(pageType==='SERVICE_FEE'?'服务费比例':'运费补贴')"/>
              <span style="margin-left:10px;">{{ pageType==='SERVICE_FEE'?'%':'元' }}</span>
            </a-form-model-item>
          </div>
          <div v-else>
            <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="999999"
                  :precision="2"
                  placeholder="请输入"/>元
              </div>
              <div>
                大于等于<a-input-number
                  v-model="form.ruleTarget2"
                  :min="form.ruleTarget1"
                  style="width:80px;margin:0 6px;"
                  :max="999999"
                  id="addSubsidyModal-ruleTarget2"
                  :precision="0"
                  placeholder="请输入"/>条，每条补贴
                <a-input-number
                  v-model="form.ruleValue2"
                  :min="form.ruleValue1"
                  id="addSubsidyModal-ruleValue2"
                  style="width:80px;margin:0 6px;"
                  :max="999999"
                  :precision="2"
                  placeholder="请输入"/>元
              </div>
              <div>
                大于等于<a-input-number
                  v-model="form.ruleTarget3"
                  :min="form.ruleTarget2"
                  style="width:80px;margin:0 6px;"
                  :max="999999"
                  id="addSubsidyModal-ruleTarget3"
                  :precision="0"
                  placeholder="请输入"/>条，每条补贴
                <a-input-number
                  v-model="form.ruleValue3"
                  :min="form.ruleValue2"
                  id="addSubsidyModal-ruleValue3"
                  style="width:80px;margin:0 6px;"
                  :max="999999"
                  :precision="2"
                  placeholder="请输入"/>元
              </div>
            </a-form-model-item>
          </div>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="editTireSettingModal-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="editTireSettingModal-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
// 接口
import { editSubsidyRule } from '@/api/subsidyRule'
export default {
  name: 'EditTireSettingModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    pageType: { //  页面类型
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false, // 页面加载状态
      formItemLayout: {// form表单中label设置
        labelCol: { span: 5 },
        wrapperCol: { span: 17 }
      },
      // 提交数据
      form: {
        ruleSn: undefined,
        defaultFlag: 0,
        dealerName: undefined,
        ruleStartDate: undefined,
        ruleTarget1: undefined,
        ruleValue1: undefined,
        ruleTarget2: undefined,
        ruleValue2: undefined,
        ruleTarget3: undefined,
        ruleValue3: undefined
      },
      // 必填项判断
      rules: {
        ruleValue1: [{ required: true, message: '请输入服务费比例', trigger: 'blur' }],
        ruleTarget1: [{ required: true, message: '增量补贴不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 为空判断
          if (_this.pageType === 'SUBSIDY_QUARTER_FEE' || _this.pageType === 'SUBSIDY_YEAR_FEE') {
            const newForm = JSON.parse(JSON.stringify(_this.form))
            delete newForm.ruleSn
            delete newForm.defaultFlag
            delete newForm.dealerName
            delete newForm.ruleStartDate
            const flag = _this.isValid(newForm)
            if (!flag) {
              _this.$message.error('季度增量补贴【条数】与【补贴】不符合填写规则！')
              return
            }
          }
          _this.form.ruleType = _this.pageType
          _this.spinning = true
          editSubsidyRule(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
        }
      })
    },
    // 为空判断
    isValid (obj) {
      const { ruleTarget1, ruleValue1, ruleTarget2, ruleValue2, ruleTarget3, ruleValue3 } = obj

      // 检查第一阶梯是否填写
      if (!ruleTarget1 || !ruleValue1) {
        return false
      }

      // 如果第三阶梯存在，检查第二阶梯是否存在以及值是否正确
      if (ruleTarget3 || ruleValue3) {
        if ((!ruleTarget2 || !ruleValue2) || (!ruleTarget3 || !ruleValue3)) {
          return false
        } else {
          if (ruleValue1 >= ruleValue2 || ruleValue2 >= ruleValue3 || ruleTarget1 >= ruleTarget2 || ruleTarget2 >= ruleTarget3) {
            return false
          }
        }
      }

      // 如果只有第二阶梯存在（没有第三阶梯），检查值是否正确
      if ((ruleTarget2 || ruleValue2) && (!ruleTarget3 && !ruleValue3)) {
        if (!ruleTarget2 || !ruleValue2) {
          return false
        } else {
          if (ruleValue1 >= ruleValue2 || ruleTarget1 >= ruleTarget2) {
            return false
          }
        }
      }
      // 所有条件都满足，返回true
      return true
    },
    // 错误提示语
    openTip (code) {
      const _this = this
      this.$info({
        title: '提示',
        content: '当前规则不可操作，请刷新后重试',
        centered: true,
        onOk () {
          console.log('知道了')
        }
      })
    },
    // 重置
    resetFormData () {
      this.form.defaultFlag = 0
      this.form.ruleSn = undefined
      this.form.dealerName = undefined
      this.form.ruleStartDate = 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.ruleForm.resetFields()
    },
    // 获取编辑数据
    getFormData (getData) {
      this.form = getData
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetFormData()
      }
    }
  }
}
</script>

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