<template>
  <div class="dm-wrap card_profit">
    <div class="title">
      <h2>卡券收益</h2>
      <span>计划中的卡券，计划中卡券触达的人群使用该卡券消费的收益，包含已过计划收益有效期的数据</span>
    </div>
    <template v-if="tableData.length">
      <div class="list">
        <div class="list_left">
          <div>
            <p>触达人数</p>
            <p>{{ formatterNum(list.touchMbrNum) }}</p>
          </div>
          <div>
            <p>
              领取人数<span> （领取率 {{ formatterRate((list.getMbrNum / list.touchMbrNum) * 100) }}）</span>
            </p>
            <p>{{ formatterNum(list.getMbrNum) }}</p>
          </div>
          <div>
            <p>
              使用人数<span> （使用率 {{ formatterRate((list.useMbrNum / list.getMbrNum) * 100) }}）</span>
            </p>
            <p>{{ formatterNum(list.useMbrNum) }}</p>
          </div>
        </div>
        <div class="list_right">
          <div>
            <p>销售单数</p>
            <p>{{ formatterNum(list.orderCnt) }}</p>
          </div>
          <div>
            <p>销售单金额（元）</p>
            <p>{{ parseFloat(list.salesAmt || 0).toLocaleString('zh', { minimumFractionDigits: 2 }) }}</p>
          </div>
        </div>
      </div>
      <el-table :data="tableData" v-if="tableData.length > 1" style="margin-bottom:47px" max-height="710">
        <el-table-column :prop="cardName" label="卡券名称" min-width="150">
          <template slot-scope="scope">
            <div class="name" v-if="scope.row.cardName">
              <div class="top">
                <span v-if="scope.row.cardName.length <= 15" :class="[scope.row.status == 0 ? 'del_card' : 'pointer']" @click="toCardDetail(scope.row)">{{ scope.row.cardName }}</span>
                <el-tooltip open-delay="100" placement="top-start" :content="scope.row.cardName" v-else>
                  <span :class="['card_name', scope.row.status == 0 ? 'del_card' : 'pointer']" @click="toCardDetail(scope.row)">{{ scope.row.cardName }}</span>
                </el-tooltip>
                <span class="type" v-if="scope.row.status != 0">{{ scope.row.cardType == 0 ? '抵金券' : scope.row.cardType == 1 ? '折扣券' : scope.row.cardType == 2 ? '兑换券' : '--' }}</span>
                <img :src="require('@/assets/img/Icon-yishanchu.svg')" alt="" v-else style="width: 46px;height: 21px;margin-left: 6px" />
              </div>
              <p>{{ scope.row.subTitle }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-for="(v, i) in tableHeader" :key="i" :prop="v.prop" :label="v.label" :formatter="v.formatter">
          <template slot-scope="scope">
            <span v-if="v.formatter" v-html="v.formatter(scope.row)"></span>
            <span v-else>{{ scope.row[v.prop] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div class="empty" v-else>
      <div class="content">
        <span class="icon iconfont">&#xe76e;</span>
        <span class="none-tip">本计划未使用卡券营销</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'card-profit',
  props: {
    list: {
      type: Object,
      default: () => {}
    },
    tableData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      tableHeader: [
        { label: '触达人数', prop: 'touchMbrNum', minWidth: '160', align: 'left', fixed: 'left', formatter: row => this.formatterNum(row.touchMbrNum) },
        { label: '领取人数', prop: 'getMbrNum', minWidth: '160', align: 'left', fixed: 'left', formatter: row => this.formatterNum(row.getMbrNum) },
        { label: '领取率', prop: 'drawRate', minWidth: '160', align: 'left', fixed: 'left', formatter: row => this.formatterRate((row.getMbrNum / row.touchMbrNum) * 100) },
        { label: '使用人数', prop: 'useMbrNum', minWidth: '160', align: 'left', fixed: 'left', formatter: row => this.formatterNum(row.useMbrNum) },
        { label: '使用率', prop: 'useRate', minWidth: '160', align: 'left', fixed: 'left', formatter: row => this.formatterRate((row.useMbrNum / row.getMbrNum) * 100) },
        { label: '销售单数', prop: 'orderCnt', minWidth: '160', align: 'left', fixed: 'left', formatter: row => this.formatterNum(row.orderCnt) },
        { label: '销售单金额', prop: 'salesAmt', minWidth: '160', align: 'left', fixed: 'left', formatter: row => parseFloat(row.salesAmt || 0).toLocaleString('zh', { minimumFractionDigits: 2 }) }
      ]
    };
  },
  methods: {
    toCardDetail(row) {
      if (row.status == 0) return;
      window.open(window.location.origin + `/marketing/#/card/edit/${row.cardId}`);
    }
  },
  computed: {
    formatterNum() {
      return val => (!val ? '0' : parseInt(val).toLocaleString());
    },
    formatterRate() {
      return val => (!val ? '0.00%' : parseFloat(val).toFixed(2) + '%');
    }
  }
};
</script>

<style scoped lang="scss">
.card_profit {
  padding: 19px 34px 7px 28px !important;
  margin-bottom: 10px !important;
  font-family: PingFangSC-Regular, PingFang SC;
  .title {
    padding-left: 4px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    h2 {
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 700;
      color: #303133;
      line-height: 22px;
    }
    span {
      margin-left: 28px;
      font-size: 14px;
      color: #909399;
    }
  }
  .list {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .list_left {
      margin-right: 10px;
    }
    .list_left,
    .list_right {
      background: #f0f5ff;
      border-radius: 6px;
      flex: 1;
      height: 115px;
      padding-left: 40px;
      box-sizing: border-box;
      display: flex;
      div {
        flex: 1;
        padding-top: 26px;
        &:nth-of-type(1) {
          flex: 0.8;
        }
        p {
          &:nth-child(1) {
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #606266;
            line-height: 20px;
            padding-top: 4px;
            margin-bottom: 6px;
          }
          &:nth-child(2) {
            font-size: 24px;
            font-family: DINAlternate-Bold, DINAlternate;
            font-weight: bold;
            color: #303133;
            line-height: 28px;
          }
          .active {
            color: #f5222d;
            margin-left: 4px;
          }
          span {
            color: #303133;
          }
        }
      }
    }
    .list_right div {
      text-align: center;
    }
  }
  .name {
    color: #303133;
    line-height: 22px;
    .top {
      display: flex;
      align-items: center;
      .type {
        display: inline-block;
        width: 42px;
        height: 16px;
        line-height: 16px;
        text-align: center;
        background: #fff1f0;
        border-radius: 2px;
        border: 1px solid #ffa39e;
        font-size: 12px;
        color: #f5222d;
        margin-left: 6px;
      }
      .card_name {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        max-width: 210px;
      }
      .del_card {
        cursor: normal;
        opacity: 0.56;
      }
      .pointer {
        cursor: pointer;
      }
    }
    p {
      font-size: 12px;
      color: #909399;
      line-height: 22px;
    }
  }
  .empty {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    color: #909399;
    font-size: 16px;
    .content {
      display: flex;
      align-items: center;
      .icon {
        font-size: 30px;
        margin-right: 10px;
      }
      .none-tip {
        color: #c0c4cc;
        font-size: 14px;
      }
    }
  }
}
</style>
