<template>
  <template-page ref="pageRef" :get-list="getList" :table-attributes="tableAttributes" :table-events="tableEvents"
    :options-evens-group="optionsEvensGroup" :moreParameters="moreParameters" :column-parsing="columnParsing"
    :operation="operation" :exportList="exportList" :operationColumnWidth="210">
    <div slot="moreSearch">
      <el-radio-group v-model="type" size="mini" @change="changeType">
        <el-radio-button label="">全部({{ statistics.all || 0 }})</el-radio-button>
        <el-radio-button :label="true">启用({{ statistics.jxz || 0 }})</el-radio-button>
        <el-radio-button :label="false">禁用({{ statistics.yjs || 0 }})</el-radio-button>
      </el-radio-group>
      <br><br>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import { promotionGroupListPageV2, promotionGroupPageExport, getActivityCount, changeStatus } from '@/api/groupbuy'
import { downloadFiles } from '@/utils/util'
export default {
  components: { TemplatePage },
  mixins: [import_mixin],
  data() {
    return {
      type: "",
      statistics: {},
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '添加团购活动',
              isRole: true,
              click: () => {
                this.$router.push({
                  name: "groupbuy_add",
                  query: {}
                })
              }
            }
          ],
        ],
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      // 勾选选中行
      recordSelected: [],
    }
  },
  computed: {
    // 更多参数
    moreParameters() {
      return []
    },
  },
  methods: {
    // 切换状态
    changeType(val) {
      this.$refs.pageRef.refreshList()
    },
    // 列表请求函数
    getList(p) {
      try {
        var pam = JSON.parse(JSON.stringify(p))
        if (this.type !== '') {
          pam.status = this.type
          pam.params.push({ "param": "a.status", "compare": "=", "value": this.type })
        }
        return promotionGroupListPageV2(pam)
      } catch (error) {
        console.log(error)
      } finally {
        getActivityCount().then(res => {
          this.statistics = res.data
        })
      }
    },
    // 列表导出函数
    exportList: promotionGroupPageExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    // 表格操作列
    operation(h, { row, index, column }) {
      return (
        <div class='operation-btns'>
          <el-popconfirm
            title={`是否确定${row.status ? "关闭" : "开启"}？`}
            onConfirm={() => {
              changeStatus({ promotionGroupId: row.promotionGroupId, status: !row.status }).then(res => {
                this.$message({ type: 'success', message: `${row.status ? "关闭" : "开启"}成功!` })
                this.$refs.pageRef.refreshList()
              })
            }}
          >
            <el-button type="text" slot="reference">{row.status ? "关闭" : "开启"}</el-button>
          </el-popconfirm>
          <el-button type="text" onClick={() => {
            this.$router.push({
              name: "groupbuy_add",
              query: {
                id: row.promotionGroupId
              }
            })
          }}>编辑</el-button>
          <el-button type="text" onClick={() => {
            this.$router.push({
              name: "groupbuy_detail",
              query: {
                id: row.promotionGroupId
              }
            })
          }}>拼团详情</el-button>
          <el-button type="text" onClick={() => {
            let screenData = {
              promotionGroupId: row.promotionGroupId
            };
            downloadFiles('order/export', screenData);
          }}>导出订单</el-button>
        </div>
      )
    },
  }
}
</script>

<style lang="scss" scoped></style>
