<template>
<section class="sms-lib">
  <div :class="pbSize">
    <span class="pr10">选择卡券(共{{total}}条)</span>
    <el-input :disabled="disabled" v-model="listParams.searchParam" class="w200" clearable placeholder="请输入卡券名称" @change="getCardList"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
    <span class="fz12 gray pl20">领取限制 < 100的卡券不支持选择，系统已自动过滤</span>
  </div>
  <el-table tooltipEffect="light" :data="tableList" :height="tableHeight" style="width: 100%" v-loading="loading" @row-click="chooseCard">
    <el-table-column  :show-overflow-tooltip="false" width="60" align="center" prop="coupCardId">
        <template slot-scope="scope">
          <div class="sms-record_left label-hidden">
              <el-radio :disabled="disabled" v-if="cardIdName === 'wechatCardId'" v-model="selectedId" :label="scope.row.wechatCardId" class="pr10"></el-radio>
              <el-radio :disabled="disabled" v-else v-model="selectedId" :label="scope.row.coupCardId" class="pr10"></el-radio>
          </div>
        </template>
    </el-table-column>
    <el-table-column  :show-overflow-tooltip="false" align="left"  prop="cardName" label="卡券名称"></el-table-column>
    <el-table-column  :show-overflow-tooltip="true"  align="left"  prop="subName" label="备注名"></el-table-column>
    <el-table-column  :show-overflow-tooltip="false" align="left"  prop="cardLimit" label="兑换限制"></el-table-column>
    <el-table-column  :show-overflow-tooltip="false" align="left"  prop="couponStock" label="库存"></el-table-column>
  </el-table>
  <el-pagination v-show="tableList.length && showPagination" 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, prev, pager, next" :total="total"></el-pagination>
</section>
</template>

<script>
import {getCardList} from '@/service/api/mallApi.js';
export default {
  props:{
    activeId:{
      type:String,
      default:''
    },
    pbSize:{
      type:String,
      default:'pb22'
    },
    cardIdName:{
      type:String,
      default:'coupCardId'
    },
    tableHeight:{
      type:String,
      default:'auto'
    },
    showPagination:{
      type:Boolean,
      default:true
    },
    disabled:{
      type:Boolean,
      default:false
    },
    cardType:{
      type:String,
      default:'2'
    }
  },
  data(){
    return{
      listParams:{
        searchParam:'',
        currentPage:1,
        pageSize:20,
        requestProject:'gic-web',
        coupCardId:'',
        cardType:this.cardType
      },
      total:0,
      tableList:[],
      selectedId:this.activeId
    }
  },
  watch: {
    selectedId(val) {
      let obj = {};
      if (this.cardIdName === 'wechatCardId') {
        this.tableList.map(v => {
          if (v.coupCardId === val) {
            val = v.wechatCardId
            obj = v;
          }
        })
      } else {
        this.tableList.map(v => {
          if (v.coupCardId === val) {
            obj = v;
          }
        })
      }

      this.$emit('update:activeId',val);
      this.$emit('emitActiveObj',obj);
    },
    activeId(val) {
      this.selectedId = val;
    }

  },
  created(){
    this.selectedId = this.activeId;
    this.getCardList();
  },
  methods:{
      handleSizeChange(val) {
          this.listParams.pageSize = val;
          this.getCardList();
      },
      handleCurrentChange(val) {
          this.listParams.currentPage = val;
          this.getCardList();
      },
      async getCardList() {
          this.loading = true;
          if (this.$route.meta.type === 'edit' ||  this.$route.meta.type === 'info') {
             this.listParams.coupCardId = this.activeId;
          }
          let res = await getCardList(this.listParams);
          this.tableList = res.result.rows || [];
          this.total = res.result.total;
          this.loading = false;
      },
      reset() {
        this.listParams.searchParams = '';
        this.getCardList();
      },
      chooseCard(row) {
        this.selectedId = row.coupCardId;
        $bus.$emit('card-temp-choose',row);
      }
  }
}
</script>
