<template>
  <a-modal
    centered
    class="costAdd-modal"
    :footer="null"
    :maskClosable="false"
    :title="!itemSn?'新增':'编辑'"
    v-model="isShow"
    @cancel="isShow=false"
    :width="700">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="costAddModal-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="开始时间" v-if="itemSn&&timeInfo">
            {{ timeInfo }}
          </a-form-model-item>
          <a-form-model-item label="返利基数" prop="ruleValue1">
            <a-input-number
              id="costAddModal-ruleValue1"
              v-model="form.ruleValue1"
              style="width:90%;"
              :max="999999"
              :precision="2"
              placeholder="请输入返利基数"/>
            <span style="margin-left:10px;">元</span>
          </a-form-model-item>
          <a-form-model-item label="当前返利基数" v-if="rebateList&&rebateList.length>0">
            {{ (rebateList[0].ruleValue1||rebateList[0].ruleValue1==0)?toThousands(rebateList[0].ruleValue1) :'--' }}
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="costAddModal-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="costAddModal-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
// 接口
import { getSubsidyRuleList, subsidyRuleSave, createSubsidyRule, editSubsidyRule } from '@/api/subsidyRule'
export default {
  name: 'CostAddModal',
  mixins: [commonMixin],
  components: { VSelect, custList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    pageType: { //  页面类型
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false, // 页面加载状态
      formItemLayout: {// form表单中label设置
        labelCol: { span: 4 },
        wrapperCol: { span: 17 }
      },
      // 提交数据
      form: {
        defaultFlag: 1, // 0指定客户  1默认
        dealerSn: undefined, // 客户名称
        ruleValue1: undefined, // 返利基数
        ruleType: undefined, // 弹窗类型
        effectType: 0
      },
      // 必填项判断
      rules: {
        ruleValue1: [{ required: true, message: '请输入返利基数', trigger: 'blur' }]
      },
      rebateList: null, // 当前返利基数 数据
      itemSn: null, // 当前行ruleSn
      timeInfo: null// 开始时间
    }
  },
  methods: {
    // 获取当前返利基数  历史记录
    getHistoryData (ajaxData) {
      this.spinning = true
      getSubsidyRuleList(ajaxData).then(res => {
        if (res.status == 200) {
          this.rebateList = res.data
        } else {
          this.rebateList = null
        }
        this.spinning = false
      })
    },
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (_this.form.ruleValue1 < 0) {
            _this.$message.warning('请输入大于等于0的返利基数')
            return
          }
          _this.form.ruleType = _this.pageType
          _this.spinning = true
          const ajaxName = _this.itemSn ? editSubsidyRule : subsidyRuleSave
          _this.form.ruleSn = _this.itemSn ? _this.itemSn : undefined
          ajaxName(_this.form).then(res => {
            if (res.status == 200) {
              if (!_this.itemSn) {
                if (!res.data) {
                  _this.isShow = false
                  _this.$message.success(res.message)
                  _this.$emit('ok')
                } else {
                  _this.openTip(res.data)
                }
              } else {
                _this.isShow = false
                _this.$message.success(res.message)
                _this.$emit('ok')
              }
            }
            _this.spinning = false
          }).catch(err => {
            _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 {
        const tipWord = '已存在【启用中】的返利基数'
        _this.setInfoModal(tipWord)
      }
    },
    // 提示弹窗
    setInfoModal (tip) {
      this.$info({
        title: '提示',
        content: tip,
        centered: true,
        onOk () {
          console.log('知道了')
        }
      })
    },
    // 编辑获取数据
    pageInit (itemData) {
      this.itemSn = itemData.ruleSn
      this.form.ruleValue1 = itemData.ruleValue1
      this.timeInfo = itemData.ruleStartDate
    },
    // 重置
    resetFormData () {
      this.form.defaultFlag = 1
      this.form.dealerSn = undefined
      this.form.ruleValue1 = undefined
      this.form.ruleType = undefined
      this.itemSn = null
      this.form.effectType = 0
      this.rebateList = null
      this.timeInfo = null
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetFormData()
      } else {
        if (!this.itemSn) {
          this.form.ruleValue1 = undefined
          this.getHistoryData({ defaultFlag: 1, subsidyQueryStatus: 'NOT_FINISH', ruleType: this.pageType })
        }
      }
    }
  }
}
</script>

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