<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-04-12 17:56:45
 * @LastEditors: 无尘
 * @LastEditTime: 2020-04-29 09:34:15
 -->
<template>
  <section class="common-right-wrap">
    <div class="p-20">
      <div class="flex flex-space-between m-b-20">
        <div class="flex ">
          <el-date-picker prefix-icon="el-icon-time" :picker-options="pickerOptions" v-model="conditionObj.getDateRange" @change="changeDate" :editable="false" :value-format="'yyyy-MM-dd'" type="daterange" align="right" unlink-panels range-separator="~" start-placeholder="领取开始时间" end-placeholder="领取结束时间"> </el-date-picker>
          <el-date-picker class="m-l-10" prefix-icon="el-icon-time" :picker-options="pickerOptions" v-model="conditionObj.useDateRange" @change="changeDate" :editable="false" :value-format="'yyyy-MM-dd'" type="daterange" align="right" unlink-panels range-separator="~" start-placeholder="核销开始时间" end-placeholder="核销结束时间"> </el-date-picker>
          <el-input placeholder="请输入卡券名称/卡券代码/会员信息" v-model="conditionObj.searchInput" class="w-264 m-l-10" style="width: 284px;" @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 v-if="!scope.row.receiveTime">
              --
            </div>
            <div v-else>
              <div class="color-606266 line-18">{{ scope.row.receiveTime | timeStampToYmd }}</div>
              <div class="color-606266 line-18">{{ scope.row.receiveTime | timeStampToHms }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="核销时间">
          <template slot-scope="scope">
            <div v-if="!scope.row.useTime">
              --
            </div>
            <div v-else>
              <div class="color-606266 line-18">{{ scope.row.useTime | timeStampToYmd }}</div>
              <div class="color-606266 line-18">{{ scope.row.useTime | timeStampToHms }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="会员信息" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="font-14 color-606266 line-18">{{ scope.row.memberName || '--' }}</div>
            <div class="font-12 color-909399 line-18">{{ scope.row.phoneNum | formatPhoneNum }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="员工信息" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="flex nowrap">
              <div class="flex flex-align-center flex-pack-center bg-82C5FF table-head-pic">
                <i v-if="!scope.row.headImg" class="iconfont icon-yewuduanmorentouxian"></i>
                <img v-else :src="scope.row.headImg" alt="img" />
              </div>
              <div class="flex flex-column apply-info m-l-10" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                <span class="font-14 color-606266 text-ellipsis line-18">{{ scope.row.staffName || '--' }}</span>
                <span class="font-12 color-909399 text-ellipsis line-18">{{ scope.row.departmentName || '--' }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="卡券类型">
          <template slot-scope="scope">
            <span v-if="scope.row.cardType == 0">抵金券</span>
            <span v-if="scope.row.cardType == 1">折扣券</span>
          </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">
            <span v-if="scope.row.status == 3" class="status-dot bg-7c6af2"></span><span v-if="scope.row.status == 4" class="status-dot bg-2f54eb"></span><span v-if="scope.row.status == 5" class="status-dot bg-52c41a"></span><span v-if="scope.row.status == 6" class="status-dot bg-f5222d"></span><span v-if="scope.row.status == 7" class="status-dot bg-c0c4cc"></span><span v-if="scope.row.status == 8" class="status-dot bg-faad14"></span><span class="inline-block">{{ statusObj[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="" label="投放来源" show-overflow-tooltip>
          <template slot-scope="scope">
            好办内购券
          </template>
        </el-table-column>
        <el-table-column prop="" label="核销来源" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.status != 5 ? '--' : scope.row.checkSource == 0 ? '订单核销' : '手动核销' }}
          </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 {
      activeId: '2',
      wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      statusObj: {
        '3': '待领取',
        '4': '待核销',
        '5': '已核销',
        '6': '已过期',
        '7': '已销毁',
        '8': '已占用'
      },
      conditionObj: {
        getDateRange: [],
        useDateRange: [],
        searchInput: ''
      },
      choiceDateCopy: [],
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.choiceDateCopy = [minDate.getTime()];
          if (maxDate) {
            this.choiceDateCopy = [];
          }
        },
        disabledDate: time => {
          if (this.choiceDateCopy.length) {
            const one = 365 * 24 * 3600 * 1000;
            const minTime = this.choiceDateCopy[0] - one;
            const maxTime = this.choiceDateCopy[0] + one;
            return time.getTime() < minTime || time.getTime() > maxTime;
          }
        }
      },
      templateList: [],
      strategyList: [],
      tableData: [],
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0
    };
  },
  watch: {
    brandId(val) {
      if (!!val) {
        this.getTableList();
      }
    }
  },
  methods: {
    /**
     *
     */
    changeDate() {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 删除
     */
    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 = {
        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();
            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();
    },

    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      let that = this;
      that.pageSize = val;
      that.getTableList();
    },

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

    /**
     * 获取列表数据
     */
    async getTableList() {
      const that = this;
      if (!that.conditionObj.getDateRange) {
        that.conditionObj.getDateRange = [];
      }
      if (!that.conditionObj.useDateRange) {
        that.conditionObj.useDateRange = [];
      }
      let para = {
        queryType: 2,
        wxEnterpriseId: that.wxEnterpriseId,
        receiveStartDate: that.conditionObj.getDateRange[0],
        receiveEndDate: that.conditionObj.getDateRange[1],
        useStartDate: that.conditionObj.useDateRange[0],
        useEndDate: that.conditionObj.useDateRange[1],
        search: that.conditionObj.searchInput || '', // 搜索字段
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize // 一页显示个数
      };
      postRequest('/haoban-app-customer-web/inner/find-card-log-page', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.result || [];
            that.total = resData.result.totalCount;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  mounted() {
    let that = this;
    that.$emit('showTab', '42');
    that.getTableList();
  }
};
</script>
<style type="text/less" lang="less" scoped>
.w-123 {
  width: 123px;
}
.p-20 {
  padding: 20px;
  box-sizing: border-box;
}
.text-ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nowrap {
  white-space: nowrap;
}
.table-head-pic {
  width: 36px;
  height: 36px;
  max-width: 36px;
  max-height: 36px;
  min-width: 36px;
  img {
    max-width: 36px;
    max-height: 36px;
  }
}
.status-dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 3px;
  &.bg-2f54eb {
    background: #2f54eb;
  }
  &.bg-f5222d {
    background: #f5222d;
  }
  // 已销毁
  &.bg-c0c4cc {
    background: #c0c4cc;
  }
  // 待领取
  &.bg-7c6af2 {
    background: #7c6af2;
  }
  // 已核销
  &.bg-52c41a {
    background: #52c41a;
  }
  // 已占用
  &.bg-faad14 {
    background: #faad14;
  }
}
</style>
