<template>
  <a-modal
    centered
    class="costAdd-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="costAddModal-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="costAddModal-defaultFlag" v-model="form.defaultFlag" @change="defaultChange">
              <a-radio :value="0" id="costAddModal-defaultFlag0">
                指定客户
              </a-radio>
              <a-radio :value="1" id="costAddModal-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="costAddModal-dealerName" @change="custChange" />
          </a-form-model-item>
          <a-form-model-item :label="pageType==='SERVICE_FEE'?'服务费比例':'运费补贴'" prop="ruleValue1">
            <a-input-number
              id="costAddModal-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>
          <a-form-model-item label="生效方式" prop="effectType">
            <a-radio-group v-model="form.effectType" id="costAddModal-effectType">
              <a-radio :value="0" id="costAddModal-effectType0">
                立即生效
              </a-radio>
              <a-radio :value="1" id="costAddModal-effectType1">
                次月生效
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
          <div class="formList formBoxList" v-if="feeDataList&&feeDataList.length>0">
            <div v-for="item in feeDataList" :key="item.id">{{ item.effectType==1?'当前':'次月' }}{{ pageType==='SERVICE_FEE'?'服务费比例':'运费补贴' }}：{{ item.ruleValue1 }}{{ pageType==='SERVICE_FEE'?'%':'元' }}；</div>
          </div>
        </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 { VSelect } from '@/components'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
// 接口
import { getSubsidyRuleList, subsidyRuleSave, createSubsidyRule } from '@/api/subsidyRule'
export default {
  name: 'CostAddModal',
  components: { VSelect, dealerSubareaScopeList },
  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: 0, // 0指定客户  1默认
        dealerSn: undefined, // 客户名称
        ruleValue1: undefined, // 服务费比例
        ruleType: undefined, // 弹窗类型
        effectType: undefined// 生效方式
      },
      // 必填项判断
      rules: {
        dealerSn: [{ required: true, message: '请选择客户名称', trigger: 'change' }],
        ruleValue1: [{ required: true, message: '请输入服务费比例', trigger: 'blur' }],
        effectType: [{ required: true, message: '请选择生效方式', trigger: 'change' }]
      },
      feeDataList: null // 当月/次月费用数据
    }
  },
  methods: {
    // 客户名称 change
    custChange (val) {
      if (val && val.key) {
        this.form.dealerSn = val.key
        this.getHistoryData({ dealerSn: val.key, subsidyQueryStatus: 'NOT_FINISH', ruleType: this.pageType })
      } else {
        this.feeDataList = null
      }
    },
    // 0指定客户  1默认客户  change
    defaultChange (e) {
      this.form.defaultChange = e.target.value
      this.form.dealerSn = undefined
      if (e.target.value == '1') {
        this.getHistoryData({ defaultFlag: 1, subsidyQueryStatus: 'NOT_FINISH', ruleType: this.pageType })
      } else {
        this.feeDataList = null
      }
    },
    // 获取当月/次月  历史数据值
    getHistoryData (ajaxData) {
      this.spinning = true
      getSubsidyRuleList(ajaxData).then(res => {
        if (res.status == 200) {
          this.feeDataList = res.data
        } else {
          this.feeDataList = null
        }
        this.spinning = false
      })
    },
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _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
          }).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: '将重新生成新的' + (_this.pageType === 'SERVICE_FEE' ? '服务费比例' : '运费补贴') + '，并结束当前' + (_this.pageType === 'SERVICE_FEE' ? '服务费比例。' : '运费补贴。'),
          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.$info({
          title: '提示',
          content: '已存在【未开始】的' + (_this.pageType === 'SERVICE_FEE' ? '服务费比例' : '运费补贴'),
          centered: true,
          onOk () {
            console.log('知道了')
          }
        })
      }
    },
    // 重置
    resetFormData () {
      this.form.defaultFlag = 0
      this.form.dealerSn = undefined
      this.form.ruleValue1 = undefined
      this.form.ruleType = undefined
      this.form.effectType = undefined
      if (this.form.defaultFlag == 0) {
        this.$refs.dealerSubareaScopeList.resetForm()
      }
      this.feeDataList = null
      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">
  .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>
