<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.01"
                style="width:90%;"
                :max="pageType==='SERVICE_FEE'?100:99999999"
                :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="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>
          </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) {
      // 检查阶梯1是否有值
      const hasStep1 = obj.ruleTarget1 && obj.ruleValue1
      // 检查阶梯2是否有值
      const hasStep2 = obj.ruleTarget2 && obj.ruleValue2
      // 检查阶梯3是否有值
      const hasStep3 = obj.ruleTarget3 && obj.ruleValue3
      // 情况1: 阶梯1必须有值
      if (!hasStep1) {
        return false
      }
      // 情况2: 如果阶梯3有值，阶梯1和阶梯2都不能为空
      if (hasStep3 && (!hasStep1 || !hasStep2)) {
        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>
