<template>
  <div id="companyInfoConfirm">
    <div>
      <div class="edit_layout">
        <div style="flex: 1">
          <span>企业名称：</span>
          <span>{{ entName }}</span>
        </div>
        <div style="flex: 1">
          <span>纳税人识别号：</span>
          <span>{{ entTaxId }}</span>
        </div>
      </div>
      <div class="edit_layout">
        <div style="flex: 1">
          <span>企业地址：</span>
          <el-input v-model.trim="entAddress" placeholder="请输入企业地址" class="ent_input"></el-input>
        </div>
        <div style="flex: 1">
          <span>企业电话：</span>
          <el-input v-model.trim="entPhone" placeholder="请输入企业电话" class="ent_input"></el-input>
          <br/>
        </div>
      </div>
      <div class="edit_layout">
        <div style="flex: 1">
          <span>银行名称：</span>
          <el-input v-model.trim="bankName" placeholder="请输入银行户名" class="ent_input"></el-input>
          <br/>
        </div>
        <div style="flex: 1">
          <span>银行账号：</span>
          <el-input
              v-model.trim="bankAccountNumber"
              placeholder="请输入银行账号"
              class="ent_input"
          ></el-input>
          <br/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { FindEntInfoByCurrentEnt } from '@/api/ent_info'
import { GetDeviceLabel } from '@/api/ent_device'
import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

export default {
  name: 'companyInfoConfirm',
  data() {
    return {
      loading: false,
      entName: '',
      entShortName: '',
      entTaxId: '',
      bankName: '',
      entType: '',
      bankAccountNumber: '',
      entAddress: '',
      payees: '',
      entPhone: '',
      reviewers: '',
      entContactPerson: '',
      availableTaxes: '',
      defaultInvoiceDevice: '',
      dialogVisible: false,
      options: [
        {
          value: 1,
          label: '一般纳税人'
        },
        {
          value: 2,
          label: '小规模纳税人'
        },
        {
          value: 3,
          label: '起征点以下纳税人'
        }
      ],
      entTaxModal: [],
      token: '',
      account: '',
      deviceList: [],
      entTax: [],
      defaultInvoiceDeviceKey: ''
    }
  },
  created() {
    this.callFindEntInfoByCurrentEnt()
  },
  methods: {
    // 根据用户当前选中的税号查询企业信息
    callFindEntInfoByCurrentEnt() {
      this.loading = true
      FindEntInfoByCurrentEnt()
          .then((val) => {
            this.loading = false
            this.entName = val.data.entName
            this.entShortName = val.data.entShortName
            this.entTaxId = val.data.entTaxId
            this.bankName = val.data.bankName
            this.entType = val.data.entType
            this.bankAccountNumber = val.data.bankAccountNumber
            this.entAddress = val.data.entAddress
            this.payees = val.data.payees
            this.entPhone = val.data.entPhone
            this.reviewers = val.data.reviewers
            this.entContactPerson = val.data.entContactPerson
            this.availableTaxes = val.data.availableTaxes
            if (
                val.data.availableTaxes !== '' ||
                val.data.availableTaxes != null
            ) {
              this.entTax = val.data.availableTaxes.split(',')
            }
            this.defaultInvoiceDevice = GetDeviceLabel(
                val.data.defaultInvoiceDevice
            )
            this.defaultInvoiceDeviceKey =
                val.data.defaultInvoiceDevice
          })
          .catch((err) => {
            this.loading = false
            this.$message.error(err)
          })
    },
    // 编辑企业信息
    async entEditSave() {
      let user = JSON.parse(cache.getSession('user'))
      let token = cache.getToken()
      let taxRate = ''
      if (this.entAddress === '' || this.entAddress == null) {
        this.$message({
          message: '企业地址不能为空！',
          type: 'warning'
        })
        return
      }
      if (this.entTax.length <= 0) {
        this.$message({
          message: '企业可用税率不能为空！',
          type: 'warning'
        })
        return
      } else {
        taxRate = this.entTax.join(',')
      }
      if (user.account && token) {
        const url = '/sys/entInfo/updateBriefInfo'
        let response = await AxiosRequestData(
            url,
            token,
            {
              entTaxId: this.entTaxId,
              entName: this.entName,
              entType: this.entType,
              entAddress: this.entAddress,
              availableTaxes: taxRate,
              entContactPerson: this.entContactPerson,
              entPhone: this.entPhone,
              bankName: this.bankName,
              bankAccountNumber: this.bankAccountNumber,
              payees: this.payees,
              reviewers: this.reviewers,
              reqChannel: 4,
              mobile: user.account,
              entShortName: this.entShortName,
              defaultInvoiceDevice: this.defaultInvoiceDeviceKey,
              doConfirm: true
            },
            false,
            1
        )
        if (response) {
          if (response.code === 0) {
            this.dialogVisible = false
            await this.getDefaultCompany(user.account, token)
            this.callFindEntInfoByCurrentEnt()
            this.$attrs.companyInfo.companyInfoShow()
            this.$message({
              message: '开票信息确认成功！',
              type: 'success'
            })
          } else {
            this.$message.error('开票信息确认失败！')
            this.dialogVisible = false
          }
        }
      }
    },
    // 获取默认企业信息
    async getDefaultCompany(account, token) {
      if (account && token) {
        const url = '/auth/comm/user/findDefaultChoose'
        let response = await AxiosRequestData(
            url,
            token,
            {
              mobile: account
            },
            false,
            2
        )
        if (response) {
          if (response.code === 0) {
            this.companyName = response.data.entName
            cache.setSession('company', response.data)
            console.log('企业信息保存')
            console.log(response.data)
          }
        }
      }
    }
  }
}
</script>

<style scoped>
.ent_input {
  width: 250px;
}

.edit_layout {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
</style>
