import Vue from 'vue';
export default Vue.component('render-temp', {
  props: {
    item: Object
  },
  methods: {
    handler(val) {
      this.$emit('handler', val);
    }
  },
  render(h) {
    const { $getButtonLimit, $buttonCode } = Vue.prototype;
    const canEdit = this.item.canEdit !== false; // 只有canEdit === false 才证明不能使用   没有权限仅可以详情/报表
    const isApiCreate = this.item.isApiCreate || false; // 由外部api创建 仅展示详情、删除、报表按钮
    const showPutonBtn = this.item.showPutonBtn || false;
    // 是否有商场抵金券的创建权限 true：有权限 false：无权限
    const hasCouponCreate = this.$store.getters.getLimitCodeList.every(el => el != 'couponCreate');
    // 是否是商场类商户
    const hasStoreCardAuth = this.$store.getters.getStoreCardAuth;
    // 当前卡券是否 是长期有效||在投放有效期内
    // putEffectiveMode 0：固定日期，1：长期
    const canGroupSend = this.item.putEffectiveMode == 1 ? true : new Date() > new Date(this.item.putBeginDate) && new Date() < new Date(this.item.putEndDate);
    const isWeimo = this.item.gicCouponType == 3;
    const isCommon = this.item.gicCouponType == 4;
    // 微盟外部券：领取记录、投放、报表
    // 通用外部券：领取记录、报表
    const bind1 =
      $getButtonLimit($buttonCode.marketingSendCard) && (!isApiCreate || isWeimo) && canGroupSend && !isCommon && showPutonBtn ? (
        <a title="投放" limit-code={$buttonCode.marketingSendCard} onClick={this.handler.bind(null, 1)}>
          <i class="iconfont icon-daohang-" />
        </a>
      ) : (
        ''
      );
    const bind2 = (
      <a title="报表" onClick={this.handler.bind(null, 2)}>
        <i class="el-icon-tickets" />
      </a>
    );
    const bind3 =
      $getButtonLimit($buttonCode.marketingEditCard) && !isWeimo && !isApiCreate && (this.item.cardType == 3 && hasStoreCardAuth ? hasCouponCreate : true) ? (
        <a title="编辑" limit-code={$buttonCode.marketingEditCard} onClick={this.handler.bind(null, 3)}>
          <i class="el-icon-edit" />
        </a>
      ) : (
        ''
      );
    const bind4 =
      $getButtonLimit($buttonCode.marketingDelCard) && canEdit && !isWeimo && (this.item.cardType == 3 && hasStoreCardAuth ? hasCouponCreate : true) ? (
        <a title="删除" limit-code={$buttonCode.marketingDelCard} onClick={this.handler.bind(null, 4)}>
          <i class="el-icon-delete" />
        </a>
      ) : (
        ''
      );
    const bind5 = (
      <a title="详情" onClick={this.handler.bind(null, 5)}>
        <i class="el-icon-document" />
      </a>
    );
    const bind6 =
      $getButtonLimit($buttonCode.marketingCopyCard) && canEdit && !isApiCreate && !isWeimo && (this.item.cardType == 3 && hasStoreCardAuth ? hasCouponCreate : true) ? (
        <a title="复制" limit-code={$buttonCode.marketingCopyCard} onClick={this.handler.bind(null, 6)}>
          <i class="iconfont icon-wxcard-copy" />
        </a>
      ) : (
        ''
      );

    const bind7 =
      (canEdit && !isApiCreate) || isWeimo || isCommon ? (
        <a style="position:relative;top:1px" title="领取记录" onClick={this.handler.bind(null, 7)}>
          <i class="iconfont icon-tubiaozhizuomoban-" />
        </a>
      ) : (
        ''
      );

    const statushtml = (
      <span>
        {bind7}
        {bind6}
        {bind1}
        {bind2}
        {bind3}
        {bind4}
        {isWeimo || isCommon ? bind5 : ''}
      </span>
    );
    const overDateHtml = (
      <span>
        <span class="danger">已过期</span>
        {bind7}
        {bind6}
        {bind2}
        {bind5}
        {bind4}
      </span>
    );
    const noputonStatushtml = (
      <span>
        {bind7}
        {bind6}
        {bind2}
        {bind3}
        {bind4}
      </span>
    );
    const statusNostockhtml = noputonStatushtml;
    const statusFaildhtml = (
      <span>
        {bind6}
        {bind3}
        {bind4}
      </span>
    );
    const _html = this.item.putEffectiveMode == 0 && new Date(this.item.putEndDate) < new Date() ? overDateHtml : statushtml;
    let failMsg = '';
    if (this.item.autingFaildMsg) {
      failMsg = '：' + this.item.autingFaildMsg;
      if (this.item.autingFaildMsg.length > 8) {
        failMsg = (
          <el-popover placement="top-start" title="" width="200" trigger="hover" content={this.item.autingFaildMsg}>
            <span class="danger-color" slot="reference">
              ：{this.item.autingFaildMsg.substring(0, 8) + '...'}
            </span>
          </el-popover>
        );
      }
    }

    if (this.item.putEffectiveMode == 0 && new Date(this.item.putEndDate) < new Date()) {
      return <div class="fr">{overDateHtml}</div>;
    }

    if (this.item.auditingStatus === 0) {
      return (
        <div class="fr">
          <span class="green">审核中</span>
        </div>
      );
    } else if (this.item.auditingStatus === -1) {
      return (
        <div class="fr">
          <span class="danger-color">
            审核失败{failMsg}
            {statusFaildhtml}
          </span>
        </div>
      );
    } else if (this.item.auditingStatus === -2) {
      return (
        <div class="fr">
          <span class="danger-color">
            更新失败{failMsg}
            {_html}
          </span>
        </div>
      );
    } else if (this.item.useCustomCode == 1 && this.item.customCodeSync == 1) {
      return (
        <div class="fr">
          <span class="danger-color">正在上传卡券Code</span>
        </div>
      );
    } else if (this.item.stock === 0) {
      return <div class="fr">{statusNostockhtml}</div>;
    } else if (this.item.cardLimit) {
      // 放开cardLimit一张限制
      return <div class="fr">{_html}</div>;
    } else {
      return <div class="fr">{noputonStatushtml}</div>;
    }
  }
});
