<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-20 09:35:11
 * @LastEditTime : 2019-12-24 15:42:01
 * @LastEditors  : Please set LastEditors
 -->
<template>
  <h-view id="invoice" class="public-style">
    <h-header :proportion="[5,1,1]" class="bar-custom">
      <div slot="left" class="h-header-btn" @click="$routeGo()">
        <img src="@/assets/userBind/arrow.png">
        <span>开票信息</span>
      </div>
    </h-header>
    <list-item :item-height="44">
      <item :showArrow="true">
        <div slot="name" class="font-color">纳税人类型</div>
        <input
          slot="content"
          v-model="invoiceInfo.taxpayer_type_n"
          type="text"
          readonly
          placeholder="请选择"
          @click="selectTaxpayer"
        >
      </item>
      <item :showArrow="true">
        <div slot="name" class="font-color">发票类型</div>
        <input
          slot="content"
          v-model="invoiceInfo.invoice_kind_n"
          type="text"
          readonly
          placeholder="请选择"
          @click="selectInvoiceType"
        >
      </item>
      <item>
        <div slot="name" class="font-color">发票抬头</div>
        <input slot="content" v-model="invoiceInfo.invoice_title" type="text" placeholder="请输入发票抬头" >
      </item>
      <item>
        <div slot="name" class="font-color">发票地址</div>
        <textarea
          slot="content" ref="myTestarea" v-model="invoiceInfo.invoice_adds" cols="22"
          rows="1"
          type="text" placeholder="请输入发票地址" @input="addRows(invoiceInfo.invoice_adds)" />
      </item>
      <item>
        <div slot="name" class="font-color">开户电话</div>
        <input slot="content" v-model="invoiceInfo.invoice_tel" type="text" placeholder="请输入开户电话" >
      </item>
      <item>
        <div slot="name" class="font-color">发票开户行</div>
        <textarea
          slot="content"
          ref="myTestareaLive"
          v-model="invoiceInfo.open_bank"
          cols="22"
          rows="1"
          type="text"
          placeholder="请输入发票开户行"
          @input="addRowsLive(invoiceInfo.open_bank)"
        />
      </item>
      <item>
        <div slot="name" class="font-color">开户行账号</div>
        <input slot="content" v-model="invoiceInfo.open_account" type="text" placeholder="请输入开户行账号" >
      </item>
      <item>
        <div slot="name" class="font-color">纳税人识别号</div>
        <input
          slot="content"
          v-model="invoiceInfo.organization_code" placeholder="纳税人识别号" type="text" readonly
        >
      </item>
    </list-item>
    <bottom-tab class="footer-button">
      <tab-button class="save" @click.native="verified">下一步</tab-button>
    </bottom-tab>
  </h-view>
