<template>
  <!--  @mouseover="show = true" @mouseout="show = false" -->
  <div class="card">
    <div class="card-head" :style="`background: ${ item._status === 13 || item.deleteFlag == 1 ? '#c0c4cc' : item.color }`">
      <p class="title" p-key="couponId" :title="item.couponId">{{ item.cardName }}</p>
      <p>{{ item.remarkName }}</p>
    </div>
    <div class="card-body">
      <ul>
        <li class="ellipsis-100">
          <label>有效期：</label>
            <el-tooltip open-delay="200" placement="top">
              <div slot="content" style="max-width:400px;line-height:18px;">{{ formateEffectDate(item) }}</div>
              <div class="ellipsis-100 tip-li">
                <span>{{ formateEffectDate(item) }}</span>
              </div>
            </el-tooltip>
          <!-- <template v-if="item.cardEffectiveMode !== 0">领取后第{{ item.effectTimeCount }}{{ item.effectTimeMode ? '月' : '天' }}-第{{ item.expireTimeCount + item.effectTimeCount - 1 }}天</template>
          <template v-if="item.cardEffectiveMode === 0">{{ formatDateTimeByType(item.startDate, 'yyyy-MM-dd') }}~{{ formatDateTimeByType(item.expireDate, 'yyyy-MM-dd') }}</template> -->
        </li>
        <li>
          <label>使用说明：</label>
          <div class="ellipsis-100 tip-li">
            <el-tooltip open-delay="200" placement="top">
              <div slot="content" style="max-width:400px;line-height:18px;">{{ item.useExplain }}</div>
              <span style="display:inline-block;max-width:110%;overflow:hidden;max-height:100%;">{{ item.useExplain }}</span>
            </el-tooltip>
          </div>
        </li>
        <li>
          <label>适用门店：</label>
          <template v-if="item.adaptStoreExplain">
            <div class="ellipsis-100 tip-li">
              <el-tooltip open-delay="200" placement="top">
                <div slot="content" style="max-width:400px;line-height:18px;">{{ item.adaptStoreExplain }}</div>
                <span style="display:inline-block;max-width:110%;overflow:hidden;max-height:100%;">{{ item.adaptStoreExplain || '--'}}</span>
              </el-tooltip>
            </div>
          </template>
          <span v-else>{{ item.adaptStoreExplain || '--'}}</span>
        </li>
        <li>
          <label>领券限制：</label>每人限制 {{ item.getLimit }} 张
        </li>
        <li>
          <label>剩余库存：</label>
          <span>{{ item.remainedStock }}
            <!-- 需要根据状态判断库存是否可以改变  使用中,随机生成才可以改 -->
            <a class="btn-a hover-btn" v-if="[11, 12].includes(item._status) && item.customCodeFlag === 0" title="编辑库存" @click="preAdjustStock(item)"><i class="el-icon-edit-outline ml3"></i></a>
            <!-- 导入券码的卡券  使用中才可以改 -->
            <a class="btn-a hover-btn ml5" v-if="[11, 12].includes(item._status) && item.customCodeFlag == 2" title="上传券码" @click="$emit('upload-code', item)"><i class="iconfont-market4 icon-shangchuan ml3"></i></a>
          </span>
        </li>
        <li v-if="item.erpDemoCode"><label>demo券号：</label>{{ item.erpDemoCode }}</li>
        <li class="flex">
          <!-- {{ item._status }} -->
          <label>卡券状态：</label>
          <span class="inline-block" v-html="renderStatus(item)"></span>
          <template v-if="item.deleteFlag">
            <el-tooltip open-delay="200" :content="item.remark" placement="top">
              <span v-if="item.remark"><i style="top:1px" class="el-icon-info unuse-icon"></i></span>
            </el-tooltip>
          </template>
          <template v-else-if="[2, 4, 6, 7, 8].includes(item._status)">
            <el-tooltip open-delay="200" :content="item.auditingFaildMsg" placement="top">
              <i class="el-icon-question fz13 gray03 cursor ml8"></i>
            </el-tooltip>
          </template>
        </li>
      </ul>
    </div>
    <!-- <div class="card-hover" :class="{ show }" @mouseover="show = true" @mouseout="show = false"> -->
    <div class="card-hover show">
      <!-- :close-delay="40000000" -->
      <el-popover open-delay="200" placement="bottom" trigger="hover" popper-class="option-dropdown">
        <!-- 可操作按钮 -->
        <div @mouseover="show = true" @mouseout="show = false">
          <li :class="{ disabled: $itemPerm($itemCode.gicCardFormEdit) || item.deleteFlag }" v-if="[2, 4, 6, 11, 12].includes(item._status)"><mk-button type="text" :option="{ perm: $itemPerm($itemCode.gicCardFormEdit), deleteFlag: item.deleteFlag, deleteRemark: item.remark }" @click="handler(3, item)">编辑</mk-button></li>
          <li :class="{ disabled: $itemPerm($itemCode.gicCardFormCopy) || item.deleteFlag }" v-if="[2, 4, 6, 7, 8, 11, 12, 13].includes(item._status)"><mk-button type="text" :option="{ perm: $itemPerm($itemCode.gicCardFormCopy), deleteFlag: item.deleteFlag, deleteRemark: item.remark }" @click="handler(6, item)">复制</mk-button></li>
          <!-- <li v-if="[12, 13].includes(item._status)"><mk-button type="text" :disabled="" @click="handler(2, item)">报表</mk-button></li> -->
          <li :class="{ disabled: $itemPerm($itemCode.gicCardFormInfo) }"><dm-perm-button type="text" @click="handler(5, item)" :option="{ perm: $itemPerm($itemCode.gicCardFormInfo), deleteFlag: item.deleteFlag, deleteRemark: item.remark }">详情</dm-perm-button></li>
          <li :class="{ disabled: $itemPerm($itemCode.gicCardGroupSendForm) || item.deleteFlag }" v-if="[4, 6, 11, 12].includes(item._status)"><mk-button type="text" @click="handler(1, item)" :option="{ perm: $itemPerm($itemCode.gicCardGroupSendForm), deleteFlag: item.deleteFlag, deleteRemark: item.remark }">投放</mk-button></li>
          <li :class="{ disabled: $itemPerm($itemCode.gicCardSyncStock) || item.deleteFlag }" v-if="[11, 12].includes(item._status)"><mk-button type="text" @click="handler(7, item)" :option="{ perm: $itemPerm($itemCode.gicCardSyncStock), deleteFlag: item.deleteFlag, deleteRemark: item.remark }">同步</mk-button></li>
          <li :class="{ disabled: $itemPerm($itemCode.gicCardFormDel) }"><dm-perm-button type="text" @click="handler(4, item)" :disabled="$itemPerm($itemCode.gicCardFormDel)">删除</dm-perm-button></li>
          <li :class="{ disabled: $itemPerm($itemCode.gicCardGroupBtn) || item.deleteFlag }" v-if="[12, 13].includes(item._status)"><mk-button type="text" @click="handler(8, item)"  :option="{ perm: $itemPerm($itemCode.gicCardGroupBtn), deleteFlag: item.deleteFlag, deleteRemark: item.remark }">领取记录</mk-button></li>
        </div>
        <i class="icon-gengduo iconfont-market4 more" slot="reference"></i>
      </el-popover>
    </div>
  </div>
