<template>
  <div>
    <a-modal
      centered
      wrapClassName="settlementModifyModal-modal"
      :footer="null"
      :maskClosable="false"
      title="修改结算方式"
      v-model="isshow"
      @cancle="handleBack"
      :width="650">
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="settlementModifyModal-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-row :gutter="24">
            <a-col :span="20">
              <a-form-model-item label="当前结算方式">
                {{ settleInfo&&settleInfo.settleType=='CREDIT'?'授信结算':'按月结算' }}
              </a-form-model-item>
            </a-col>
            <a-col :span="20">
              <a-form-model-item label="新结算方式" prop="settleType">
                <a-select
                  style="background-color: none;"
                  placeholder="请选择结算方式"
                  id="settlementModifyModal-settleType"
                  allowClear
                  @change="changeSettleType"
                  v-model="form.settleType"
                >
                  <a-select-option v-for="item in settlementTypeList" :key="item.code" :value="item.code" :disabled="settleInfo&&settleInfo.settleType==item.code?true:false">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-row v-if="form.settleType=='CREDIT'">
              <a-col :span="20">
                <a-form-model-item label="授信额度" prop="creditLimit">
                  <a-input-number
                    id="settlementModifyModal-creditLimit"
                    v-model="form.creditLimit"
                    :precision="2"
                    :min="0.01"
                    :max="999999"
                    placeholder="请输入正数（最多允许两位小数）"
                    style="width: 100%;display: inline-block;" />
                </a-form-model-item>
              </a-col>
              <a-col :span="4"> <span style="display: inline-block; padding: 10px;">元</span></a-col>
            </a-row>
            <a-col :span="20" v-if="form.settleType=='MONTH'">
              <a-form-model-item label="结算日期" prop="settleDay" >
                <div style="display: flex;padding: 0;align-items: center;">
                  <span style="display: inline-block;margin-right: 10px;">每月</span>
                  <a-select
                    style="background-color: none;width: 65%"
                    placeholder="请选择结算日期"
                    id="settlementModifyModal-settleDay"
                    allowClear
                    v-model="form.settleDay"
                  >
                    <a-select-option v-for="item in dateList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
                  </a-select>
                  <span style="display: inline-block;margin-left: 10px;">号</span>
                </div>
              </a-form-model-item>
            </a-col>
            <a-row v-if="form.settleType=='CREDIT'">
              <a-col :span="20">
                <a-form-model-item label="结算提醒金额" prop="creditWarnLimit" >
                  <a-input-number
                    id="settlementModifyModal-creditWarnLimit"
                    v-model="form.creditWarnLimit"
                    :precision="2"
                    :min="0.01"
                    :max="999999"
                    placeholder="请输入正数（最多允许两位小数）"
                    style="width: 100%;display: inline-block;" />
                </a-form-model-item>
              </a-col>
              <a-col :span="4"> <span style="display: inline-block; padding: 10px;">元</span></a-col>
            </a-row>
            <a-col :span="20" v-if="form.settleType=='MONTH'">
              <a-form-model-item label="结算提醒" prop="warnBeforeDays" >
                <div style="display: flex;padding: 0;align-items: center;">
                  <span style="display: inline-block;margin-right: 10px;">提前</span>
                  <a-select
                    style="background-color: none;width: 65%;"
                    placeholder="请选择结算提醒日期"
                    id="settlementModifyModal-warnBeforeDays"
                    allowClear
                    v-model="form.warnBeforeDays"
                  >
                    <a-select-option v-for="item in dateTXList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
                  </a-select>
                  <span style="display: inline-block;margin-left: 10px;">天开始提醒</span>
                </div>
              </a-form-model-item>
            </a-col>
            <a-col :span="20">
              <a-form-model-item :label="form.settleType=='CREDIT'?'超过授信额度':'超期未结算'" v-if="form.settleType">
                <a-checkbox id="settlementModifyModal-onChange" :checked="form.limitFlag==1" @change="onChange">
                  限制汽车修理厂无法使用数字货架
                </a-checkbox>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;">
            <a-button type="primary" @click="handleSubmit()" :style="{ marginRight: '8px' }" id="settlementModifyModal-handleSubmit">保存</a-button>
            <a-button :style="{ marginLeft: '8px' }" @click="handleBack" id="settlementModifyModal-handleBack">取消</a-button>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import { STable, VSelect, Upload } from '@/components'
