<template>
  <div class="payreturnPage">
    <!-- 搜索 -->
    <div class="formSearch">
      <!-- 搜索条件 -->
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input v-model="query.search" maxlength="20" placeholder="请输入活动名称" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" plain @click="search">查询</el-button>
          <el-button plain @click="clear">重置</el-button>
          <el-button type="primary" plain @click="$refs.freeQueuingModalRef && $refs.freeQueuingModalRef.show()">发起排队免单</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table ref="multipleTable" :data="tableData" border :header-cell-style="{ background: '#EEF3FF', color: '#333333' }" tooltip-effect="dark" style="width: 100%">
        <el-table-column align="center" type="index" label="#" width="55"></el-table-column>
        <el-table-column prop="activitiesName" label="活动名称" align="center" width="220" />
        <el-table-column prop="productImage" label="参与商家ID" align="center">
          <template slot-scope="scope">
            <span v-if="!scope.row.shopIds">---</span>
            <el-tag style="margin: 3px" v-for="(item, index) in scope.row.shopIds.split(',')" size="mini" :key="index">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="productPrice" label="发起类型" align="center" width="120">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.isPlatformInit === 1">平台发起</el-tag>
            <el-tag type="success" size="mini" v-else>商家发起</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="shopExemptionRatio" label="免单比例" align="center" width="120">
          <template slot-scope="scope">
            <el-tag type="success" size="mini">{{ scope.row.shopExemptionRatio || scope.row.shopExemptionRatio === 0 ? (scope.row.shopExemptionRatio * 100).toFixed(2) + '%' : '--' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="280" label="操作" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="btnList">
              <el-button type="text" @click="$refs.freeQueuingModalRef && $refs.freeQueuingModalRef.show(scope.row)">编辑</el-button>
              <el-button type="text" @click="$refs.detailModalRef && $refs.detailModalRef.show(scope.row.id)">详情</el-button>
              <el-button type="text" style="color: #f56c6c" @click="handleDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="query.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <FreeQueuingModal @refresh="search" ref="freeQueuingModalRef"></FreeQueuingModal>
    <DetailModal ref="detailModalRef"></DetailModal>
  </div>
</template>

<script>
import { getFreeQueuingListApi, deleteFreeQueuingDeatilApi } from '@/api/freeQueuing'
import FreeQueuingModal from './components/FreeQueuingModal'
import DetailModal from './components/DetailModal'

export default {
  components: {
    FreeQueuingModal,
    DetailModal
  },
  data() {
    return {
      query: { page: 1, pageSize: 10, search: undefined },
      total: 1,
      tableData: []
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    async getAll() {
      const res = await getFreeQueuingListApi(this.query)
      this.tableData = res.data.list
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.query.pageSize = val
      this.getAll()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getAll()
    },
    search() {
      this.total = 1
      this.query.page = 1
      this.getAll()
    },
    clear() {
      this.query = {
        page: 1,
        pageSize: 10,
        search: ''
      }
      this.getAll()
    },

    // 删除
    async handleDelete(row) {
      this.$confirm(`是否删除【${row.activitiesName}】这项排队免单?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteFreeQueuingDeatilApi({ id: row.id })
          if (res.code == '200') {
            this.$message.success('删除成功')
            this.getAll()
          }
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.payreturnPage {
  padding: 30px;

  .tableBox {
    .fenye {
      margin: 20px;
    }
  }
}
</style>
