<template>
  <div id="enterprise_info_change">
    <div class="edit_layout">
      <div style="flex: 1">
        <span style="color: red">*</span>
        <span>企业名称：</span>
        <el-input v-model="entInfo.entName" placeholder="请输入企业名称" class="ent_input"></el-input>
      </div>
      <div style="flex: 1">
        <span style="color: red">*</span>
        <span>企业税号：</span>
        <el-input v-model="entInfo.entTaxId" disabled="disabled" placeholder="请输入企业税号" class="ent_input"></el-input>
      </div>
    </div>
    <div class="edit_layout">
      <div style="flex: 1">
        <span style="color: red">*</span>
        <span>企业类型：</span>
        <el-select v-model="entInfo.entType" placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          ></el-option>
        </el-select>
      </div>
    </div>
  </div>
</template>

<script>
import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

export default {
  name: 'enterprise_info_change',
  data() {
    return {
      options: [
        {
          value: 1,
          label: '一般纳税人'
        },
        {
          value: 2,
          label: '小规模纳税人'
        },
        {
          value: 3,
          label: '起征点以下纳税人'
        }
      ],
      entType: '',
      entInfo: {},
      entTax: [],
      account: '',
      token: ''
    }
  },
  created() {
    let user = JSON.parse(cache.getLocal('user'))
    this.account = user.account
    this.token = cache.getToken()
  },
  methods: {
    // 获取企业信息
    getEntInfo() {
      this.entInfo = this.$attrs.enterpriseInfoChange.rowInfo
      if (this.entInfo.availableTaxes !== '' || this.entInfo.availableTaxes != null) {
        this.entTax = this.entInfo.availableTaxes.split(',')
      }
    },
    // 修改企业信息
    async entEditSave() {
      let taxRate = ''
      if (this.entInfo.entType === '' || this.entInfo.entType == null) {
        this.$message({
          message: '企业类型不能为空！',
          type: 'warning'
        })
        return
      }
      if (this.entInfo.entShortName === '' || this.entInfo.entShortName == null) {
        this.$message({
          message: '企业简称不能为空！',
          type: 'warning'
        })
        return
      } else {
        if (this.entInfo.entShortName.length > 9) {
          this.$message({
            message: '企业简称长度不能大于9为字符！',
            type: 'warning'
          })
          return
        }
      }
      if (this.entInfo.entAddress === '' || this.entInfo.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 (this.account && this.token) {
        const url = '/sys/entInfo/updateBriefInfo'
        let response = await AxiosRequestData(
            url,
            this.token,
            {
              entTaxId: this.entInfo.entTaxId,
              entName: this.entInfo.entName,
              entType: this.entInfo.entType,
              entAddress: this.entInfo.entAddress,
              availableTaxes: taxRate,
              entContactPerson: this.entInfo.entContactPerson,
              entPhone: this.entInfo.entPhone,
              bankName: this.entInfo.entBankName,
              bankAccountNumber: this.entInfo.bankAccountNumber,
              payees: this.entInfo.payees,
              reviewers: this.entInfo.reviewers,
              reqChannel: 4,
              mobile: this.account,
              entShortName: this.entInfo.entShortName,
              defaultInvoiceDevice: this.entInfo.defaultInvoiceDevice
            },
            false,
            1
        )
        if (response) {
          if (response.code === 0) {
            this.$message({
              message: '企业信息编辑成功！',
              type: 'success'
            })
            await this.$attrs.enterpriseInfoChange.entInfoPageClose()
          } else {
            this.$message.error('企业信息编辑失败！')
          }
        }
      }
    }
  }
}
</script>

<style scoped>
.body {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.ent_input {
  width: 250px;
}

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