<template>
  <a-card size="small" :bordered="false">
    <a-spin :spinning="spinning" tip="Loading...">
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="活动名称">
                <a-input id="redPacket-shelfName" allowClear v-model.trim="queryParam.ruleName" placeholder="请输入活动名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="活动状态">
                <v-select id="redPacket-state" code="REWARD_RULE_STATUS" v-model="queryParam.ruleStatus" allowClear placeholder="请选择状态"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" id="redPacket-search">查询</a-button>
              <a-button type="" @click="reset" id="redPacket-reset" style="margin-left: 10px;">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" class="button-error" @click="handleEdit()" id="redPacket-handleEdit">新增红包活动</a-button>
      </div>
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px', wordBreak: 'break-all' }"
        size="small"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <template slot="shelf" slot-scope="text, record">
          共 <a title="查看参与货架" @click="viewShelf(record)">{{ text }}</a> 个货架
        </template>
        <template slot="action" slot-scope="text, record">
          <a-button v-if="record.ruleStatus == 'wait'" size="small" type="link" class="button-info" @click="handlePublish(record)">发布</a-button>
          <a-button v-if="record.ruleStatus == 'release'" size="small" type="link" class="button-info" @click="handleEnd(record)">终止</a-button>
          <a-button v-if="record.ruleStatus == 'wait'||record.ruleStatus == 'release'" size="small" type="link" class="button-info" @click="handleEdit(record)">编辑</a-button>
          <a-button v-if="record.ruleStatus == 'wait'" size="small" type="link" class="button-error" @click="handleDelete(record)">删除</a-button>
          <a-button size="small" type="link" class="button-info" @click="handleDetail(record)">详情</a-button>
        </template>
      </s-table>
    </a-spin>
    <shelfDetail ref="shelfDetail" :itemData="itemData" :visible="showModal" @close="closeModal"></shelfDetail>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { rewardRuleList, rewardRuleRelease, rewardRuleDelete, rewardRuleClose } from '@/api/redPacket.js'
import dealerList from '@/views/common/dealerList.vue'
import storeList from '@/views/common/storeList.vue'
import shelfDetail from './shelfDetail.vue'
export default {
  name: 'UserList',
  components: { STable, VSelect, storeList, dealerList, shelfDetail },
  data () {
    return {
      spinning: false,
      advanced: false,
      tableHeight: 0,
      // 查询参数
      queryParam: {
        ruleName: '', // 活动名称
        ruleStatus: undefined // 状态
      },
      showModal: false,
      itemData: null, // 编辑行数据
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '活动名称', dataIndex: 'ruleName', width: '17%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '活动发布时间', dataIndex: 'releaseDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '活动截止时间', dataIndex: 'expireDate', width: '10%', align: 'center', customRender: function (text, record) { return record.neverExpireFlag == 1 ? '永久' : text } },
        { title: '参与货架', dataIndex: 'joinNum', width: '8%', align: 'center', scopedSlots: { customRender: 'shelf' } },
        { title: '活动范围', dataIndex: 'bizTypes', width: '8%', align: 'center', customRender: function (text) { return '货架订单' } },
        { title: '活动状态', dataIndex: 'ruleStatusDictValue', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '备注', dataIndex: 'remarks', width: '12%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '创建时间', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', width: '12%', align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return rewardRuleList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 重置
    reset () {
      this.queryParam = {
        ruleName: '', // 活动名称
        ruleStatus: undefined // 状态
      }
      this.$refs.table.refresh(true)
    },
    // 编辑/新增
    handleEdit (row) {
      const _this = this
      if (row) { //  编辑
        this.$confirm({
          title: '提示',
          content: '确认要编辑吗？',
          centered: true,
          onOk () {
            _this.$router.push({ name: 'redPacketEdit', params: { sn: row.rewardRuleSn } })
          }
        })
      } else {
        this.$router.push({ name: 'redPacketAdd' })
      }
    },
    // 详情
    handleDetail (row) {
      this.$router.push({ name: 'redPacketDetail', params: { sn: row.rewardRuleSn } })
    },
    // 查看参与货架详细
    viewShelf (row) {
      this.showModal = true
      this.itemData = row
    },
    closeModal () {
      this.showModal = false
      this.itemData = null
    },
    // 发布
    handlePublish (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要发布吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          rewardRuleRelease({ rewardRuleSn: row.rewardRuleSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 终止
    handleEnd (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要终止吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          rewardRuleClose({ rewardRuleSn: row.rewardRuleSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 删除
    handleDelete (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          rewardRuleDelete({ rewardRuleSn: row.rewardRuleSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    pageInit () {
      const _this = this
      _this.$refs.table.refresh()
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 215
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    this.pageInit()
    this.reset()
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (from.name == 'redPacketEdit' || from.name == 'redPacketAdd') {
        vm.$refs.table.refresh()
      } else {
        vm.reset()
      }
    })
  }
}
</script>