import { shelfSettleRuleSave } from '@/api/shelf.js'
export default {
  name: 'SettlementModifyModal',
  components: {
    STable, VSelect, Upload
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    shelfInfo: {
      type: Object,
      default: function () {
        return {}
      }
    },
    settleInfo: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isshow: this.visible,
      roleList: [],
      formItemLayout: {
        labelCol: { span: 10 },
        wrapperCol: { span: 14 }
      },
      settlementTypeList: [{ code: 'MONTH', name: '按月结算' }, { code: 'CREDIT', name: '授信结算' }],
      dateList: [{ id: 1, name: 1 }, { id: 2, name: 2 }, { id: 3, name: 3 }, { id: 4, name: 4 }, { id: 5, name: 5 }, { id: 6, name: 6 }, { id: 7, name: 7 }, { id: 8, name: 8 }, { id: 9, name: 9 }, { id: 10, name: 10 }, { id: 11, name: 11 }, { id: 12, name: 12 }, { id: 13, name: 13 }, { id: 14, name: 14 }, { id: 15, name: 15 }, { id: 16, name: 16 }, { id: 17, name: 17 }, { id: 18, name: 18 }, { id: 19, name: 19 }, { id: 20, name: 20 }, { id: 21, name: 21 }, { id: 22, name: 22 }, { id: 23, name: 23 }, { id: 24, name: 24 }, { id: 25, name: 25 }],
      dateTXList: [{ id: 1, name: 1 }, { id: 2, name: 2 }, { id: 3, name: 3 }, { id: 4, name: 4 }, { id: 5, name: 5 }, { id: 6, name: 6 }, { id: 7, name: 7 }, { id: 8, name: 8 }, { id: 9, name: 9 }, { id: 10, name: 10 }],
      form: {
        settleType: undefined, // 结算方式
        creditLimit: '', // 授信额度
        creditWarnLimit: '', // 结算提醒金额
        settleDay: undefined, // 每月结算日
        warnBeforeDays: undefined, // 结算提醒提前天数
        limitFlag: 0// 是否限制使用
      },
      rules: {
        settleType: [ { required: true, message: '请选择结算方式', trigger: 'change' } ],
        creditLimit: [ { required: true, message: '请输入授信额度', trigger: 'blur' } ],
        creditWarnLimit: [{ required: true, message: '请输入结算提醒金额', trigger: 'blur' }],
        settleDay: [ { required: true, message: '请选择每月结算日期', trigger: 'change' } ],
        warnBeforeDays: [ { required: true, message: '请选择每月结算提醒提前的日期', trigger: 'change' } ]
      }
    }
  },
  methods: {
    // 过滤空格
    filterEmpty () {
      let str = this.form.name
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.name = str
    },
    onChange (e) {
      console.log(e.target.checked)
      if (e.target.checked) {
        this.form.limitFlag = 1
      } else {
        this.form.limitFlag = 0
      }
    },
    changeSettleType (value, option) {
      this.form.settleType = value
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = Object.assign({}, _this.form)
          console.log(params,'params---------------------')
          if (params.settleType=='CREDIT'&&params.creditWarnLimit >= params.creditLimit) {
            _this.$message.warning('结算提醒金额需要小于授信额度')
            return
          }
          if (this.shelfInfo) {
            console.log(this.shelfInfo, 'this.nowData')
            params.shelfSn = this.shelfInfo.shelfSn
            params.storeSn = this.shelfInfo.storeSn
            params.dealerSn = this.shelfInfo.dealerSn
          }
          if (this.settleInfo) {
            params.bondAmount = this.settleInfo.bondAmount
            params.bondFlag = this.settleInfo.bondFlag
            params.id = this.settleInfo.id
          }
          _this.spinning = true
          shelfSettleRuleSave(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('close', params.settleType)
              setTimeout(function () {
                _this.isshow = false
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleBack () {
      this.form = {
        settleType: undefined, // 结算方式
        creditLimit: '', // 授信额度
        creditWarnLimit: '', // 结算提醒金额
        settleDay: undefined, // 每月结算日
        warnBeforeDays: undefined, // 结算提醒提前天数
        limitFlag: 0 // 是否限制使用
      }
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (!newValue) {
        this.$refs.ruleForm.resetFields()
        this.$emit('close')
      }
    }
  }
}
</script>
<style lang="less">
.settlementModifyModal-modal{
  .ant-input-number-handler-wrap{
    display: none;
  }
}
</style>
