<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="game-ptyx-header pb10 clearfix">
      <span class="pr10">卡券共{{ total }}张</span>
      <el-button-group class="vertical-middle pr4">
        <el-button :disabled="!ewashDisable" @click="sortList('col1')">创建时间<i class="el-icon--right" :class="listParams.sortBy === 'col1' ? 'el-icon-caret-top' : listParams.sortBy === 'col1 desc' ? 'el-icon-caret-bottom' : 'el-icon-d-caret'"></i></el-button>
        <el-button :disabled="!ewashDisable" @click="sortList('col2')">领取量<i class="el-icon--right" :class="listParams.sortBy === 'col2' ? 'el-icon-caret-top' : listParams.sortBy === 'col2 desc' ? 'el-icon-caret-bottom' : 'el-icon-d-caret'"></i></el-button>
        <el-button :disabled="!ewashDisable" @click="sortList('col3')">使用量<i class="el-icon--right" :class="listParams.sortBy === 'col3' ? 'el-icon-caret-top' : listParams.sortBy === 'col3 desc' ? 'el-icon-caret-bottom' : 'el-icon-d-caret'"></i></el-button>
        <el-button :disabled="!ewashDisable" @click="sortList('col4')">核销率<i class="el-icon--right" :class="listParams.sortBy === 'col4' ? 'el-icon-caret-top' : listParams.sortBy === 'col4 desc' ? 'el-icon-caret-bottom' : 'el-icon-d-caret'"></i></el-button>
      </el-button-group>
      <el-select :disabled="!ewashDisable" class="dm-select" v-model="listParams.giftCouponDiscountType" placeholder="选择卡券类型" @change="refresh">
        <el-option v-for="(item, index) in cardTypeOptions" :key="index" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-input :disabled="!ewashDisable" v-model="listParams.searchParam" class="w200 vertical-middle" placeholder="输入卡券名称/备注" clearable @change="refresh"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <div class="fr">
        <el-button type="primary" @click="addCard" :disabled="!ewashDisable">新增卡券</el-button>
      </div>
    </div>
    <ul class="clearfix" element-loading-text="拼命加载中">
      <card-item @adjust-stock="preAdjustStock" :item="v" v-for="(v, i) in tableList" :key="i" @delete-card="delData"></card-item>
    </ul>
    <div class="text-center" v-if="tableList.length === 0">
      <img class="block block-center pt100" width="60" height="60" src="../../assets/img/no-data_icon.png" alt="" />
      <el-button class="block block-center mt10 pb20" :disabled="!ewashDisable" type="text" @click="addCard">新增卡券</el-button>
    </div>
    <adjust-stock :show.sync="adjustStock" :totalCount="currentCard.couponStock" :coupCardId="currentCard.coupCardId" @refresh="delayRefresh"></adjust-stock>
    <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>
    <!-- 未创建e袋洗服务 -->
    <unable-tips :ewashTipDialog="ewashTipDialog" />
  </section>
</template>
<script>
import { appletUnableAlert } from '@/utils/index.js';
import { cardPageList, deleteCard } from '@/service/api/cardApi.js';
import adjustStock from '@/views/card/partials/adjust-Stock';
import cardItem from './partials/card-item';
import tableMethods from '@/mixins/tableMethods.js';

import unableTips from './partials/unable-tips';
export default {
  name: 'card-list',
  mixins: [tableMethods],
  data() {
    return {
      listParams: {
        shelfId: '',
        sortBy: '',
        searchParam: '',
        currentPage: 1,
        pageSize: 20,
        cardType: '',
        gicCouponType: 1,
        giftCouponDiscountType: ''
      },
      total: 0,
      loading: false,
      tableList: [],
      cardTypeOptions: [{ value: '', label: '全部卡券类型' }, { value: 1, label: '抵金券' }, { value: 2, label: '免单券' }], // eslint-disable-line
      sortFlag: false,
      adjustStock: false,
      currentCard: {},
      dialogVisible: false,
      ewashTipDialog: false,
      ewashDisable: false // e袋洗服务能否使用
    };
  },
  components: {
    'adjust-stock': adjustStock,
    'card-item': cardItem,
    unableTips
  },
  created() {
    window.scrollTo(0, 0);
    this.getTableList();
    this.$store.commit('mutations_breadcrumb', [{ name: 'e袋洗服务', path: '' }, { name: '服务列表', path: '' }]); // eslint-disable-line
  },
  methods: {
    //编辑库存 前置
    preAdjustStock(val) {
      if (!this.$store.state.marketing.appletEnable) {
        appletUnableAlert();
      } else {
        this.currentCard = val;
        this.adjustStock = true;
      }
    },
    //修改库存后台异步  所以这边延迟刷新列表
    delayRefresh(val) {
      this.currentCard.couponStock = val;
    },
    sortList(val) {
      this.sortFlag = !this.sortFlag;
      this.listParams.sortBy = this.sortFlag ? val + ' desc' : val;
      this.refresh();
    },
    async getTableList() {
      this.loading = true;
      try {
        let res = await cardPageList(this.listParams);
        this.tableList = res.result.result;
        this.total = res.result.totalCount;
        this.ewashDisable = true;
      } catch (err) {
        if (err.data && err.data.errorCode === 170049) {
          // 未开通e袋洗服务
          this.ewashTipDialog = true;
          this.ewashDisable = false;
        }
        // this.$tips({ type: 'error', message: '加载列表失败' });
      }
      this.loading = false;
    },
    delData(val) {
      this.$confirm('是否删除该卡券?', '提示', {
        confirmButtonText: '确定',
        cancelBUttonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteCard(val);
        })
        .catch(() => {
          this.$tips({ type: 'info', message: '已取消删除' });
        });
    },
    addCard() {
      if (!this.$store.state.marketing.appletEnable) {
        appletUnableAlert();
      } else {
        this.$router.push('/ewash/add');
      }
    },
    async deleteCard(coupCardId) {
      await deleteCard({ coupCardId });
      this.$tips({ type: 'success', message: '删除成功!' });
      if (this.tableList.length === 1 && this.listParams.currentPage !== 1) {
        this.listParams.currentPage--;
      }
      this.getTableList();
    }
  }
};
</script>
