<template>
  <div class="promotionRulesList-rule-wrap">
    <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-wrap">
      <!-- 操作按钮 -->
      <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="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :scroll="{ y: tableHeight }"
        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>
      <!-- 新增/编辑 -->
      <edit-rule-modal :openModal="openModal" :itemSn="itemSn" @ok="handleOk" @close="closeModal" />
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import editRuleModal from './editRuleModal.vue'
import { promoRuleQueryList, promoRuleDel } from '@/api/promoRule'
export default {
  components: { STable, VSelect, editRuleModal },
  data () {
    return {
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center' },
        { title: '促销类型', dataIndex: 'promoRuleTypeDictValue', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销规则', dataIndex: 'promoRule', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 502
        }
        return promoRuleQueryList({ promoActiveSn: this.$route.params.sn }).then(res => {
          const 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].orderRuleType == 'PROD_QTY') {
                data[i].promoRule = '购买满' + data[i].orderAmount + '元正价产品，送' + data[i].promoAmount + '元采购额可用于购买促销品'
              } else if (data[i].orderRuleType == 'ORDER_AMOUNT') {
                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
          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 () {
          promoRuleDel({ sn: row.promoRuleSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    // 关闭弹框
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/promotionRulesManagement/promotionRules/list' })
    }
  }
}
</script>
<style lang="less" scoped>
  .promotionRulesList-rule-wrap{
    .promotionRulesList-rule-back{
      margin-bottom: 15px;
    }
  }
</style>
