<template>
  <section class="dm-wrap">
    <div class="pb22 clearfix">
      <el-select class="w150" v-model="listParams.cardStatus" placeholder="选择卡券状态" @change="search">
        <el-option v-for="(item,index) in cardStatusOptions" :key="index" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-select class="w150" v-model="listParams.cardType" placeholder="选择卡券类型" @change="search">
        <el-option v-for="(item,index) in cardTypeOptions" :key="index" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-select class="w150" v-model="listParams.upperShelfStatus" placeholder="选择上架状态" @change="search">
        <el-option v-for="(item,index) in upperShelfStatusOptions" :key="index" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-input v-model="listParams.searchParam" class="w200 vertical-middle" placeholder="输入名称/备注" clearable @change="loadGicCardList"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-button class="fr" type="primary" @click="$router.push('/card/add')">新增卡券</el-button>
    </div>
    <ul class="clearfix" v-loading="loading" 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" type="text" @click="$router.push('/card/add')">新增卡券</el-button>
    </div>
    <adjust-stock :show.sync="adjustStock" :totalCount="currentCard.couponStock" :coupCardId="currentCard.coupCardId" @refresh="delayRefresh"></adjust-stock>
    <el-pagination v-show="tableList.length" background class="dm-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listParams.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="listParams.pageSize" layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
  </section>
</template>
<script>
import {loadGicCardList,deleteCard} from '@/service/api/giccardApi.js';
import adjustStock from './adjust-Stock';
import cardItem from './card-item';
export default {
    name: 'card-record',
    data () {
      return {
        listParams:{cardType:-1,cardStatus:-1,upperShelfStatus:-1,searchParam:'',currentPage:1,pageSize:20},
        cardTypeOptions:[{label:'全部',value:-1},{label:'抵金券',value:0},{label:'折扣券',value:1},{label:'兑换券',value:2}],
        cardStatusOptions:[{label:'全部',value:-1},{label:'正常',value:1},{label:'过期',value:2}],
        upperShelfStatusOptions:[{label:'全部',value:-1},{label:'已下架',value:0},{label:'待上架',value:1},{label:'已上架',value:2}],
        total:0,
        loading:false,
        tableList:[],
        sortFlag:false,
        adjustStock:false,
        currentCard:{}
      }
    },
    components:{
      'adjust-stock':adjustStock,
      'card-item':cardItem
    },
    created() {
      this.loadGicCardList();
      this.$store.commit('mutations_breadcrumb',[{name:'营销管理',path:''},{name:'卡券营销',path:'/card'},{name:'卡券库',path:''}])
    },
    methods: {
        //编辑库存 前置
        preAdjustStock(val) {
          this.currentCard = val;
          this.adjustStock = true;
        },
        //修改库存后台异步  所以这边延迟刷新列表
        delayRefresh(val) {
          this.currentCard.couponStock = val;
        },
        handleSizeChange(val) {
            this.listParams.pageSize = val;
            this.loadGicCardList();
        },
        handleCurrentChange(val) {
            this.listParams.currentPage = val;
            this.loadGicCardList();
        },
        search() {
          this.listParams.currentPage = 1;
          this.loadGicCardList();
        },
        async loadGicCardList() {
            this.loading = true;
            // try {
              let res = await loadGicCardList(this.listParams);
              this.tableList = res.result.result;
              this.total = res.result.totalCount;
            // } catch (err) {
              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: '已取消删除'});
          });
        },
        async deleteCard(coupCardId){
          let res = await deleteCard({coupCardId});
          this.$tips({type: 'success',message: '删除成功!'});
          this.loadGicCardList();
        }
    },
}
</script>
