<template>
  <ui-page
    title="变更规则"
    :back-url="`${rootPath}`"
    :query-method="handleQueryData"
  >
    <template slot="addon">
      <ui-button type="primary" :click="handleConfirmClick">保存</ui-button>
    </template>
    <ui-layout>
      <template slot="sidebar">
        <ui-navmenu>
          <ui-navmenu-group
            :label="menu.menuName"
            v-for="menu in SettlementRuleMenus"
            :key="menu.id"
          >
            <ui-navmenu-item
              v-for="item in menu.menuList"
              :key="item.id"
              :url="`${rootPath}/adjust/${menu.id}/${item.id}`"
            >{{ item.menuName }}</ui-navmenu-item>
          </ui-navmenu-group>
        </ui-navmenu>
      </template>
      <router-view :key="viewKey"></router-view>
    </ui-layout>
  </ui-page>
</template>
<script>
export default {
  name: 'BiSettlementRuleAdjust',
  props: {
    rootPath: {
      type: String
    },
    loadSettlementRuleMenuDataMethod: {
      type: Function
    },
    getSettlementRuleDetailMethod: {
      type: Function
    },
    getCurrentSettlementRuleDetailMethod: {
      type: Function
    },
    updateCurrentSettlementRuleMethod: {
      type: Function
    }
  },
  provide() {
    return {
      AdjustRule: {
        getData: (name) => {
          return this.adjustRuleData[name]
        },
        setData: (name, value) => {
          this.adjustRuleData[name] = value
        }
      }
    }
  },
  computed: {
    viewKey() {
      return `${this.$route.params.class}/${this.$route.params.type}`
    }
  },
  data() {
    return {
      SettlementRuleMenus: [],
      adjustRuleData: null
    }
  },
  methods: {
    handleConfirmClick() {
      this.$confirm('确定', '修改后，使用该规则的项目的所有订单将全部按照修改后的规则进行结算，是否确认修改？').then(() => {
        return this.updateCurrentSettlementRuleMethod({
          id: this.adjustRuleData.id,
          ruleName: this.adjustRuleData.ruleName,
          minGuaranteeRule: JSON.stringify(this.adjustRuleData.minGuaranteeRule),
          runRule: JSON.stringify(this.adjustRuleData.runRule),
          vehicleRule: JSON.stringify(this.adjustRuleData.vehicleRule),
          taxRate: this.adjustRuleData.taxRate,
          type: this.adjustRuleData.type
        }).then(() => {
          this.$success('保存成功')
        })
      })
    },
    handleQueryData() {
      return Promise.all([
        this.loadSettlementRuleMenuDataMethod().then(rs => {
          this.SettlementRuleMenus = rs || []
          if (rs && rs.length && (!this.$route.params.class || !this.$route.params.type)) {
            this.$router.replace({
              path: `${this.rootPath}/adjust/${rs[0].id}/${rs[0].menuList[0].id}`
            })
          }
        }),
        this.getCurrentSettlementRuleDetailMethod().then(data => {
          const current = data.find(v => v.flag === '1')
          return this.getSettlementRuleDetailMethod(current.id).then((rs) => {
            if (rs && rs.minGuaranteeRule) {
              if (typeof rs.minGuaranteeRule === 'string') {
                rs.minGuaranteeRule = JSON.parse(rs.minGuaranteeRule)
              }
            }
            if (rs && rs.vehicleRule) {
              if (typeof rs.vehicleRule === 'string') {
                rs.vehicleRule = JSON.parse(rs.vehicleRule)
              }
            }
            if (rs && rs.runRule) {
              if (typeof rs.runRule === 'string') {
                rs.runRule = JSON.parse(rs.runRule)
              }
            }
            this.adjustRuleData = JSON.parse(JSON.stringify(rs))
          })
        })
      ])
    }
  }
}
</script>
