<template>
  <div class="invoice-wrapper">
    <div class="mobile-nav">
      <div class="mobile-header mobile-center-header">
        <a @click="goBack()"><i class="iconfont icon-fanhui"></i></a>
        <p>发票管理</p>
        <p class="en-name">
          <img :src="`/images/mobile/center/${user.data.enterprise && user.data.enterprise.uu ? 'en' : 'self'}.png`" alt="">{{currentEnName}}
        </p>
      </div>
    </div>
    <div class="order-nav">
      <div class="active" @click="changeTab('')"><span>发票信息</span></div>
      <nuxt-link to="/mobile/center/user/invoice/waitinvoice" tag="div"><span>待开票订单</span></nuxt-link>
      <nuxt-link to="/mobile/center/user/invoice/invoiceRecord" tag=div><span>开票记录</span></nuxt-link>
    </div>
    <div class="invoice-wrapper-el">
      <div class="invoice-top clearfix">
        <div class="pull-left">已设置的开票信息</div>
        <div class="pull-right" v-if="invoiceList.length !== 2" @click="addInvoiceNew()"><img src="/images/mobile/center/user/icon-rm-invoice.png"/>新增发票</div>
      </div>
      <div class="invoice-middle" v-if="invoiceList.length > 0">
        <div v-for="(item, index) in invoiceList">
          <!-- 普票 -->
          <div class="invoice-normal" v-if="item.kind === 1206">
            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">发票类型：</div>
              <div class="pull-right normal-primary">普票</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">开票抬头：</div>
              <div class="pull-right overhiden">{{item.head}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">收票人：</div>
              <div class="pull-right overhiden">{{item.name}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">收票人电话：</div>
              <div class="pull-right overhiden">{{item.telephone}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">收票地址：</div>
              <div class="pull-right">
                <p>{{item.area}}</p>
                <p>{{item.detailAddress}}</p>
              </div>
            </div>

            <div class="invoice-controll clearfix">
              <div class="pull-left" @click="updateInvoice(item, index)"><i class="iconfont icon-edit" ></i>修改</div>
              <div class="pull-left" @click="deleteInvoice(item, index)"><i class="iconfont icon-lajitong" ></i>删除</div>
            </div>
          </div>
          <!-- 专票 -->
          <div :style="!hasnormal ? '': 'margin-top: 0.24rem'" class="invoice-normal invoice-speci" v-else-if="item.kind === 1205">
            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">发票类型：</div>
              <div class="pull-right normal-primary ">专票</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">开票抬头：</div>
              <div class="pull-right overhiden">{{item.head}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">单位地址：</div>
              <div class="pull-right overhiden">{{item.companyAddress}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">收票电话：</div>
              <div class="pull-right">{{item.companyPhone}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">税务登记号：</div>
              <div class="pull-right overhiden">{{item.companyTaxNumber}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">开户银行：</div>
              <div class="pull-right overhiden">{{item.bankName}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">开户银行账号：</div>
              <div class="pull-right overhiden">{{item.bankAccount}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">收票人：</div>
              <div class="pull-right">{{item.name}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">联系电话：</div>
              <div class="pull-right">{{item.telephone}}</div>
            </div>

            <div class="invoice-list clearfix">
              <div class="invoice-list-title pull-left">收票地址：</div>
              <div class="pull-right">
                <p>{{item.area}}</p>
                <p>{{item.detailAddress}}</p>
              </div>
            </div>

            <div class="invoice-controll clearfix">
              <div class="pull-left" @click="updateInvoice(item, index)"><i class="iconfont icon-edit" ></i>修改</div>
              <div class="pull-left" @click="deleteInvoice(item, index)"><i class="iconfont icon-lajitong" ></i>删除</div>
            </div>
          </div>
        </div>
      </div>
      <div class="com-none-state" v-if="invoiceList.length === 0">
        <img src="/images/mobile/@2x/empty-collect.png">
        <p>抱歉，暂无发票消息</p>
      </div>
    </div>
    <div class="deleteKuang" v-if="showDeleteAlert" @touchmove="preventTouchMove($event)">
      <div class="kuangContent">
        <div class="title">删除信息</div>
        <div class="titleinfo">是否删除此发票信息</div>
        <!--<div class="info" v-show="isUploadpro">*存在已上架信息</div>-->
        <div class="K_btn">
          <div class="cancelBtn" @click="showDeleteAlert = false">取消</div>
          <div class="answerBtn" @click="deleteFn()">确定</div>
        </div>
      </div>
    </div>
    <mobile-footer></mobile-footer>
    <remind-box :title="collectResult" :timeoutCount="timeoutCount"></remind-box>

   <addinvoice
     :chooseItem="chooseItem"
     @addinvoiceBtn="addinvoiceFn"
     v-if="showAddinvoice"
     :isSaveinvoiceType="$kind"
     :joinType="joinType"
   ></addinvoice>
  </div>
</template>

<script>
  import axios from '~plugins/axios'
  import { RemindBox } from '~components/mobile/common'
  import { addinvoice } from '~components/mobile/center'
  import { MobileFooter } from '~components/mobile'
  export default {
    name: 'invoice-view',
    middleware: 'authenticated',
    layout: 'mobile',
    data() {
      return {
        activeType: '',
        invoiceList: [],
        hasnormal: false, // 是否拥有普票
        showDeleteAlert: false,
        collectResult: '',
        timeoutCount: 0,
        chooseItem: {},
        showAddinvoice: false,
        $kind: 0,
        joinType: 'add'
      }
    },
    async asyncData() {
      let { data } = await axios.get('/trade/bill/list/personal')
      let _arr = []
      data.forEach(obj => {
        if (obj.kind === 1206) {
          _arr.splice(0, 0, obj)
        } else {
          _arr.push(obj)
        }
      })
      let hasnormal = data.find(item => {
        return item.kind === 1206
      })
      return {
        invoiceList: _arr,
        hasnormal: hasnormal
      }
    },
    methods: {
      changeTab(str) {
        this.activeType = str
      },
      deleteInvoice(item, index) {
        this.$item = item
        this.$index = index
        this.showDeleteAlert = true
      },
      async addinvoiceFn(ty, item) {
        this.showAddinvoice = false
        let { data } = await axios.get('/trade/bill/list/personal')
        let _arr = []
        data.forEach(obj => {
          if (obj.kind === 1206) {
            _arr.splice(0, 0, obj)
          } else {
            _arr.push(obj)
          }
        })
        let hasnormal = data.find(item => {
          return item.kind === 1206
        })
        this.invoiceList = _arr
        this.hasnormal = hasnormal
      },
      updateInvoice(item, index) {
        this.joinType = 'update'
        this.chooseItem = item
        this.$kind = 0
        this.showAddinvoice = true
      },
      addInvoiceNew() {
        this.joinType = 'add'
        this.$kind = this.invoiceList.length > 0 ? this.invoiceList[0].kind : 0
        this.chooseItem = {}
        this.showAddinvoice = true
      },
      deleteFn() {
        this.$http.delete(`/trade/bill/delete/${this.$item.id}`).then(res => {
          this.setRemindText('删除成功')
          this.showDeleteAlert = false
          this.invoiceList.splice(this.$index, 1)
        })
      },
      setRemindText(str) {
        this.collectResult = str
        this.timeoutCount++
      }
    },
    components: {
      RemindBox,
      addinvoice,
      MobileFooter
    }
  }
</script>

<style lang="scss" scoped>
  @import '~assets/scss/mobileInvoice';
  @mixin overFlowHidden {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  @mixin lineHeight($value) {
    height: $value;
    line-height: $value;
  }
  .invoice-wrapper {
    background: #f1f3f6;
    /*margin: 1.26rem 0 0 0;*/
    left: 0;
    bottom: 0;
    top: 0;
    right: 0;
    z-index: 11;
    /*padding: 0.82rem 0 1.04rem;*/
    /*height: calc(100vh - 1.26rem);*/
    position: absolute;
    width: 100%;
    .invoice-wrapper-el {
      margin: 2.08rem 0 0.98rem;
      height: calc(100vh - 1.26rem - 0.98rem);
      overflow-y: scroll;
      padding-bottom: 0.98rem;
    }
    /*overflow-y: scroll;*/
    .mobile-header{
      position: fixed;
      top: 0;
      z-index: 10;
      width:100%;
      height: 1.26rem;
      line-height: 1.26rem;
      /*border-bottom:.01rem solid #ccc;*/
      background: #3e82f5;
      padding:0 .2rem 0 .1rem;
      color:#fff;
    }
    .mobile-header p{
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size:.36rem;
      text-align: center;
      width: 6rem;
      padding-left: 1rem;
    }
    .mobile-center-header p.en-name {
      font-size: .3rem;
    }
    .mobile-header a{
      font-size:.28rem;
      color:#fff;
      position: absolute;
    }
    .mobile-header a i{
      font-size: .48rem;
      margin-right: -.1rem;
    }
    .order-nav {
      background: #fff;
      position: fixed;
      top: 1.26rem;
      width: 100%;
      z-index: 10;
      div {
        height: 0.82rem;
        line-height: 0.82rem;
        display: inline-block;
        width: 33.3%;
        text-align: center;
        font-size: .28rem;
        color: #666;
        &.active span{
          color: #3f84f6;
          border-bottom: 0.04rem solid #3f84f6;
          padding-bottom: 0.2rem;
        }
      }
    }
    .invoice-top {
      padding: 0 0.3rem;
      font-weight: bold;
      font-size: 0.28rem;
      color: #333;
      @include lineHeight(0.78rem);
      img {
        width: 0.29rem;
        vertical-align: top;
        height: 0.33rem;
        margin-top: 0.22rem;
        margin-right: 0.04rem;
      }
    }
    .invoice-middle {
      padding-bottom: 0.4rem;
      .invoice-normal {
        font-size: 0.28rem;
        line-height: 0.76rem;
        background: #fff;
        margin: 0 0.3rem;
        color: #333;
        .invoice-list {
          .pull-right {
            max-width: 4.4rem;
            text-align: right;
          }
          .overhiden {
            @include overFlowHidden()
          }
          border-bottom: 1px solid #d9d9d9;
          padding: 0 0.24rem;
          .invoice-list-title {
            color: #666;
            width: 2rem;
            display: inline-block;
            text-align: justify;
            vertical-align: top;
            height: 0.76rem;
            &:after {
              content: '';
              display: inline-block;
              width: 100%;
              overflow: hidden;
              height: 0;
            }
          }
          p {
            line-height: 0.4rem;
          }
          &:nth-last-of-type(2){
            padding: 0.2rem 0.24rem;
          }
          .normal-primary {
            margin-top: 0.24rem;
            color: #15b262;
            font-size: 0.24rem;
            width: 0.7rem;
            @include lineHeight(0.32rem);
            text-align: center;
            border-radius: 0.07rem;
            border: 1px solid #15b262;
            background: #e7f7ef;
          }
        }
      }
      .invoice-speci {
        .invoice-list {
          .normal-primary {
            background: #fff5e5;
            color: #fe9d03;
            border: 1px solid #fe9d03;
          }
        }
      }
      .invoice-controll {
        color: #333;
        text-align: center;
        font-size: 0.26rem;
        @include lineHeight(0.8rem);
        div {
          width: 50%;
          &:nth-child(1) {
            border-right: 1px solid #d9d9d9;
          }
        }
        .icon-edit {
          font-size: 0.34rem;
          vertical-align: top;
          margin-top: 0.02rem;
        }
        .icon-lajitong {
          font-size: 0.35rem;
          vertical-align: top;
          margin-top: 0.02rem;
        }
      }
    }
    .com-none-state {
      background: #fff;
      margin: 0 0.3rem;
      padding-bottom: 2rem;
      width: auto;
    }
    .deleteKuang {
      position: fixed;
      background: rgba(0,0,0,0.5);
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 9999;
      .kuangContent {
        border-radius: 5px;
        background: #fff;
        width: 5rem;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate3d(-50%, -50%, 0);
        overflow: hidden;
        .titleinfo {
          font-size: .3rem;
          color: #666;
          text-align: center;
          margin-top: 0.5rem;
          margin-bottom: 0.1rem;
        }
        .title {
          background: #5078cb;
          height: .7rem;
          line-height: .7rem;
          font-size: .3rem;
          color: #fff;
          text-align: center;
        }
        .info {
          color: #f00;
          text-align: center;
        }
        .K_btn {
          margin-top: 0.4rem;
          line-height: 0.7rem;
          height: 0.7rem;
          &::after{
            clear: both;
            display: block;
            content: ' ';
            visibility: hidden;
            zoom: 1;
          }
          div {
            float: left;
            width: 50%;
            font-size: 0.3rem;
            text-align: center;
            &.cancelBtn {
              background: #b4b5b9;
              color: #333;
            }
            &.answerBtn {
              background: #5078cb;
              color: #fff;
            }
          }
        }
      }
    }
    .mi-remind-area {
      background: #fff;
    }
  }
</style>

