<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="clearfix pb22">
      <el-input v-model="listParams.cdkeyName" 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" clearable v-model="listParams.cdkeyType" placeholder="兑换码类型" @change="refresh">
        <el-option label="专属码" :value="1"></el-option>
        <el-option label="通用码" :value="2"></el-option>
      </el-select>
      <el-select class="dm-select" clearable v-model="listParams.cdkeyPlanStatus" placeholder="兑换码状态" @change="refresh">
        <el-option v-for="item in status" :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%">
      <el-table-column min-width="140" align="left" prop="cdkeyName" label="兑换码名称" fixed="left" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="150" align="left" prop="createTime" label="有效期">
        <template slot-scope="scope">
          <div>{{ formatDateTimeByType(scope.row.cdkeyEffectTime, 'yyyy-MM-dd HH:mm:ss') }}~</div>
          <div>{{ formatDateTimeByType(scope.row.cdkeyLimitTime, 'yyyy-MM-dd HH:mm:ss') }}</div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" label="兑换码类型">
        <template slot-scope="scope">
          <!-- 兑换码类型（1：专属，2：通用） -->
          {{ scope.row.cdkeyType == 1 ? '专属码' : '通用码' }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="cdkeyContentDescribe" label="兑换码内容">
        <template slot-scope="scope">
          <el-button v-if="scope.row.cdkeyType == 1" type="text">导出兑换码列表</el-button>
          <span v-else>{{ scope.row.cdkeyValue }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="left" prop="cdkeyCount" label="兑换码数量"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="cdkeyContentDescribe" label="兑换内容"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" prop="createTime" label="兑换码状态">
        <template slot-scope="scope">
          <!-- 兑换码计划状态（1：未生效，2：生效中，3：已过期，4：已失效） -->
          <div v-if="scope.row.cdkeyPlanStatus == 1" class="dm-status--warning">未生效</div>
          <div v-if="scope.row.cdkeyPlanStatus == 2" class="dm-status--primary--flash">生效中</div>
          <div v-if="scope.row.cdkeyPlanStatus == 3" class="dm-status--info">已过期</div>
          <div v-if="scope.row.cdkeyPlanStatus == 4" 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" v-if="scope.row.cdkeyPlanStatus == 1" @click="$router.push('/cdKey/edit/' + scope.row.cdkeyPlanId)">编辑</el-button>
          <el-button type="text" v-if="scope.row.cdkeyPlanStatus != 1" @click="$router.push('/cdKey/info/' + scope.row.cdkeyPlanId)">详情</el-button>
          <dm-delete v-if="scope.row.cdkeyPlanStatus == 2" @confirm="invalidData(scope.row)" tips="是否禁用该条数据?">
            <el-button type="text">禁用</el-button>
          </dm-delete>
          <dm-delete v-if="scope.row.cdkeyPlanStatus == 1" @confirm="delData(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 { cdkeyPlanRecord, cdkeyPlanForbid, cdkeyPlanDelete } from '@/service/api/cdKeyApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
import timeCounts from '@/components/timeCount/index.vue';
import tableMethods from '@/mixins/tableMethods.js';
export default {
  name: 'cdkey-list',
  components: {
    timeCounts
  },
  mixins: [tableMethods],
  data() {
    return {
      status: [{ value: 1, label: '未生效' }, { value: 2, label: '生效中' }, { value: 4, label: '已失效' }], // eslint-disable-line
      loading: false,
      tableList: [],
      listParams: {
        cdkeyPlanStatus: '',
        cdkeyType: '',
        cdkeyEffectTime: '',
        cdkeyLimitTime: '',
        cdkeyName: '',
        currentPage: 1,
        pageSize: 20
      },
      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.loading = true;
        if (this.dateTime) {
          this.listParams.cdkeyEffectTime = formatDateTimeByType(this.dateTime[0], 'yyyy-MM-dd');
          this.listParams.cdkeyLimitTime = formatDateTimeByType(this.dateTime[1], 'yyyy-MM-dd');
        } else {
          this.listParams.cdkeyEffectTime = this.listParams.cdkeyLimitTime = '';
        }
        let res = await cdkeyPlanRecord(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.loading = false;
      } catch (err) {
        this.loading = false;
      }
    },
    // 删除
    delData({ cdkeyPlanId }) {
      this.loading = true;
      cdkeyPlanDelete({ cdkeyPlanId })
        .then(res => {
          this.$tips({ type: 'success', message: '删除成功!' });
          this.getTableList();
        })
        .catch(err => {
          this.$tips({ type: 'error', message: '删除失败!' });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 禁用
    invalidData({ cdkeyPlanId }) {
      this.loading = true;
      cdkeyPlanForbid({ cdkeyPlanId })
        .then(res => {
          this.$tips({ type: 'success', message: '禁用成功!' });
          this.getTableList();
        })
        .catch(err => {
          this.$tips({ type: 'error', message: '禁用失败!' });
        })
        .finally(() => {
          this.loading = false;
        });
    }
  }
};
</script>
