<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="flex pb20">
      <div>
        <el-select v-model="pageParams.state" placeholder="计划状态" class="w200" clearable @change="refresh">
          <el-option :value="0" label="未开始"></el-option>
          <el-option :value="1" label="进行中"></el-option>
          <el-option :value="2" label="已结束"></el-option>
        </el-select>
        <el-input v-model="pageParams.storeSignName" class="w300" placeholder="计划名称" clearable @change="refresh"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      </div>
      <el-button type="primary" v-if="$getButtonLimit($buttonCode.marketingAddSign)" :limit-code="$buttonCode.marketingAddSign" @click="goAddPlan">新建计划</el-button>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%">
      <el-table-column :show-overflow-tooltip="true" align="left" prop="storeSignName" label="计划名称"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="left" prop="updateTime" label="最近编辑时间">
        <template slot-scope="scope">
          <p class="cell-time">
            {{ formatDateTimeByType(scope.row.updateTime, 'yyyy-MM-dd-HH-mm', true).y }}<br />
            {{ formatDateTimeByType(scope.row.updateTime, 'yyyy-MM-dd-HH-mm', true).h }}
          </p>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="left" label="签到人群">
        <template slot-scope="scope"> {{ scope.row.memberSearchType | memberSearchTypeFilter }}</template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="left" prop="state" label="计划状态">
        <template slot-scope="scope"> {{ scope.row.state | stateFilter }}</template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="210px" fixed="right">
        <template slot-scope="scope">
          <!-- 只有未开始的计划可编辑 -->
          <el-button type="text" v-if="$getButtonLimit($buttonCode.marketingEditSign) && scope.row.state === 0" :limit-code="$buttonCode.marketingEditSign" @click="$router.push(`/sign/setting/edit?id=${scope.row.storeSignId}&canEdit=1`)">编辑</el-button>
          <el-button type="text" v-else @click="$router.push(`/sign/setting/check?id=${scope.row.storeSignId}`)">查看</el-button>
          <dm-delete v-if="$getButtonLimit($buttonCode.marketingDelSign)" @confirm="delPlan(scope.row)" tips="是否删除该签到计划？">
            <el-button type="text" :limit-code="$buttonCode.marketingDelSign">删除</el-button>
          </dm-delete>
        </template>
      </el-table-column>
    </el-table>
    <dm-pagination v-show="tableList.length" background class="dm-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next" :total="total"></dm-pagination>
  </section>
</template>

<script>
/* eslint-disable */
import { getPageSignPlan, delSignPlan } from '@/service/api/storeSign.js';
import { formatDateTimeByType } from '@/utils/index.js';
export default {
  name: 'sign-record',
  data() {
    return {
      loading: false,
      projectName: 'gic-web',
      pageParams: {
        currentPage: 1,
        pageSize: 20,
        state: null
      },
      total: 0,
      tableList: [],
      // 门店单选
      storeName: '',
      storeDialog: false,
      formatDateTimeByType
    };
  },
  filters: {
    memberSearchTypeFilter(v) {
      switch (v) {
        case 0:
          return '全部会员';
          break;
        case 1:
          return '自定义人群';
          break;
        case 2:
          return '会员分组';
          break;
        default:
          return '';
          break;
      }
    },
    stateFilter(v) {
      switch (v) {
        case 0:
          return '未开始';
          break;
        case 1:
          return '进行中';
          break;
        case 2:
          return '已结束';
          break;
        default:
          return '';
          break;
      }
    }
  },
  created() {
    this.$store.commit('mutations_breadcrumb', [{ name: '门店签到' }, { name: '计划列表', path: '' }]);
    this.fetch();
  },
  methods: {
    async fetch() {
      try {
        this.loading = true;
        const params = { ...this.pageParams };
        let res = await getPageSignPlan(params);
        if (res.errorCode === 0 && res.result) {
          this.tableList = Array.isArray(res.result.result) ? res.result.result : [];
          this.total = res.result.totalCount;
        } else {
          this.tableList = [];
          this.total = 0;
        }
        this.loading = false;
      } catch (err) {
        this.loading = false;
      }
    },
    refresh() {
      this.pageParams.currentPage = '1';
      this.fetch();
    },
    goAddPlan() {
      this.$router.push('/sign/setting');
    },
    async delPlan(item) {
      try {
        this.loading = true;
        const params = { storeSignId: item.storeSignId };
        let res = await delSignPlan(params);
        if (res.errorCode === 0) {
          this.$message.success('删除成功');
          this.refresh();
        } else {
          this.$message.error('删除失败');
        }
        this.loading = false;
      } catch (err) {
        this.loading = false;
      }
    },
    handleSizeChange(val) {
      this.pageParams.pageSize = val;
      this.fetch();
    },
    handleCurrentChange(val) {
      this.pageParams.currentPage = val;
      this.fetch();
    }
  }
};
</script>
<style lang="scss" scoped>
.memberInfo {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  img {
    display: block;
    width: 40px;
    height: 40px;
    margin-right: 10px;
    border-radius: 5px;
  }
  .cell-memberInfo {
    color: #606266;
    line-height: 20px;
    font-size: 14px;
  }
}
.flex {
  justify-content: space-between;
  width: 100%;
}
</style>
