<template>
  <h-view class="public-style partner-mainten" title="客户信息">
    <!--<h-header :has-border="false" class="bar-custom">
      <div slot="left" class="h-header-btn" @click="$routeGo()">
        <i class="ion-ios-arrow-back"/>
      </div>
      <div slot="center">客户信息</div>
      <div v-show="index===0 || index===2" slot="right" class="h-header-btn" @click="scanCard()">
        <i class="icon ion-qr-scanner" style="font-size: 0.4rem"/>
      </div>
    </h-header>-->
    <s-tab @tabClick="changeData">
      <tab-item>基本信息</tab-item>
      <tab-item>开票信息</tab-item>
      <tab-item>银行信息</tab-item>
    </s-tab>
    <h-content>
      <section v-show="tabIndex[0]">
        <list-item>
          <item>
            <section slot="name">客户名称</section>
            <section slot="content">
              {{ bpMaster.bp_name }}
            </section>
          </item>
          <item>
            <section slot="name">客户类型</section>
            <section slot="content">
              {{ bpMaster.bp_type_n }}
            </section>
          </item>
          <item>
            <section slot="name">客户分类</section>
            <section slot="content">
              {{ bpMaster.bp_class_n }}
            </section>
          </item>
        </list-item>
        <list-item>
          <item>
            <img slot="left-icon" src="@/assets/image/bpMaster/user@2x.png" class="left-icon">
            <section slot="name">工商信息</section>
          </item>
          <item>
            <section slot="name" class="required">法人姓名</section>
            <input
              slot="content" v-model="bpMaster.corporate_code" type="text" placeholder="请输入法人姓名">
          </item>
          <item>
            <section slot="name" class="required">联系电话</section>
            <input
              slot="content" v-model="bpMaster.cell_phone" type="text"
              placeholder="请输入联系电话">
          </item>
          <item>
            <section slot="name" class="required">组织机构代码</section>
            <input
              slot="content" v-model="bpMaster.organization_code" type="text" required
              placeholder="请输入组织机构代码">
          </item>
          <item>
            <section slot="name">营业执照号</section>
            <input
              slot="content" v-model="bpMaster.business_license_num" type="text"
              placeholder="请输入营业执照号">
          </item>
          <item>
            <section slot="name">税务登记号</section>
            <input
              slot="content" v-model="bpMaster.tax_registry_num" type="text"
              placeholder="请输入税务登记号">
          </item>
          <item :show-arrow="true">
            <section slot="name">注册时间</section>
            <input
              slot="content" v-model="bpMaster.founded_date" type="text"
              placeholder="请输入注册时间" readonly @click="foundedDateClick">
          </item>
          <item>
            <section slot="name">注册资本(万元)</section>
            <input
              slot="content" v-model="bpMaster.registered_capital" type="number"
              placeholder="请输入注册资本">
          </item>
          <item>
            <section slot="name">经营地址</section>
            <input
              slot="content" v-model="bpMaster.work_unit_address" type="text"
              placeholder="请输入经营地址">
          </item>

        </list-item>
      </section>
      <section v-show="tabIndex[1]">
        <list-item>
          <item :show-arrow="true">
            <section slot="name" class="">纳税人类型</section>
            <input
              slot="content" v-model="bpMaster.taxpayer_type_n" type="text" placeholder="请输入账号名称"
              readonly @click="taxpayerTypeClick">
          </item>
          <item>
            <section slot="name" class="">发票抬头</section>
            <input slot="content" v-model="bpMaster.invoice_title" type="text" placeholder="请输入发票抬头">
          </item>
          <item>
            <section slot="name" class="">发票地址及电话</section>
            <input slot="content" v-model="bpMaster.invoice_bp_address_phone_num" type="text" placeholder="请输入发票地址及电话">
          </item>
          <item>
            <section slot="name" class="">发票开户行及账号</section>
            <input slot="content" v-model="bpMaster.invoice_bp_bank_account" type="text" placeholder="请输入发票开户行及账号">
          </item>

        </list-item>
      </section>
      <section v-show="tabIndex[2]">
        <list-item>
          <item>
            <img slot="left-icon" src="@/assets/image/bpMaster/bank-card@2x.png" class="left-icon">
            <section slot="name">银行信息</section>
          </item>
          <item>
            <section slot="name" class="">账号名称</section>
            <input slot="content" v-model="bpAccount.bank_account_name" type="text" placeholder="请输入账号名称">
          </item>
          <item>
            <section slot="name" class="">银行账号</section>
            <input
              slot="content" v-model="bpAccount.bank_account_num" type="text" placeholder="请输入银行账号">
          </item>
          <item>
            <section slot="name" class="">银行名称</section>
            <input slot="content" v-model="bpAccount.bank_full_name" type="text" placeholder="请输入银行名称">
          </item>
          <item>
            <section slot="name" class="">支行名称</section>
            <input slot="content" v-model="bpAccount.bank_branch_name" type="text" placeholder="请输入支行名称">
          </item>
        </list-item>
      </section>
    </h-content>
    <bottom-tab>
      <tab-button @click.native="save">
        保存
      </tab-button>
    </bottom-tab>
  </h-view>