</template>

<script>
import { cardStatus } from '@/config/mapping/gic-card';
import { formatDateTimeByType } from '@/utils/index.js';
export default {
  name: 'CardItem',
  props: {
    item: {
      type: Object,
      default() {
        return {
          cardEffectiveMode: 0
        };
      }
    },
  },
  data() {
    return {
      show: false
    };
  },
  methods: {
    handler(val, item, disabled) {
      this.$emit('handler', val, item, disabled);
    },
    formateEffectDate(item = {}) {
      const { cardEffectiveMode, startDate, expireDate, effectTimeMode, expireTimeCount, effectTimeCount } = item;
      if (cardEffectiveMode === 0) {
        return `${formatDateTimeByType(startDate, 'yyyy-MM-dd')} ~ ${ formatDateTimeByType(expireDate, 'yyyy-MM-dd') }`;
      }
      if (cardEffectiveMode === 1) {
      // 领取后第{{ info.effectTimeCount }}{{ info.effectTimeMode ? '月' : '天' }} - 第{{ info.expireTimeCount + info.effectTimeCount - 1 }}{{ info.effectTimeMode ? '月' : '天' }}
        const unit = effectTimeMode ? '月' : '天';
        let pre = '';
        switch (effectTimeCount) {
          case 1:
            pre = '当';
            break;
          case 2:
            pre = '次';
            break;
          default:
            pre = effectTimeCount;
            break;
        }
        return `领取后${pre}${unit}生效，有效天数${expireTimeCount}${unit}`;
      }
      return '--';
    },
    // 编辑库存 前置
    preAdjustStock() {
      this.$emit('adjust-stock', this.item);
    },
    delShelf(obj) {
      this.$emit('del-shelf', obj);
    },
    renderStatus(item) {
      const { _status, deleteFlag } = item;
      if(deleteFlag == 1) {
        // 失效
        return '<span class="state-point state-point-default">已失效</span>';
      }
      let _cl = '';

      switch (_status) {
        case 1:
        case 3:
        case 5:
        case 9:
        case 10:
          _cl = 'state-point-loading'; // 审核和使用中
          break;
        case 12:
          _cl = 'state-point-loading-scale'; // 使用中
          break;
        case 2:
        case 4:
        case 6:
        case 7:
        case 8:
          _cl = 'state-point-error'; // 审核失败
          break;
        case 9:
        case 11:
          _cl = 'state-point-warning'; // 未生效
          break;
        case 13:
          _cl = 'state-point-default'; // 已过期
          break;
        default:
          _cl = 'state-point-default';
          break;
      }
      return `<span class="state-point ${_cl}">${cardStatus[_status]}</span>`;
    }

  },
};
</script>

