<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-04-12 17:56:45
 * @LastEditors: 无尘
 * @LastEditTime: 2020-04-28 14:06:35
 -->
<template>
  <section class="common-right-wrap">
    <div class="p-20">
      <div class="flex flex-space-between m-b-20">
        <div>
          <span class="font-12 color-909399 p-l-10 font-w-300">内购券投放是针对卡券策略中没有下发的内购券，针对员工进行补发</span>
        </div>
        <el-button type="primary" @click="toNewCard">新增补发</el-button>
      </div>
      <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: calc(100% - 0px);">
        <el-table-column prop="" label="卡券组合名称" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.cardCombineName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="卡券模板" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.cardTemplates }}
            <!-- <span v-for="(item, key) in scope.row.cardTemplateJson" :key="key + 'temp'">{{ item.templateName }}</span> -->
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="toEdit(scope.$index, scope.row)">查看</el-button>
            <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 {
      activeId: '2',
      wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',

      searchInput: '',
      tableData: [],
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0
    };
  },
  watch: {
    brandId(val) {
      if (!!val) {
        this.getTableList();
      }
    }
  },
  methods: {
    /**
     * 删除
     */
    toDel(index, row) {
      const that = this;
      that
        .$confirm('是否要删除选中的补发卡券?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.delCardList(row.repairId);
        })
        .catch(() => {});
    },
    /**
     * 删除卡券
     */
    delCardList(repairId) {
      const that = this;
      let para = {
        repairId: repairId
      };
      postRequest('/haoban-app-customer-web/inner/delete-repair', 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
          });
        });
    },
    /**
     * 编辑卡券
     */
    toEdit(index, row) {
      const that = this;
      that.$router.push(`/newCardPut?repairId=${row.repairId}`);
    },
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    }, 500),
    // 搜索清除
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    },
    /**
     * 新建卡券
     */
    toNewCard() {
      const that = this;
      that.$router.push(`/newCardPut`);
    },
    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      let that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      let that = this;
      that.currentPage = val;
      that.getTableList();
    },
    /**
     * 切换年
     */
    changeYear() {
      let that = this;
      that.currentPage = 1;
      that.getTableList();
    },

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