</template>

<script>
export default {
  name: 'PartnerOrgMainten',
  data () {
    return {
      index: 0,
      tabIndex: [1, 0, 0],
      bpMaster: {
        bp_id: '',
        bp_code: '',
        bp_name: '',
        bp_type: '',
        bp_type_n: '',
        bp_class: '',
        bp_class_n: '',
        bp_category: '',
        bp_category_n: '',
        province_id: null,
        province_id_n: null,
        city_sp: null,
        city_sp_n: null,
        corporate_code: '',
        cell_phone: '',
        organization_code: '',
        business_license_num: '',
        tax_registry_num: '',
        founded_date: '',
        registered_capital: '',
        taxpayer_type: '',
        taxpayer_type_n: '',
        invoice_title: '',
        invoice_bp_address_phone_num: '',
        invoice_bp_bank_account: '',
        contact_person: '',
        phone_extra: '',
        work_unit_address: '',
      },
      bpAccount: {
        bp_id: null,
        bank_account_id: null,
        bank_account_name: null,
        bank_account_num: null,
        bank_full_name: null,
        bank_branch_name: null,
      },
      taxpayerTypeList: [
        {
          'code': 'GENERAL_TAXPAYER',
          'code_name': '一般纳税人',
        },
        {
          'code': 'SMALL_SCALE_TAXPAYER',
          'code_name': '小规模纳税人',
        },
      ],
    }
  },
  watch: {
  },
  mounted () {
    this.bpAssign()
    this.load()
  },
  methods: {
    bpAssign () {
      let vm = this
      Object.assign(vm.bpMaster, vm.$route.params.bpData)
    },
    changeData (index) {
      let vm = this
      vm.index = index
      vm.tabIndex = [0, 0, 0]
      vm.tabIndex[index] = 1
    },

    /**
       * 加载时调用
       */
    load () {
      let vm = this
      /**
         * 加载商业伙伴
         * 和银行信息
         */
      if (vm.bpMaster.bp_id) {
        let url1 = $config.basePath + 'query_bp_master'
        let param1 = {
          'bp_id': vm.bpMaster.bp_id,
        }
        vm.hlsPopup.showLoading('请稍候')
        vm.hlsHttp.post(url1, param1).then(function (res) {
          vm.hlsPopup.hideLoading()
          if (res.result === 'S') {
            // 商业伙伴
            vm.bpMaster = Object.assign(vm.bpMaster, res.bpMaster_list[0])
            // 如果有银行信息则赋值
            if (res.account_list.length !== 0) {
              vm.bpAccount = Object.assign(vm.bpAccount, res.account_list[0])
            }
          }
        })
      }
    },

    taxpayerTypeClick () {
      let vm = this
      vm.hlsPopup.selectList({
        list: vm.taxpayerTypeList,
        code: 'taxpayer_type',
        object: vm.bpMaster,
        returnItem: function (index, obj) {
          vm.bpMaster = obj
        },
      })
    },

    foundedDateClick () {
      let vm = this
      vm.hlsPopup.showTime({
        nowDate: ' ',
        format: 'YYYY-MM-DD',
        callback: (date) => {
          vm.bpMaster.founded_date = date
        },
      })
    },

    // 保存函数
    save () {
      let vm = this
      if (!vm.bpMaster.corporate_code || !vm.bpMaster.organization_code) {
        vm.hlsPopup.showLongBottom('必输字段不能为空!')
      } else {
        let url = $config.basePath + 'bp_master_save'
        vm.bpMaster.user_id = window.localStorage.user_id
        let param = {
          master: vm.bpMaster,
        }
        vm.hlsPopup.showLoading('请稍候')
        vm.hlsHttp.post(url, param).then(function (res) {
          if (res.result === 'S') {
            vm.bpMaster.bp_id = res.resp_master[0].bp_id
            vm.bpAccount.bp_id = res.resp_master[0].bp_id
            // 调用银行信息保存;
            vm.saveBpAccount(vm.bpAccount)
            // hlsPopup.hideLoading();
          }
        })
      }
    },

    /**
       * 保存商业伙伴银行信息
       */
    saveBpAccount (account) {
      let vm = this
      let url = $config.basePath + 'bp_master_account_save'
      account.user_id = window.localStorage.user_id
      let param = {
        account: account,
      }
      hlsHttp.post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          vm.bpAccount.bank_account_id = res.resp_account[0].bank_account_id
          vm.hlsPopup.showLongBottom('保存成功')
        }
      })
    },
  },
}
</script>

<style lang="less">
  .partner-mainten {
    .hls-switch-tab-top {
      position: relative;
    }
    .content{
      padding-bottom: 100px;
    }
    .hls-item {
      .contents {
        .add-content {
          .right-icon {
            //transform: rotate(90deg);
          }
        }
      }
    }

    .bottom-tab-button {
      background-color: @headerColor;
      color: #fff;
    }
  }

</style>