<style lang="scss" scoped>
.card {
  position: relative;
  width: 244px;
  min-height: 326px;
  overflow: hidden;
  float: left;
  margin-right: 20px;
  margin-bottom: 30px;
  .card-head {
    border-radius:2px 2px 0 0;
    text-align: center;
    height: 120px;
    position: relative;
    color: white;
    background:#60C996;
    p.title {
      font-size:20px;
      line-height:28px;
      padding: 37px 0 8px;
    }
    p {
      font-size:14px;
      line-height:20px;
    }
    &::before {
      content: '';
      position: absolute;
      display: block;
      background: url('../../../assets/images/circle-icon.png') no-repeat;
      width: 30px;
      height: 15px;
      top: 0;
      left: 50%;
      margin-left: -15px;
    }
    &::after {
      content: '';
      width: 100%;
      height: 3px;
      position: absolute;
      left: 0;
      bottom: 0;
      background: url('../../../assets/images/dot-icon.png') repeat-x 4px 0;
    }
  }
  .card-body {
    min-height: 200px;
    background:rgba(248,248,248,1);
    padding: 10px 8px 12px;
    border-radius: 0 0 2px 2px;
    ul {
      font-size:12px;
      color:rgba(48,49,51,1);
      line-height:17px;
      li {
        margin-bottom: 8px;
        &:last-child {
          margin-bottom: 0px;
        }
        &.flex {
          display: flex;
          align-items: center;
        }
        .tip-li {
          display: inline-block;
          width: 67%;
          vertical-align: bottom;
          cursor: pointer;
          height: 17px;
          line-height: 17px;
        }
      }
      label {
        color:rgba(96,98,102,1);
        display: inline-block;
        width: 72px;
        // margin-right: 12px;
      }
    }
  }
  .card-hover {
    &.show {
      opacity: 1;
    }
    cursor: pointer;
    opacity: 0;
    transition: 0.2s all ease;
    position: absolute;
    width: 24px;
    height: 24px;
    text-align: center;
    right: 10px;
    top: 6px;
    background: white;
    border-radius: 50%;
    line-height: 24px;
    i.more {
      font-size: 14px;
      color: $gray03;
    }
  }
}

</style>