<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-04-12 17:56:45
 * @LastEditors: 无尘
 * @LastEditTime: 2020-12-14 10:40:11
 -->
<template>
  <section class="common-right-wrap">
    <div class="p-20">
      <div class="flex flex-space-between m-b-20">
        <el-input placeholder="请输入卡券名称" maxlength="50" v-model="searchInput" class="w-264" style="width: 264px;" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearInput"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input>
        <el-button v-if="$getButtonLimit($buttonCode.addCard)" :limit-code="$buttonCode.addCard" type="primary" @click="toNewCard" :disabled="$store.state.wxEnterpriseType">新增卡券</el-button>
      </div>
      <el-table class="select-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" style="width: calc(100% - 3px);">
        <el-table-column prop="" label="卡券名称" show-overflow-tooltip min-width="200px">
          <template slot-scope="scope">
            {{ scope.row.cardName }}
          </template>
        </el-table-column>

        <el-table-column prop="" label="卡券类型" min-width="80px">
          <template slot-scope="scope">
            <span v-if="scope.row.cardType == 0">抵金券</span>
            <span v-if="scope.row.cardType == 1">折扣券</span>
            <span v-if="scope.row.cardType == 2">兑换券</span>
          </template>
        </el-table-column>
        <el-table-column prop="" label="卡券模板有效期" show-overflow-tooltip min-width="260px">
          <template slot-scope="scope">{{ scope.row.effectDateString || '--' }}</template>
        </el-table-column>
        <el-table-column prop="" label="库存" min-width="100px">
          <template slot-scope="scope">
            <div>{{ scope.row.couponStock }}</div>
          </template>
        </el-table-column>
        <el-table-column label="卡券库状态" prop="cardStatus" min-width="90px">
          <template slot-scope="{ row }">
            <!-- 0删除1有效 2已过期 3未开始 -->
            <span class="state-point state-point-error" v-if="row.cardStatus == 0">已删除</span>
            <span class="state-point state-point-success" v-if="row.cardStatus == 1">正常</span>
            <span class="state-point state-point-error" v-if="row.cardStatus == 2">已过期</span>
            <span class="state-point state-point-default" v-if="row.cardStatus == 3">未生效</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="creatorName" show-overflow-tooltip min-width="120px"></el-table-column>
        <el-table-column label="编辑人" prop="modifierName" show-overflow-tooltip min-width="120px"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="120px">
          <div slot-scope="{ row }" style="line-height: normal">
            <template v-if="row.createTime">
              <p>{{ row.createTime | timeStampToYmd }}</p>
              <p>{{ row.createTime | timeStampToHms }}</p>
            </template>
            <template v-else>--</template>
          </div>
        </el-table-column>
        <el-table-column prop="" label="操作" show-overflow-tooltip v-if="$getButtonLimit($buttonCode.editCard) || $getButtonLimit($buttonCode.delCard)" min-width="150px">
          <template slot-scope="scope">
            <el-button v-if="$getButtonLimit($buttonCode.editCard)" :limit-code="$buttonCode.editCard" type="text" @click="toEdit(scope.$index, scope.row)" :disabled="$store.state.wxEnterpriseType">编辑</el-button>
            <el-button v-if="$getButtonLimit($buttonCode.delCard)" :limit-code="$buttonCode.delCard" type="text" @click="toDel(scope.$index, scope.row)" :disabled="$store.state.wxEnterpriseType">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
        <dm-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"> </dm-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 {
      loading: false,
      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.coupCardId);
        })
        .catch(() => {});
    },
    /**
     * 删除卡券
     */
    delCardList(coupCardId) {
      const that = this;
      let para = {
        gicEnterpriseId: that.brandId,
        coupCardId: coupCardId
      };
      postRequest('/haoban-app-customer-web/cardCoupon/del-card-coup', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.currentPage = 1;
            that.getTableList(that.brandId);
            return;
          }
          if (resData.errorCode == 100013) {
            that.currentPage = 1;
            that.getTableList(that.brandId);
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 编辑卡券
     */
    toEdit(index, row) {
      const that = this;
      that.$router.push(`/editCard?coupCardId=${row.coupCardId}`);
    },
    /**
     * 输入
     */
    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(`/newCard`);
    },
    /**
     * 分页---页码变化
     *  @param {Number} val
     */
    handleSizeChange(val) {
      let that = this;
      that.currentPage = 1;
      that.pageSize = val;
      that.getTableList();
    },

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

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