<template>
  <cr-popup class="invoice" :value="show" round position="bottom" closeable @close="onClose">
    <div class="invoice-title">发票抬头选择</div>
    <div class="invoice-wrapper">
      <div v-if="list.length" class="invoice-content">
        <cr-swipe-cell v-for="(item, index) in list" :key="index">
          <div class="invoice__cell" @click="selectInvoiceEvent(item)">
            <dl class="invoice__cell__msg">
              <dt>{{ item.invoiceTitle }}</dt>
              <dd>{{ titleType[item.invoiceTitleType] }}抬头</dd>
            </dl>
            <div class="invoice__cell__edit" @click.stop="editInvoiceEvent(item)">编辑</div>
          </div>
          <!-- <template #right>
            <cr-button class="invoice__delete" square type="danger" text="删除" @click="onDelete" />
          </template> -->
        </cr-swipe-cell>
      </div>
      <Empy v-else top="30%" />
    </div>
    <footer-button btn-text="添加新的抬头" @btn-click="addInvoiceHeader" />
  </cr-popup>
</template>

<script>
const DIALOG_CLOSE_EVENT = 'close';
const SELECT_INVOICE_ITEM = 'select';
import { titleType } from '../config';
import jumpAppMixin from '../mixins/jumpApp.mixin';
import Empy from './empy.vue';
import footerButton from './footerButton.vue';
export default {
  name: 'GiftPopup',
  components: {
    Empy,
    footerButton
  },
  mixins: [jumpAppMixin],
  props: {
    value: {
      type: Boolean,
      default: false
    },
    invoiceTitleType: {
      type: Number,
      default: 0
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      titleType,
      // list: new Array(10),
      show: false
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.show = val;
      }
    }
  },
  methods: {
    addInvoiceHeader() {
      const invoiceTitleType = this.invoiceTitleType;
      this.onClose();
      this.goInvoiceHeader(`/electronicReceipt/invoiceHeader`, { invoiceTitleType });
      // this.openAppUrl('/electronicReceipt/invoiceHeader', { invoiceTitleType });
    },
    selectInvoiceEvent(item) {
      this.$emit(SELECT_INVOICE_ITEM, item);
      this.onClose();
    },
    editInvoiceEvent(item) {
      const id = item.id;
      const invoiceTitleType = this.invoiceTitleType;
      this.onClose();
      this.goInvoiceHeader(`/electronicReceipt/invoiceHeader`, { id, invoiceTitleType });
      // this.openAppUrl(`/electronicReceipt/invoiceHeader`, { id, invoiceTitleType });
    },
    goInvoiceHeader(url, params) {
      const timer = setTimeout(() => {
        this.openAppUrl(url, params);
        clearTimeout(timer);
      }, 500);
    },
    onClose() {
      this.$emit('update:value', false);
    },
    go() {
      this.$emit(DIALOG_CLOSE_EVENT, 2);
    },
    onDelete() {}
  }
};
</script>

<style lang="less" scoped>
@import './common.less';
.invoice {
  color: @black;
  height: 534px;
  background: #f7f8fa; //@gray-1;
  display: flex;
  flex-direction: column;
  @{deep} .cr-popup--close {
    color: #c8c9cc;
    top: 13px;
    right: 16px;
    z-index: 1;
  }

  &-title {
    .text-size(16);
    font-weight: bold;
    height: 48px;
    line-height: 48px;
    text-align: center;
    .border-bottom();
  }
  &-wrapper {
    flex: 1;
    padding: 15px 12px;
    box-sizing: border-box;
    overflow-y: hidden;
  }
  &-content {
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    &::-webkit-scrollbar {
      display: none;
    }
  }
  &__cell {
    padding: 8px 12px;
    background-color: @white;
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    border-radius: 8px;
    box-sizing: border-box;
    overflow: hidden;
    &__msg {
      padding: 2px 0;
      dt {
        .text-size(14);
        max-width: 280px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      dd {
        .text-size(12);
        color: @gray-4;
        margin-top: 2px;
      }
    }

    &__edit {
      line-height: 100%;
      font-size: 14px;
      padding-left: 10px;
      position: relative;
      display: flex;
      align-items: center;
      padding-right: 1px;
      &::before {
        content: '';
        width: 1px;
        height: 100%;
        background-color: @gray-7;
        position: absolute;
        left: 0;
        top: 0;
        transform: scaleX(0.5);
      }
    }
  }
  &__delete {
    margin-left: 1px;
  }
}
</style>
