<template>
  <section class="dm-wrap" v-loading="recordLoading">
    <div class="clearfix pb22">
      <el-input v-model="listParams.search" class="w280" placeholder="请输入兑换码名称/姓名/手机号" clearable @change="refresh"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-date-picker class="w300" v-model="dateTime" type="daterange" range-separator="至" start-placeholder="兑换开始日期" end-placeholder="兑换结束日期" @change="refresh"></el-date-picker>
      <el-select class="dm-select" v-model="listParams.sendStatus" placeholder="兑换码类型" @change="refresh">
        <el-option v-for="item in sendStatusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-select class="dm-select" v-model="listParams.sendStatus" placeholder="兑换码状态" @change="refresh">
        <el-option v-for="item in sendStatusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <div class="fr">
        <el-button type="primary">兑换链接</el-button>
        <el-button type="primary" @click="$router.push('/cdKey/add')">新建兑换码</el-button>
      </div>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%" row-key="sendTime">
      <el-table-column min-width="140" align="left" prop="receiveTime" label="兑换码名称" fixed="left" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" prop="createTime" label="有效期">
        <template slot-scope="scope">
          <div>{{ formatDateTimeByType(scope.row.createTime, 'yyyy-MM-dd-HH-mm') }}-</div>
          <div>{{ formatDateTimeByType(scope.row.createTime, 'yyyy-MM-dd-HH-mm') }}</div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" label="兑换码类型">
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? '专属码' : '兑换码' }}
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="left" prop="orderNumber" label="兑换码数量"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="orderNumber" label="兑换内容"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" prop="createTime" label="兑换码状态">
        <template slot-scope="scope">
          <div class="dm-status--info">????</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="160px" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="$router.push('/cdKey/edit/' + scope.row.smsId)">编辑</el-button>
          <el-button type="text" @click="$router.push('/cdKey/info/' + scope.row.smsId)">详情</el-button>
          <dm-delete @confirm="invalidData(scope.row)" tips="是否禁用该条数据?">
            <el-button type="text">禁用</el-button>
          </dm-delete>
          <dm-delete @confirm="invalidData(scope.row)" tips="是否删除该条数据?">
            <el-button type="text">删除</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="listParams.currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="listParams.pageSize" layout="total, sizes, prev, pager, next" :total="total"></dm-pagination>
  </section>
</template>
<script>
import { LoadMessageList, invalidsmsRecord } from '@/service/api/messageApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
import timeCounts from '@/components/timeCount/index.vue';
import tableMethods from '@/mixins/tableMethods.js';
export default {
  name: 'sms-record',
  components: {
    timeCounts
  },
  mixins: [tableMethods],
  data() {
    return {
      sendStatusOptions: [{ value: '', label: '所有状态', type: '' }, { value: 0, label: '定时执行', type: 'warning' }, { value: 1, label: '执行完毕', type: 'success' }, { value: 2, label: '执行失败', type: 'danger' }, { value: 3, label: '无可用模板', type: 'danger' }, { value: 5, label: '执行中', type: 'primary--flash' }, { value: -1, label: '已失效', type: 'danger' }], // eslint-disable-line
      recordLoading: false,
      tableList: [],
      listParams: {
        sendStatus: '',
        sendBeginTime: '',
        sendEndTime: '',
        search: '',
        currentPage: 1,
        pageSize: 20,
        showSelfFlag: ''
      },
      dateTime: ['', ''],
      total: 0
    };
  },
  created() {
    this.$store.commit('mutations_breadcrumb', [{ name: '营销管理', path: '' }, { name: '兑换码', path: '' }, { name: '兑换码列表', path: '' }]); // eslint-disable-line
    this.getTableList();
  },
  methods: {
    formatDateTimeByType,
    async getTableList() {
      try {
        this.recordLoading = true;
        if (this.dateTime) {
          this.listParams.sendBeginTime = formatDateTimeByType(this.dateTime[0], 'yyyy-MM-dd');
          this.listParams.sendEndTime = formatDateTimeByType(this.dateTime[1], 'yyyy-MM-dd');
        } else {
          this.listParams.sendBeginTime = this.listParams.sendEndTime = '';
        }
        let res = await LoadMessageList(this.listParams);
        if (res.errorCode === 0 && res.result) {
          this.tableList = res.result.result || [];
          this.total = res.result.totalCount;
        } else {
          this.tableList = [];
          this.total = 0;
        }
        this.recordLoading = false;
      } catch (err) {
        this.recordLoading = false;
      }
    },
    invalidData(row) {
      invalidsmsRecord({ smsId: row.smsId })
        .then(res => {
          this.$tips({ type: 'success', message: '禁用成功!' });
          this.getTableList();
        })
        .catch(err => {
          this.$tips({ type: 'error', message: '禁用失败!' });
        });
    },
    filterStatus(val) {
      let result = { label: '执行错误', type: 'warning' };
      this.sendStatusOptions.forEach(v => {
        if (val === v.value) result = v;
      });
      return result;
    }
  }
};
</script>