</template>
<script>
export default {
  data () {
    return {
      bp_id: '',
      invoicList: [],
      taxpayerList: [],
      invoice_kind_n: '',
      invoiceInfo: {
        invoice_kind: '',
        invoice_kind_n: '',
        taxpayer_type: '',
        invoice_title: '',
        taxpayer_type_n: '',
        organization_code: '',
        invoice_adds: '',
        invoice_tel: '',
        open_bank: '',
        open_account: '',
      },
    }
  },
  watch: {},
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (from.name === 'EnterpriseNew') {
        vm.invoiceInfo = {
          invoice_kind: '',
          invoice_kind_n: '',
          taxpayer_type: '',
          invoice_title: '',
          taxpayer_type_n: '',
          organization_code: '',
          invoice_adds: '',
          invoice_tel: '',
          open_bank: '',
          open_account: '',
        }
        vm.invoiceInfo.invoice_title = JSON.parse(window.localStorage.getItem('handleUser')).bp_name
        vm.invoiceInfo.organization_code = JSON.parse(window.localStorage.getItem('handleUser')).organization_code
        vm.bp_id =JSON.parse(window.localStorage.getItem('handleUser')).bp_id
        vm.invoiceQuery() // 开票信息查询
      }

      /*    if (from.name === 'EnterpriseNew') {
        vm.invoiceInfo = {
          invoice_kind: '',
          invoice_kind_n: '',
          taxpayer_type: '',
          invoice_title: vm.$route.params.bp_name,
          taxpayer_type_n: '',
          organization_code: '',
          invoice_adds: '',
          invoice_tel: '',
          open_bank: '',
          open_account: '',
        }
        // vm.invoiceInfo.invoice_title = vm.$route.params.bp_name
        vm.bp_id = vm.$route.params.bp_id
        vm.invoiceInfo.organization_code = vm.$route.params.organization_code
        vm.getInvoiceType()
        vm.getTaxpayer()
        if (vm.$route.params.from) {
          vm.invoiceQuery() // 开票信息查询
        }
        vm.addRows(this.invoiceInfo.invoice_adds)
        vm.addRowsLive(this.invoiceInfo.open_bank)
      } */
    })
  },
  created () {
    let vm = this
    vm.getInvoiceType()
    vm.getTaxpayer()
  },
  methods: {
    invoiceQuery () {
      let vm = this
      let url = process.env.basePath + 'bp_invoice_query'
      let param = {
        bp_id: vm.bp_id,
      }
      vm.hlsPopup.showLoading('请稍候')
      vm.hlsHttp.post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          console.log('invoiceInfo', res.info)
          vm.invoiceInfo = res.info
          vm.invoiceInfo.invoice_title = JSON.parse(window.localStorage.getItem('handleUser')).bp_name
          // Object.assign(vm.invoiceInfo, res.info)
          vm.addRows(vm.invoiceInfo.invoice_adds)
          vm.addRowsLive(vm.invoiceInfo.open_bank)
        }
      })
    },

    addRows (e) {
      if (e) {
        // let addNum = Math.ceil(e.length / 16)
        // if (addNum === 0) {
        //   this.$refs.myTestarea.rows = 1
        // } else if (addNum >= addNum - 1 && addNum < addNum + 1) {
        //   this.$refs.myTestarea.rows = addNum + 1
        // }
        let len = 0
        for (let i = 0; i < e.length; i++) {
          var c = e.charCodeAt(i)
          // 统计字符串的字符长度
          if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
            len += 1
          } else {
            len += 2
          }
        }
        let addNum = Math.ceil(len / 21)
        this.$refs.myTestarea.rows = addNum
      }
    },
    addRowsLive (e) {
      if (e) {
        // let addNum = Math.ceil(e.length / 16)
        // if (addNum === 0) {
        //   this.$refs.myTestareaLive.rows = 1
        // } else if (addNum >= addNum - 1 && addNum < addNum + 1) {
        //   this.$refs.myTestareaLive.rows = addNum + 1
        // }
        let len = 0
        for (let i = 0; i < e.length; i++) {
          var c = e.charCodeAt(i)
          // 统计字符串的字符长度
          if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
            len += 1
          } else {
            len += 2
          }
        }
        let addNum = Math.ceil(len / 21)
        this.$refs.myTestareaLive.rows = addNum
      }
    },
    getInvoiceType () {
      let vm = this
      let url = process.env.basePath + 'bp_invoice_kind_query'
      let param = {}
      vm.hlsPopup.showLoading('请稍候')
      vm.hlsHttp.post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          vm.invoicList = res.lists.map(item => {
            return {
              code: item.invoice_kind,
              code_name: item.invoice_kind_n,
            }
          })
        }
      })
    },
    getTaxpayer () {
      let vm = this
      let url = process.env.basePath + 'bp_taxpayer_type_query'
      let param = {}
      vm.hlsPopup.showLoading('请稍候')
      vm.hlsHttp.post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          vm.taxpayerList = res.lists.map(item => {
            return {
              code: item.taxpayer_type,
              code_name: item.taxpayer_type_n,
            }
          })
        }
      })
    },
    selectTaxpayer (e) {
      let vm = this
      console.log('vm.taxpayerList', vm.taxpayerList)
      vm.hlsPopup.selectList({
        list: vm.taxpayerList,
        code: 'bp_type',
        object: {},
        returnItem: function (index, obj) {
          vm.$set(vm.invoiceInfo, 'taxpayer_type', obj.bp_type)
          vm.$set(vm.invoiceInfo, 'taxpayer_type_n', obj.bp_type_n)
          /* vm.invoiceInfo.taxpayer_type = obj.bp_type
          vm.invoiceInfo.taxpayer_type_n = obj.bp_type_n */
          console.log('obj', vm.invoiceInfo)
        },
      })
    },
    selectInvoiceType (e) {
      let vm = this
      vm.hlsPopup.selectList({
        list: vm.invoicList,
        code: 'bp_type',
        object: {},
        returnItem: function (index, obj) {
          vm.$set(vm.invoiceInfo, 'invoice_kind', obj.bp_type)
          vm.$set(vm.invoiceInfo, 'invoice_kind_n', obj.bp_type_n)
          /* vm.invoiceInfo.invoice_kind = obj.bp_type
          vm.invoiceInfo.invoice_kind_n = obj.bp_type_n */
        },
      })
    },
    verified () {
      // console.log('bpid', this.$route.params.bp_id)
      // this.$router.push({
      //   name: 'Enclosure',
      //   params: {
      //     bp_id: this.bp_id,
      //   },
      // })
      let vm = this
      let url = process.env.basePath + 'bp_invoice_save' 
        let roleList = JSON.parse(window.localStorage.getItem('handleUser')).multiRoleList
      let name = roleList.some(item => ['TENANT', 'GUTA'].includes(item)) ? 'Enclosure' : 'BankInfo';

      // let name = ['TENANT', 'GUTA'].includes(JSON.parse(window.localStorage.getItem('handleUser')).bp_type)
      //    ? 'Enclosure' : 'BankInfo'
         
      let param = {
        bp_id: vm.bp_id,
        bp_class: 'ORG',
        ...vm.invoiceInfo,
      }
      delete param.taxpayer_type_n
      delete param.invoice_kind_n
      vm.hlsPopup.showLoading('请稍候')
      vm.hlsHttp.post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          vm.$router.push({
            name: name,
            params: {
              bp_id: vm.bp_id,
            },
          })
        }
      })
    },
  },
}
</script>
<style lang='less'>
#invoice {
 input::placeholder{
    color: #B4B4B5;
}
.font-color {
        color: #656464;
    }
     textarea::placeholder {
    color: #b4b4b5;
  }
  textarea {
    text-align: right;
    color:#666;
    resize: none;
  }
  .footer-button {
      padding-top: 15px;
      padding-bottom: 15px;
    }
    .save {
      width: 100%;
      height: 45px;
      color: #fff;
      border-radius: 4px;
      background-color: @headerColor;
    }
}
</style>
