<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-04-12 17:56:45
 * @LastEditors: 无尘
 * @LastEditTime: 2020-04-24 17:11:56
 -->
<template>
  <section class="common-right-wrap">
    <div class="p-20">
      <div class="flex flex-space-between m-b-20">
        <div>
          <el-select class="w-168 " v-model="conditionObj.templateId" placeholder="全部模板" @change="changeCondition">
            <el-option label="全部模板" value=""></el-option>
            <el-option v-for="item in templateData" :key="item.templateId" :label="item.templateName" :value="item.templateId"> </el-option> </el-select
          ><el-select class="w-168 m-l-10" v-model="conditionObj.policyId" placeholder="全部策略" @change="changeCondition">
            <el-option label="全部策略" value=""></el-option>
            <el-option v-for="item in strategyData" :key="item.policyId" :label="item.policyName" :value="item.policyId"> </el-option> </el-select
          ><el-input placeholder="请输入卡券名称" v-model="conditionObj.searchInput" class="w-264 m-l-10" style="width: 264px;" @keyup.native="value => toInput(value, conditionObj.searchInput)" clearable @clear="clearInput"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input>
        </div>
      </div>
      <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: calc(100% - 3px);">
        <el-table-column prop="" label="投放时间">
          <template slot-scope="scope">
            <div class="color-606266 line-18">{{ scope.row.sendTime | timeStampToYmd }}</div>
            <div class="color-606266 line-18">{{ scope.row.sendTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="卡券名称" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.cardName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="所属模板" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.templateName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="所属策略" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.policyName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="投放人数" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.cardName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="领取数量">
          <template slot-scope="scope">{{ scope.row.getedQuantity }}</template>
        </el-table-column>
        <el-table-column prop="" label="领取率">
          <template slot-scope="scope">{{ scope.row.getedRate | percenteNum }}</template>
        </el-table-column>
        <el-table-column prop="" label="使用数量">
          <template slot-scope="scope">
            <div>{{ scope.row.usageQuantity }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="核销率">
          <template slot-scope="scope">
            <div>{{ scope.row.verificationRate | percenteNum }}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="" label="操作" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="toDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
      </div>
    </div>
  </section>
</template>

<script>
import errMsg from '@/common/js/error';
import showMsg from '@/common/js/showmsg';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
export default {
  components: {},
  props: {
    brandId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      recordShow: false,
      wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',

      activeId: '2',
      conditionObj: {
        templateId: '',
        policyId: '',
        searchInput: ''
      },
      templateData: [],
      strategyData: [],
      tableData: [],
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0
    };
  },
  watch: {
    brandId(val) {
      if (!!val) {
        this.getTableList();
      }
    }
  },
  filters: {
    percenteNum(val) {
      return val > 0 ? Number(val * 100).toFixed(2) + '%' : Number(val).toFixed(2) + '%';
    }
  },
  methods: {
    /**
     * 删除
     */
    toDel(index, row) {
      const that = this;
      that
        .$confirm('是否要删除选中的卡券?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.delCardList(row.coupCardId);
        })
        .catch(() => {});
    },
    /**
     * 删除卡券
     */
    delCardList(coupCardId) {
      const that = this;
      let para = {
        gicEnterpriseId: that.brandId,
        coupCardId: coupCardId
      };
      postRequest('/haoban-app-customer-web/inner/del-card-coup', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.getTableList(that.brandId);
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    }, 500),
    // 搜索清除
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    },
    /**
     * 新建卡券
     */
    toNewCard() {
      const that = this;
      that.$router.push(`/newCard`);
    },
    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      let that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      let that = this;
      that.currentPage = val;
      that.getTableList();
    },
    /**
     * 获取模板
     */
    getTempList() {
      const that = this;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId,
        search: '' // 搜索字段
      };
      postRequest('/haoban-app-customer-web/inner/list-template', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.templateData = resData.result || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取策略
     */
    getStrategyList() {
      const that = this;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId,
        search: '' // 搜索字段
      };
      postRequest('/haoban-app-customer-web/inner/list-template', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.strategyData = resData.result || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 获取列表数据
     */
    async getTableList() {
      const that = this;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId,
        templateId: that.conditionObj.templateId || '', // 搜索字段
        policyId: that.conditionObj.policyId || '', // 搜索字段
        search: that.conditionObj.searchInput || '', // 搜索字段
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize // 一页显示个数
      };
      postRequest('/haoban-app-customer-web/inner/find-card-page', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.list || [];
            that.total = resData.result.pageInfo.total;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  mounted() {
    let that = this;
    that.$emit('showTab', '41');
    that.getTableList();
  }
};
</script>
<style type="text/less" lang="less" scoped>
.w-123 {
  width: 123px;
}
.p-20 {
  padding: 20px;
  box-sizing: border-box;
}
</style>
