<template>
  <ui-form ref="form" :rules="formRules" :model="formData" class="bi-settlement-rule-change" label-width="80px">
    <div class="bi-settlement-rule-change__tips">重新选择规则请设置新规则生效时间，该时间后的订单将按照新规则进行结算</div>
    <ui-form-item prop="nowRuleId" label="规则名称">
      <ui-select
        placeholder="请输入规则名称"
        v-model="formData.nowRuleId"
        :query-method="handleQueryRuleData"
        text-field="ruleName"
        value-field="id"
      ></ui-select>
    </ui-form-item>
    <ui-form-item prop="startTime" label="生效时间">
      <ui-date-picker
        placeholder="请选择生效时间"
        v-model="formData.startTime"
        value-format="yyyy-MM-dd"
        :picker-options="pickerOptions"
      ></ui-date-picker>
    </ui-form-item>
    <ui-toolbar slot="toolbar" align="right">
      <ui-toolbar-item>
        <ui-button>取消</ui-button>
      </ui-toolbar-item>
      <ui-toolbar-item>
        <ui-button type="primary" :click="handleConfirmClick">确定</ui-button>
      </ui-toolbar-item>
    </ui-toolbar>
  </ui-form>
</template>
<script>
import { parseDate } from '@huntianning/components/src/utils/date'

export default {
  name: 'BiSettlementRuleChange',
  props: {
    minTime: {
      type: String
    },
    maxTime: {
      type: String
    },
    ruleId: {
      type: String
    },
    success: {
      type: Function
    },
    loadSettlementRuleDataMethod: {
      type: Function
    },
    changeSettlementRuleMethod: {
      type: Function
    }
  },
  data() {
    return {
      formData: {
        nowRuleId: null,
        startTime: null
      }
    }
  },
  computed: {
    innerMinTime() {
      if (!this.minTime) {
        return
      }
      return parseDate(this.minTime)
    },
    innerMaxTime() {
      if (!this.maxTime) {
        return
      }
      return parseDate(this.maxTime)
    },
    pickerOptions() {
      return {
        disabledDate: (date) => {
          let disabled = false
          if (this.innerMinTime) {
            disabled = this.innerMinTime >= date
          }
          if (!disabled) {
            if (this.innerMaxTime) {
              disabled = this.innerMaxTime < date
            }
          }
          return disabled
        }
      }
    },
    formRules() {
      return {
        nowRuleId: [{
          required: true,
          message: '规则不能为空',
          trigger: 'change'
        }],
        startTime: [{
          required: true,
          message: '生效时间不能为空',
          trigger: 'change'
        }]
      }
    }
  },
  methods: {
    handleQueryRuleData() {
      return this.loadSettlementRuleDataMethod()
    },
    handleConfirmClick() {
      return this.$refs.form.validate().then(() => {
        return this.changeSettlementRuleMethod(Object.assign({}, this.formData, {
          ruleId: this.ruleId
        })).then(() => {
          return Promise.all([this.success ? this.success() : null]).finally(() => {
            this.$success('更新成功')
          })
        })
      })
    }
  }
}
</script>
<style lang="less">
.bi-settlement-rule-change__tips{
  color: #999;
  margin-bottom: 24px;
}
</style>
