<template>
  <div class="promotionRulesList-rule-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="promotionRulesList-rule-back" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="promotionRulesList-rule-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false" class="promotionRulesList-rule-cont">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button id="promotionRulesList-rule-add" type="primary" class="button-error" @click="openModal=true">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :showPagination="false"
          :defaultLoadData="false"
          bordered>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              class="button-info"
              @click="handleEdit(record)"
              id="promotionRulesList-rule-edit-btn">编辑</a-button>
            <a-button
              size="small"
              type="link"
              class="button-error"
              @click="handleDel(record)"
              id="promotionRulesList-rule-del-btn">删除</a-button>
          </template>
        </s-table>
      </a-card></a-spin>
    <!-- 新增/编辑 -->
    <edit-rule-modal :openModal="openModal" :itemSn="itemSn" @ok="handleOk" @close="closeModal" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import editRuleModal from './editRuleModal.vue'
import { promoRuleQueryList, promoRuleDel } from '@/api/promoRule'
export default {
  name: 'PromotionRulesManagementRule',
  mixins: [commonMixin],
  components: { STable, VSelect, editRuleModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销类型', dataIndex: 'promoRuleTypeDictValue', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销规则', dataIndex: 'promoRule', align: 'center', width: '30%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return promoRuleQueryList({ promoActiveSn: this.$route.params.sn }).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
              if (data[i].promoRuleType == 'BUY_PROD_GIVE_PROD') { //  买产品送产品
                data[i].promoRule = '购买满' + data[i].orderGoodsQty + '个正价产品，送' + data[i].promoQty + '个促销品'
              } else if (data[i].promoRuleType == 'BUY_PROD_GIVE_MONEY') { //  买产品送采购额
                if (data[i].promoType == 'promo_amount') {
                  data[i].promoRule = '购买满' + data[i].orderAmount + '元正价产品，送' + data[i].promoAmount + '元采购额可用于购买促销品'
                } else if (data[i].promoType == 'promo_amount_per') {
                  data[i].promoRule = '购买满' + data[i].orderAmount + '元正价产品可以返正价产品采购额的' + data[i].promoAmountPer * 100 + '%用于购买促销品'
                }
              } else if (data[i].promoRuleType == 'ADD_PRICE_PURCH') { //  加价换购
                data[i].promoRule = '购买满' + data[i].orderGoodsQty + '个正价产品，可以以换购价购买任意1个换购产品'
              }
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false, // 弹框
      itemSn: '' //  当前sn
    }
  },
  methods: {
    //  编辑
    handleEdit (row) {
      this.itemSn = row.promoRuleSn
      this.openModal = true
    },
    handleOk (data) {
      this.$refs.table.refresh(true)
    },
    //  详情
    handleDetail (row) {
      this.itemId = row.id
      this.openModal = true
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          promoRuleDel({ sn: row.promoRuleSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 关闭弹框
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/promotionRulesManagement/promotionRules/list', query: { closeLastOldTab: true } })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      this.tableHeight = window.innerHeight - 250
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.$refs.table.refresh(true)
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.pageInit()
      this.$refs.table.refresh(true)
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less" scoped>
  .promotionRulesList-rule-wrap{
    .promotionRulesList-rule-back{
      margin-bottom: 10px;
    }
  }
</style>
