<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    width="1000px"
    showFooter
    :cancelText="t('common.cancelText', '取消')"
    :okText="t('common.okText', '确定')"
    @ok="handleSubmit"
    :closeFunc="onClose">
    <template #title>
      <a-space :size="10">
        <div class="text-16px font-medium">{{ title }}</div>
        <a-space-compact size="small" block v-if="dataForm.id">
          <a-tooltip :title="t('common.prevRecord')">
            <a-button size="small" :disabled="getPrevDisabled" @click="handlePrev">
              <i class="icon-ym icon-ym-caret-left text-10px"></i>
            </a-button>
          </a-tooltip>
          <a-tooltip :title="t('common.nextRecord')">
            <a-button size="small" :disabled="getNextDisabled" @click="handleNext">
              <i class="icon-ym icon-ym-caret-right text-10px"></i>
            </a-button>
          </a-tooltip>
        </a-space-compact>
      </a-space>
    </template>
    <template #insertFooter>
      <div class="float-left mt-5px">
        <yunzhupaasCheckboxSingle v-model:value="submitType" :label="continueText" />
      </div>
    </template>
    <a-row class="p-10px dynamic-form">
      <!-- 表单 -->
      <a-form
        :colon="false"
        size="middle"
        layout="horizontal"
        labelAlign="right"
        :labelCol="{ style: { width: '100px' } }"
        :model="dataForm"
        :rules="dataRule"
        ref="formRef">
        <a-row :gutter="15">
          <!-- 具体表单 -->
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="company_code">
              <template #label>企业编码 </template>
              <YunzhupaasInput
                v-model:value="dataForm.company_code"
                @change="changeData('company_code', -1)"
                placeholder="系统自动生成"
                readonly
                :style="{ width: '100%' }">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="company_name">
              <template #label>企业名称 </template>
              <YunzhupaasInput
                v-model:value="dataForm.company_name"
                @change="changeData('company_name', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.company_name"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="short_name">
              <template #label>简称/昵称 </template>
              <YunzhupaasInput
                v-model:value="dataForm.short_name"
                @change="changeData('short_name', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.short_name"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="credit_code">
             <template #label>  {{ dataForm.entity_type === 'IND' ? '身份证' : '社会信用代码' }} </template>
              <YunzhupaasInput
                v-model:value="dataForm.credit_code"
                @change="changeData('credit_code', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.credit_code"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="tax_type">
              <template #label>纳税人类别 </template>
              <YunzhupaasSelect
                v-model:value="dataForm.tax_type"
                @change="changeData('tax_type', -1)"
                placeholder="请选择"
                :templateJson="state.interfaceRes.tax_type"
                :allowClear="true"
                :style="{ width: '100%' }"
                :showSearch="false"
                :options="optionsObj.tax_typeOptions"
                :fieldNames="optionsObj.tax_typeProps">
              </YunzhupaasSelect>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="org_id">
              <template #label>归属组织 </template>
              <YunzhupaasOrganizeSelect
                v-model:value="dataForm.org_id"
                @change="changeData('org_id', -1)"
                placeholder="请选择"
                :allowClear="true"
                :style="{ width: '100%' }"
                :showSearch="false"
                selectType="all">
              </YunzhupaasOrganizeSelect>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="province_id">
              <template #label>所属地区 </template>
              <YunzhupaasAreaSelect
                v-model:value="dataForm.province_id"
                @change="changeData('province_id', -1)"
                placeholder="请选择"
                :allowClear="true"
                :style="{ width: '100%' }"
                :showSearch="false"
                :level="2">
              </YunzhupaasAreaSelect>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="enterprise_nature">
              <template #label>企业类型 </template>
              <YunzhupaasSelect
                v-model:value="dataForm.enterprise_nature"
                @change="changeData('enterprise_nature', -1)"
                placeholder="请选择"
                :templateJson="state.interfaceRes.enterprise_nature"
                :allowClear="true"
                :style="{ width: '100%' }"
                :showSearch="false"
                :options="optionsObj.enterprise_natureOptions"
                :fieldNames="optionsObj.enterprise_natureProps">
              </YunzhupaasSelect>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="industry_code">
              <template #label>行业代码 </template>
              <YunzhupaasSelect
                v-model:value="dataForm.industry_code"
                @change="changeData('industry_code', -1)"
                placeholder="请选择"
                :templateJson="state.interfaceRes.industry_code"
                :allowClear="true"
                :style="{ width: '100%' }"
                :showSearch="false"
                :options="optionsObj.industry_codeOptions"
                :fieldNames="optionsObj.industry_codeProps">
              </YunzhupaasSelect>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="enterprise_scale">
              <template #label>企业规模 </template>
              <YunzhupaasSelect
                v-model:value="dataForm.enterprise_scale"
                @change="changeData('enterprise_scale', -1)"
                placeholder="请选择"
                :templateJson="state.interfaceRes.enterprise_scale"
                :allowClear="true"
                :style="{ width: '100%' }"
                :showSearch="false"
                :options="optionsObj.enterprise_scaleOptions"
                :fieldNames="optionsObj.enterprise_scaleProps">
              </YunzhupaasSelect>
            </a-form-item>
          </a-col>

          <a-col :span="8" class="ant-col-item" v-if="dataForm.entity_type=='ORG'">
            <a-form-item name="registration_date">
              <template #label>成立日期 </template>
              <YunzhupaasDatePicker
                v-model:value="dataForm.registration_date"
                @change="changeData('registration_date', -1)"
                placeholder="请选择"
                :allowClear="true"
                :style="{ width: '100%' }"
                format="yyyy-MM-dd"
                :startTime="getRelationDate(false, 1, 1, '', '')"
                :endTime="getRelationDate(false, 1, 1, '', '')">
              </YunzhupaasDatePicker>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="registered_capital">
              <template #label>注册资本 </template>
              <YunzhupaasInputNumber
                v-model:value="dataForm.registered_capital"
                @change="changeData('registered_capital', -1)"
                placeholder="请输入"
                :style="{ width: '100%' }"
                :step="1"
                addonAfter="万元"
                :controls="false">
              </YunzhupaasInputNumber>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="legal_representative">
              <template #label>法定代表人 </template>
              <YunzhupaasInput
                v-model:value="dataForm.legal_representative"
                @change="changeData('legal_representative', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.legal_representative"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="phone">
              <template #label>联系电话 </template>
              <YunzhupaasInput
                v-model:value="dataForm.phone"
                @change="changeData('phone', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.phone"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="email">
              <template #label>邮箱 </template>
              <YunzhupaasInput
                v-model:value="dataForm.email"
                @change="changeData('email', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.email"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="website">
              <template #label>网站 </template>
              <YunzhupaasInput
                v-model:value="dataForm.website"
                @change="changeData('website', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.website"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="address">
              <template #label>地址 </template>
              <YunzhupaasInput
                v-model:value="dataForm.address"
                @change="changeData('address', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :maskConfig="maskConfig.address"
                :showCount="false">
              </YunzhupaasInput>
            </a-form-item>
          </a-col>
          <a-col :span="8" class="ant-col-item">
            <a-form-item name="yunzhupaas_mdm_corporation_yunzhupaas_major_person_id">
              <template #label>负责人 </template>
              <YunzhupaasUserSelect
                v-model:value="dataForm.yunzhupaas_mdm_corporation_yunzhupaas_major_person_id"
                @change="changeData('yunzhupaas_mdm_corporation_yunzhupaas_major_person_id', -1)"
                placeholder="请选择"
                :allowClear="true"
                :style="{ width: '100%' }"
                selectType="all">
              </YunzhupaasUserSelect>
            </a-form-item>
          </a-col>
          <a-col :span="24" class="ant-col-item">
            <a-form-item name="business_scope">
              <template #label>经营范围 </template>
              <YunzhupaasTextarea
                v-model:value="dataForm.business_scope"
                @change="changeData('business_scope', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :autoSize="{ minRows: 2, maxRows: 10 }"
                :showCount="false">
              </YunzhupaasTextarea>
            </a-form-item>
          </a-col>
          <a-col :span="24" class="ant-col-item">
            <a-form-item name="remark">
              <template #label>备注 </template>
              <YunzhupaasTextarea
                v-model:value="dataForm.remark"
                @change="changeData('remark', -1)"
                placeholder="请输入"
                :allowClear="true"
                :style="{ width: '100%' }"
                :autoSize="{ minRows: 2, maxRows: 10 }"
                :showCount="false">
              </YunzhupaasTextarea>
            </a-form-item>
          </a-col>
          <a-col :span="24" class="ant-col-item">
            <a-tabs v-model:activeKey="state.activetabformItem509c12" tabPosition="top" class="mb-20">
              <a-tab-pane tab="发票抬头" key="1" forceRender>
                <a-row :gutter="15">
                  <a-col :span="24" class="ant-col-item mb-20px">
                    <a-form-item>
                      <a-table
                        :data-source="dataForm.company_invoiceList"
                        :columns="company_invoiceColumns"
                        size="small"
                        :pagination="false"
                        :scroll="{ x: 'max-content' }"
                        :rowSelection="getcompany_invoiceRowSelection"
                        rowKey="yunzhupaasId">
                        <template #headerCell="{ column }">
                          <span class="required-sign" v-if="column.required">*</span>
                          {{ column.title }}
                          <BasicHelp :text="column.tipLabel" v-if="column.tipLabel && column.title" />
                        </template>
                        <template #bodyCell="{ column, index, record }">
                          <template v-if="column.key === 'index'">{{ index + 1 }}</template>
                          <template v-if="column.key === 'title_code'">
                            <YunzhupaasInput
                              v-model:value="record.title_code"
                              @change="changeData('company_invoicetitle_code', index)"
                              placeholder="系统自动生成"
                              readonly
                              :style="{ width: '100%' }">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'title_name'">
                            <YunzhupaasInput
                              v-model:value="record.title_name"
                              @change="changeData('company_invoicetitle_name', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoicetitle_name"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'credit_code'">
                            <YunzhupaasInput
                              v-model:value="record.credit_code"
                              @change="changeData('company_invoicecredit_code', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoicecredit_code"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'tax_type'">
                            <YunzhupaasSelect
                              v-model:value="record.tax_type"
                              @change="changeData('company_invoicetax_type', index)"
                              placeholder="请选择"
                              :templateJson="state.interfaceRes.company_invoicetax_type"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :showSearch="false"
                              :options="optionsObj.company_invoicetax_typeOptions"
                              :fieldNames="optionsObj.company_invoicetax_typeProps">
                            </YunzhupaasSelect>
                          </template>
                          <template v-if="column.key === 'address'">
                            <YunzhupaasInput
                              v-model:value="record.address"
                              @change="changeData('company_invoiceaddress', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoiceaddress"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'phone'">
                            <YunzhupaasInput
                              v-model:value="record.phone"
                              @change="changeData('company_invoicephone', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoicephone"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'bank_name'">
                            <YunzhupaasInput
                              v-model:value="record.bank_name"
                              @change="changeData('company_invoicebank_name', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoicebank_name"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'bank_account'">
                            <YunzhupaasInput
                              v-model:value="record.bank_account"
                              @change="changeData('company_invoicebank_account', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoicebank_account"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'is_defalut'">
                            <YunzhupaasSwitch v-model:value="record.is_defalut" @change="changeData('company_invoiceis_defalut', index)"> </YunzhupaasSwitch>
                          </template>
                          <template v-if="column.key === 'is_valid'">
                            <YunzhupaasSwitch v-model:value="record.is_valid" @change="changeData('company_invoiceis_valid', index)"> </YunzhupaasSwitch>
                          </template>
                          <template v-if="column.key === 'remark'">
                            <YunzhupaasInput
                              v-model:value="record.remark"
                              @change="changeData('company_invoiceremark', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.company_invoiceremark"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'action'">
                            <a-space>
                              <a-button class="action-btn" type="link" @click="copyCompany_invoiceRow(index)" size="small">
                                {{ t('common.copyText', '复制') }}
                              </a-button>
                              <a-button class="action-btn" type="link" color="error" @click="removeCompany_invoiceRow(index, true)" size="small">
                                {{ t('common.delText', '删除') }}
                              </a-button>
                            </a-space>
                          </template>
                        </template>
                      </a-table>
                      <a-space class="input-table-footer-btn">
                        <a-button type="primary" preIcon="icon-ym icon-ym-btn-add" @click="addCompany_invoiceRow">
                          {{ t('common.add1Text', '添加') }}
                        </a-button>
                        <a-button type="danger" preIcon="icon-ym icon-ym-btn-clearn" @click="batchRemoveCompany_invoiceRow(true)">
                          {{ t('common.batchDelText', '批量删除') }}
                        </a-button>
                      </a-space>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-tab-pane>
              <a-tab-pane tab="银行账户" key="2" forceRender>
                <a-row :gutter="15">
                  <a-col :span="24" class="ant-col-item mb-20px">
                    <a-form-item>
                      <a-table
                        :data-source="dataForm.companyBankList"
                        :columns="companyBankColumns"
                        size="small"
                        :pagination="false"
                        :scroll="{ x: 'max-content' }"
                        :rowSelection="getcompanyBankRowSelection"
                        rowKey="yunzhupaasId">
                        <template #headerCell="{ column }">
                          <span class="required-sign" v-if="column.required">*</span>
                          {{ column.title }}
                          <BasicHelp :text="column.tipLabel" v-if="column.tipLabel && column.title" />
                        </template>
                        <template #bodyCell="{ column, index, record }">
                          <template v-if="column.key === 'index'">{{ index + 1 }}</template>
                          <template v-if="column.key === 'bank_name'">
                            <YunzhupaasInput
                              v-model:value="record.bank_name"
                              @change="changeData('companyBankbank_name', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.companyBankbank_name"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'bank_account_name'">
                            <YunzhupaasInput
                              v-model:value="record.bank_account_name"
                              @change="changeData('companyBankbank_account_name', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.companyBankbank_account_name"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'bank_account_number'">
                            <YunzhupaasInput
                              v-model:value="record.bank_account_number"
                              @change="changeData('companyBankbank_account_number', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.companyBankbank_account_number"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'bank_province'">
                            <YunzhupaasInput
                              v-model:value="record.bank_province"
                              @change="changeData('companyBankbank_province', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.companyBankbank_province"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'remark'">
                            <YunzhupaasInput
                              v-model:value="record.remark"
                              @change="changeData('companyBankremark', index)"
                              placeholder="请输入"
                              :allowClear="true"
                              :style="{ width: '100%' }"
                              :maskConfig="maskConfig.companyBankremark"
                              :showCount="false">
                            </YunzhupaasInput>
                          </template>
                          <template v-if="column.key === 'action'">
                            <a-space>
                              <a-button class="action-btn" type="link" @click="copyCompanyBankRow(index)" size="small">
                                {{ t('common.copyText', '复制') }}
                              </a-button>
                              <a-button class="action-btn" type="link" color="error" @click="removeCompanyBankRow(index, true)" size="small">
                                {{ t('common.delText', '删除') }}
                              </a-button>
                            </a-space>
                          </template>
                        </template>
                      </a-table>
                      <a-space class="input-table-footer-btn">
                        <a-button type="primary" preIcon="icon-ym icon-ym-btn-add" @click="addCompanyBankRow"> {{ t('common.add1Text', '添加') }} </a-button>
                        <a-button type="danger" preIcon="icon-ym icon-ym-btn-clearn" @click="batchRemoveCompanyBankRow(true)">
                          {{ t('common.batchDelText', '批量删除') }}
                        </a-button>
                      </a-space>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-tab-pane>
            </a-tabs>
          </a-col>
          <!-- 表单结束 -->
        </a-row>
      </a-form>
    </a-row>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { create, update, getInfo } from './helper/api';
  import { reactive, toRefs, nextTick, ref, unref, computed, toRaw, inject } from 'vue';
  import { BasicDrawer, useDrawer } from '@/components/Drawer';
  import { yunzhupaasRelationForm } from '@/components/yunzhupaas';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useI18n } from '@/hooks/web/useI18n';
  import { useUserStore } from '@/store/modules/user';
  import type { FormInstance } from 'ant-design-vue';
  import { thousandsFormat, getDateTimeUnit, getTimeUnit } from '@/utils/yunzhupaas';
  import { getDictionaryDataSelector } from '@/api/systemData/dictionary';
  import { getDataInterfaceRes } from '@/api/systemData/dataInterface';
  import dayjs from 'dayjs';
  // 表单权限
  import { usePermission } from '@/hooks/web/usePermission';
  import { cloneDeep } from 'lodash-es';
  import { buildUUID } from '@/utils/uuid';
  import { CaretRightOutlined } from '@ant-design/icons-vue';

  interface State {
    dataForm: any;
    tableRows: any;
    dataRule: any;
    optionsObj: any;
    childIndex: any;
    isEdit: any;
    interfaceRes: any;
    //可选范围默认值
    ableAll: any;
    //掩码配置
    maskConfig: any;
    //定位属性
    locationScope: any;
    activetabformItem509c12: any;
    selectedcompany_invoiceRowKeys: any;
    selectedcompanyBankRowKeys: any;
    extraOptions: any;
    title: string;
    continueText: string;
    allList: any[];
    currIndex: number;
    isContinue: boolean;
    submitType: number;
    showContinueBtn: boolean;
  }

  const emit = defineEmits(['reload']);
  const getLeftTreeActiveInfo: (() => any) | null = inject('getLeftTreeActiveInfo', null);
  const userStore = useUserStore();
  const userInfo = userStore.getUserInfo;
  const { createMessage, createConfirm } = useMessage();
  const { t } = useI18n();
  const [registerDrawer, { openDrawer, setDrawerProps }] = useDrawer();
  const formRef = ref<FormInstance>();
  const company_invoiceColumns: any[] = computed(() => {
    let list = [
      {
        title: '发票抬头编码',
        dataIndex: 'title_code',
        key: 'title_code',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'title_code',
      },
      {
        title: '发票抬头名称',
        dataIndex: 'title_name',
        key: 'title_name',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: true,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'title_name',
      },
      {
        title: '纳税人识别号',
        dataIndex: 'credit_code',
        key: 'credit_code',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: true,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'credit_code',
      },
      {
        title: '纳税人类别',
        dataIndex: 'tax_type',
        key: 'tax_type',
        labelWidth: '',
        span: '8',
        tipLabel: '',
        required: true,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'tax_type',
      },
      {
        title: '地址',
        dataIndex: 'address',
        key: 'address',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'address',
      },
      {
        title: '电话',
        dataIndex: 'phone',
        key: 'phone',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'phone',
      },
      {
        title: '开户银行',
        dataIndex: 'bank_name',
        key: 'bank_name',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'bank_name',
      },
      {
        title: '银行账户',
        dataIndex: 'bank_account',
        key: 'bank_account',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'bank_account',
      },
      {
        title: '是否默认抬头',
        dataIndex: 'is_defalut',
        key: 'is_defalut',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: '',
        align: 'left',
        fixed: false,
        formP: 'is_defalut',
      },
      {
        title: '是否有效',
        dataIndex: 'is_valid',
        key: 'is_valid',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: '',
        align: 'left',
        fixed: false,
        formP: 'is_valid',
      },
      {
        title: '备注',
        dataIndex: 'remark',
        key: 'remark',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'remark',
      },
    ];
    list.push({ title: t('component.table.action'), showLabel: true, dataIndex: 'action', key: 'action', align: 'center', fixed: 'right', width: 100 });
    const indexColumn = { title: t('component.table.index'), showLabel: true, dataIndex: 'index', key: 'index', align: 'center', fixed: 'left', width: 50 };
    //子表复杂表头-处理
    let columnList = list;
    let complexHeaderList: any[] = [];
    if (complexHeaderList.length) {
      let childColumns: any[] = [];
      let firstChildColumns: string[] = [];
      for (let i = 0; i < complexHeaderList.length; i++) {
        const e = complexHeaderList[i];
        e.title = e.fullNameI18nCode ? t(e.fullNameI18nCode, e.fullName) : e.fullName;
        e.align = e.align;
        e.children = [];
        e.yunzhupaasKey = 'complexHeader';
        if (e.childColumns?.length) {
          childColumns.push(...e.childColumns);
          for (let k = 0; k < e.childColumns.length; k++) {
            const item = e.childColumns[k];
            for (let j = 0; j < list.length; j++) {
              const o = list[j];
              if (o.key == item && o.fixed !== 'left' && o.fixed !== 'right') e.children.push({ ...o });
            }
          }
        }
        if (e.children.length) firstChildColumns.push(e.children[0].key);
      }
      complexHeaderList = complexHeaderList.filter(o => o.children.length);
      let newList: any[] = [];
      for (let i = 0; i < list.length; i++) {
        const e = list[i];
        if (!childColumns.includes(e.key) || e.fixed === 'left' || e.fixed === 'right') {
          newList.push(e);
        } else {
          if (firstChildColumns.includes(e.key)) {
            const item = complexHeaderList.find(o => o.childColumns.includes(e.key));
            newList.push(item);
          }
        }
      }
      columnList = newList;
    }
    let columns = [indexColumn, ...columnList];
    const leftFixedList = columns.filter(o => o.fixed === 'left');
    const rightFixedList = columns.filter(o => o.fixed === 'right');
    const noFixedList = columns.filter(o => o.fixed !== 'left' && o.fixed !== 'right');
    return [...leftFixedList, ...noFixedList, ...rightFixedList];
  });

  const getcompany_invoiceHasBatchBtn = computed(() => {
    let flist: any[] = [
      { btnIcon: 'icon-ym icon-ym-btn-add', show: true, label: '添加', btnType: 'primary', value: 'add', labelI18nCode: 'common.add1Text' },
      {
        btnIcon: 'icon-ym icon-ym-btn-clearn',
        showConfirm: 'true',
        show: true,
        label: '批量删除',
        btnType: 'danger',
        value: 'batchRemove',
        labelI18nCode: 'common.batchDelText',
      },
    ];
    return flist?.length && flist.some(o => o.value == 'batchRemove' && !!o.show);
  });

  const getcompany_invoiceRowSelection = computed(() => {
    if (!unref(getcompany_invoiceHasBatchBtn)) return undefined;
    const rowSelection = {
      selectedRowKeys: state.selectedcompany_invoiceRowKeys,
      onChange: (selectedRowKeys: string[]) => {
        state.selectedcompany_invoiceRowKeys = selectedRowKeys;
      },
    };
    return rowSelection;
  });

  const companyBankColumns: any[] = computed(() => {
    let list = [
      {
        title: '开户行',
        dataIndex: 'bank_name',
        key: 'bank_name',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'bank_name',
      },
      {
        title: '账户名',
        dataIndex: 'bank_account_name',
        key: 'bank_account_name',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'bank_account_name',
      },
      {
        title: '银行账号',
        dataIndex: 'bank_account_number',
        key: 'bank_account_number',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'bank_account_number',
      },
      {
        title: '开户行城市',
        dataIndex: 'bank_province',
        key: 'bank_province',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'bank_province',
      },
      {
        title: '备注',
        dataIndex: 'remark',
        key: 'remark',
        labelWidth: '',
        span: '24',
        tipLabel: '',
        required: false,
        style: { width: '100%' },
        align: 'left',
        fixed: false,
        formP: 'remark',
      },
    ];
    list.push({ title: t('component.table.action'), showLabel: true, dataIndex: 'action', key: 'action', align: 'center', fixed: 'right', width: 100 });
    const indexColumn = { title: t('component.table.index'), showLabel: true, dataIndex: 'index', key: 'index', align: 'center', fixed: 'left', width: 50 };
    //子表复杂表头-处理
    let columnList = list;
    let complexHeaderList: any[] = [];
    if (complexHeaderList.length) {
      let childColumns: any[] = [];
      let firstChildColumns: string[] = [];
      for (let i = 0; i < complexHeaderList.length; i++) {
        const e = complexHeaderList[i];
        e.title = e.fullNameI18nCode ? t(e.fullNameI18nCode, e.fullName) : e.fullName;
        e.align = e.align;
        e.children = [];
        e.yunzhupaasKey = 'complexHeader';
        if (e.childColumns?.length) {
          childColumns.push(...e.childColumns);
          for (let k = 0; k < e.childColumns.length; k++) {
            const item = e.childColumns[k];
            for (let j = 0; j < list.length; j++) {
              const o = list[j];
              if (o.key == item && o.fixed !== 'left' && o.fixed !== 'right') e.children.push({ ...o });
            }
          }
        }
        if (e.children.length) firstChildColumns.push(e.children[0].key);
      }
      complexHeaderList = complexHeaderList.filter(o => o.children.length);
      let newList: any[] = [];
      for (let i = 0; i < list.length; i++) {
        const e = list[i];
        if (!childColumns.includes(e.key) || e.fixed === 'left' || e.fixed === 'right') {
          newList.push(e);
        } else {
          if (firstChildColumns.includes(e.key)) {
            const item = complexHeaderList.find(o => o.childColumns.includes(e.key));
            newList.push(item);
          }
        }
      }
      columnList = newList;
    }
    let columns = [indexColumn, ...columnList];
    const leftFixedList = columns.filter(o => o.fixed === 'left');
    const rightFixedList = columns.filter(o => o.fixed === 'right');
    const noFixedList = columns.filter(o => o.fixed !== 'left' && o.fixed !== 'right');
    return [...leftFixedList, ...noFixedList, ...rightFixedList];
  });

  const getcompanyBankHasBatchBtn = computed(() => {
    let flist: any[] = [
      { btnIcon: 'icon-ym icon-ym-btn-add', show: true, label: '添加', btnType: 'primary', value: 'add', labelI18nCode: 'common.add1Text' },
      {
        btnIcon: 'icon-ym icon-ym-btn-clearn',
        showConfirm: 'true',
        show: true,
        label: '批量删除',
        btnType: 'danger',
        value: 'batchRemove',
        labelI18nCode: 'common.batchDelText',
      },
    ];
    return flist?.length && flist.some(o => o.value == 'batchRemove' && !!o.show);
  });

  const getcompanyBankRowSelection = computed(() => {
    if (!unref(getcompanyBankHasBatchBtn)) return undefined;
    const rowSelection = {
      selectedRowKeys: state.selectedcompanyBankRowKeys,
      onChange: (selectedRowKeys: string[]) => {
        state.selectedcompanyBankRowKeys = selectedRowKeys;
      },
    };
    return rowSelection;
  });

  const state = reactive<State>({
    dataForm: {
       company_scope:'INT',
      entity_type: 'ORG',
      company_code: undefined,
      company_name: undefined,
      short_name: undefined,
      credit_code: undefined,
      tax_type: '',
      org_id: userInfo.organizeIdList ? userInfo.organizeIdList : '',
      province_id: [],
      enterprise_nature: '',
      industry_code: '',
      enterprise_scale: '',
      registration_date: undefined,
      registered_capital: undefined,
      legal_representative: undefined,
      phone: undefined,
      email: undefined,
      website: undefined,
      address: undefined,
      business_scope: undefined,
      remark: undefined,
      yunzhupaas_mdm_corporation_yunzhupaas_major_person_id: userInfo.userId ? userInfo.userId : '',
      company_invoiceList: [],
      companyBankList: [],
    },

    tableRows: {
      company_invoiceList: {
        title_code: '',
        title_name: '',
        credit_code: '',
        tax_type: '',
        address: '',
        phone: '',
        bank_name: '',
        bank_account: '',
        is_defalut: 0,
        is_valid: 0,
        remark: '',
        enabledmark: undefined,
      },
      companyBankList: {
        bank_name: '',
        bank_account_name: '',
        bank_account_number: '',
        bank_province: '',
        remark: '',
        enabledmark: undefined,
      },
    },

    dataRule: {
      company_name: [
        {
          required: true,
          message: t('sys.validate.textRequiredSuffix', '不能为空'),
          trigger: 'blur',
        },
      ],
      credit_code: [
        {
          required: true,
          message: t('sys.validate.textRequiredSuffix', '不能为空'),
          trigger: 'blur',
        },
      ],
      tax_type: [
        {
          required: true,
          message: t('sys.validate.arrayRequiredPrefix	', '请至少选择一个'),
          trigger: 'change',
        },
      ],
      org_id: [
        {
          required: true,
          message: t('sys.validate.arrayRequiredPrefix	', '请至少选择一个'),
          trigger: 'change',
        },
      ],
      yunzhupaas_mdm_corporation_yunzhupaas_major_person_id: [
        {
          required: true,
          message: t('sys.validate.arrayRequiredPrefix	', '请至少选择一个'),
          trigger: 'change',
        },
      ],
    },

    optionsObj: {
      entity_typeOptions: [
        { fullName: '组织/企业', id: 'ORG' },
        { fullName: '个人', id: 'IND' },
      ],
      entity_typeProps: { label: 'fullName', value: 'id' },
      tax_typeOptions: [
        { fullName: '一般纳税人', id: 'GENERAL' },
        { fullName: '小规模纳税人', id: 'SMALL' },
      ],
      tax_typeProps: { label: 'fullName', value: 'id' },
      enterprise_natureOptions: [],
      enterprise_natureProps: { label: 'fullName', value: 'id' },
      industry_codeOptions: [],
      industry_codeProps: { label: 'fullName', value: 'id' },
      enterprise_scaleOptions: [],
      enterprise_scaleProps: { label: 'fullName', value: 'id' },
      company_invoicetax_typeOptions: [
        { fullName: '一般纳税人', id: 'GENERAL' },
        { fullName: '小规模纳税人', id: 'SMALL' },
      ],
      company_invoicetax_typeProps: { label: 'fullName', value: 'id' },
    },

    childIndex: -1,
    isEdit: false,
    interfaceRes: {
      company_invoicebank_name: [],
      companyBankbank_account_number: [],
      companyBankremark: [],
      company_invoicephone: [],
      industry_code: [],
      remark: [],
      company_invoicetax_type: [],
      company_invoiceis_defalut: [],
      credit_code: [],
      yunzhupaas_mdm_corporation_yunzhupaas_major_person_id: [],
      company_code: [],
      company_invoiceaddress: [],
      email: [],
      website: [],
      address: [],
      company_invoicebank_account: [],
      companyBankbank_account_name: [],
      company_invoicetitle_code: [],
      legal_representative: [],
      enterprise_nature: [],
      registered_capital: [],
      companyBankbank_province: [],
      company_invoicecredit_code: [],
      company_invoiceis_valid: [],
      business_scope: [],
      company_invoiceremark: [],
      entity_type: [],
      registration_date: [],
      tax_type: [],
      province_id: [],
      phone: [],
      org_id: [],
      company_name: [],
      short_name: [],
      enterprise_scale: [],
      company_invoicetitle_name: [],
      companyBankbank_name: [],
    },
    //可选范围默认值
    ableAll: {},

    //掩码配置
    maskConfig: {
      company_name: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      short_name: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      credit_code: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      legal_representative: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      phone: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      email: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      website: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      address: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoicetitle_name: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoicecredit_code: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoiceaddress: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoicephone: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoicebank_name: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoicebank_account: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      company_invoiceremark: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      companyBankbank_name: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      companyBankbank_account_name: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      companyBankbank_account_number: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      companyBankbank_province: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
      companyBankremark: {
        prefixType: 1,
        useUnrealMask: false,
        maskType: 1,
        unrealMaskLength: 1,
        prefixLimit: 0,
        suffixLimit: 0,
        filler: '*',
        prefixSpecifyChar: '',
        suffixType: 1,
        ignoreChar: '',
        suffixSpecifyChar: '',
      },
    },

    //定位属性
    locationScope: {},

    activetabformItem509c12: '1',
    selectedcompany_invoiceRowKeys: [],
    selectedcompanyBankRowKeys: [],
    extraOptions: {},

    title: '',
    continueText: '',
    allList: [],
    currIndex: 0,
    isContinue: false,
    submitType: 0,
    showContinueBtn: true,
  });
  const { title, continueText, showContinueBtn, dataRule, dataForm, optionsObj, ableAll, maskConfig, submitType } = toRefs(state);

  const getPrevDisabled = computed(() => state.currIndex === 0);
  const getNextDisabled = computed(() => state.currIndex === state.allList.length - 1);
  // 表单权限
  const { hasFormP } = usePermission();

  defineExpose({ init });

  function init(data) {
    state.submitType = 0;
    state.isContinue = false;
    state.title = !data.id ? t('common.add2Text', '新增') : t('common.editText', '编辑');
    state.continueText = !data.id ? t('common.continueAndAddText', '确定并新增') : t('common.continueText', '确定并继续');
    setFormProps({ continueLoading: false });
    state.dataForm.id = data.id;
    openDrawer();
    state.allList = data.allList;
    state.currIndex = state.allList.length && data.id ? state.allList.findIndex(item => item.id === data.id) : 0;
    nextTick(() => {
      getForm().resetFields();
      state.dataForm.company_invoiceList = [];
      state.dataForm.companyBankList = [];
      setTimeout(initData, 0);
    });
  }
  function initData() {
    changeLoading(true);
    state.activetabformItem509c12 = '1';
    if (state.dataForm.id) {
      getData(state.dataForm.id);
    } else {
      //初始化options
      getenterprise_natureOptions();
      getindustry_codeOptions();
      getenterprise_scaleOptions();

      if (state.dataForm.company_invoiceList) {
        for (let i = 0; i < state.dataForm.company_invoiceList.length; i++) {
          state.childIndex = i;
          state.dataForm.company_invoiceList[i].yunzhupaasId = buildUUID();
        }
      }
      if (state.dataForm.companyBankList) {
        for (let i = 0; i < state.dataForm.companyBankList.length; i++) {
          state.childIndex = i;
          state.dataForm.companyBankList[i].yunzhupaasId = buildUUID();
        }
      }
      // 设置默认值
      state.dataForm = {
         company_scope:'INT',
        entity_type: 'ORG',
        company_code: undefined,
        company_name: undefined,
        short_name: undefined,
        credit_code: undefined,
        tax_type: '',
        org_id: userInfo.organizeIdList ? userInfo.organizeIdList : '',
        province_id: [],
        enterprise_nature: '',
        industry_code: '',
        enterprise_scale: '',
        registration_date: undefined,
        registered_capital: undefined,
        legal_representative: undefined,
        phone: undefined,
        email: undefined,
        website: undefined,
        address: undefined,
        business_scope: undefined,
        remark: undefined,
        yunzhupaas_mdm_corporation_yunzhupaas_major_person_id: userInfo.userId ? userInfo.userId : '',
        company_invoiceList: [],
        companyBankList: [],
      };
      if (getLeftTreeActiveInfo) state.dataForm = { ...state.dataForm, ...(getLeftTreeActiveInfo() || {}) };
      state.childIndex = -1;
      changeLoading(false);
    }
  }
  function getForm() {
    const form = unref(formRef);
    if (!form) {
      throw new Error('form is null!');
    }
    return form;
  }
  function getData(id) {
    getInfo(id).then(res => {
      state.dataForm = res.data || {};
      getenterprise_natureOptions();
      getindustry_codeOptions();
      getenterprise_scaleOptions();

      if (state.dataForm.company_invoiceList) {
        for (let i = 0; i < state.dataForm.company_invoiceList.length; i++) {
          state.childIndex = i;
          state.dataForm.company_invoiceList[i].yunzhupaasId = buildUUID();
        }
      }
      if (state.dataForm.companyBankList) {
        for (let i = 0; i < state.dataForm.companyBankList.length; i++) {
          state.childIndex = i;
          state.dataForm.companyBankList[i].yunzhupaasId = buildUUID();
        }
      }
      state.childIndex = -1;
      changeLoading(false);
    });
  }
  async function handleSubmit(type) {
    try {
      const values = await getForm()?.validate();
      if (!values) return;
      if (!company_invoiceExist()) return;
      if (!companyBankExist()) return;

      setFormProps({ confirmLoading: true });
      const formMethod = state.dataForm.id ? update : create;
      formMethod(state.dataForm)
        .then(res => {
          createMessage.success(res.msg);
          setFormProps({ confirmLoading: false });
          if (state.submitType == 1) {
            initData();
            state.isContinue = true;
          } else {
            setFormProps({ open: false });
            emit('reload');
          }
        })
        .catch(() => {
          setFormProps({ confirmLoading: false });
        });
    } catch (_) {}
  }
  function handlePrev() {
    state.currIndex--;
    handleGetNewInfo();
  }
  function handleNext() {
    state.currIndex++;
    handleGetNewInfo();
  }
  function handleGetNewInfo() {
    changeLoading(true);
    getForm().resetFields();
    const id = state.allList[state.currIndex].id;
    getData(id);
  }
  function setFormProps(data) {
    setDrawerProps(data);
  }
  function changeLoading(loading) {
    setDrawerProps({ loading });
  }
  async function onClose() {
    if (state.isContinue) emit('reload');
    return true;
  }

  function changeData(model, index) {
    state.isEdit = false;
    state.childIndex = index;
    for (let key in state.interfaceRes) {
      if (key != model) {
        let faceReList = state.interfaceRes[key];
        for (let i = 0; i < faceReList.length; i++) {
          let relationField = faceReList[i].relationField;
          if (relationField) {
            let modelAll = relationField.split('-');
            let faceMode = '';
            let faceMode2 = modelAll.length == 2 ? modelAll[0].substring(0, modelAll[0].length - 4) + modelAll[1] : '';
            for (let i = 0; i < modelAll.length; i++) {
              faceMode += modelAll[i];
            }
            if (faceMode == model || faceMode2 == model) {
              let options = 'get' + key + 'Options';
              eval(options)(true);
              changeData(key, index);
            }
          }
        }
      }
    }
  }
  function changeDataFormData(type, data, model, index, defaultValue) {
    if (!state.isEdit) {
      if (type == 2) {
        for (let i = 0; i < state.dataForm[data].length; i++) {
          if (index == -1) {
            state.dataForm[data][i][model] = defaultValue;
          } else if (index == i) {
            state.dataForm[data][i][model] = defaultValue;
          }
        }
      } else {
        state.dataForm[data] = defaultValue;
      }
    }
  }
  function addCompany_invoiceRow() {
    let item = {
      title_code: undefined,
      title_name: undefined,
      credit_code: undefined,
      tax_type: '',
      company_invoicetax_typeOptions: state.optionsObj.company_invoicetax_typeOptions,
      address: undefined,
      phone: undefined,
      bank_name: undefined,
      bank_account: undefined,
      is_defalut: 0,
      is_valid: 0,
      remark: undefined,
      yunzhupaasId: buildUUID(),
    };
    state.dataForm.company_invoiceList.push(item);
    state.childIndex = state.dataForm.company_invoiceList.length - 1;
    state.childIndex = -1;
  }

  function removeCompany_invoiceRow(index, showConfirm = false) {
    if (showConfirm) {
      createConfirm({
        iconType: 'warning',
        title: '提示',
        content: '此操作将永久删除该数据, 是否继续?',
        onOk: () => {
          state.dataForm.company_invoiceList.splice(index, 1);
        },
      });
    } else {
      state.dataForm.company_invoiceList.splice(index, 1);
    }
  }

  function copyCompany_invoiceRow(index) {
    let item = cloneDeep(state.dataForm.company_invoiceList[index]);
    item.title_code = '';
    let copyData = {};
    for (let i = 0; i < unref(company_invoiceColumns).length; i++) {
      const cur = unref(company_invoiceColumns)[i];
      if (cur.key != 'index' && cur.key != 'action') {
        if (cur.children?.length && cur.yunzhupaasKey == 'complexHeader') {
          for (let j = 0; j < cur.children.length; j++) {
            copyData[cur.children[j].key] = item[cur.children[j].key];
          }
        } else {
          copyData[cur.key] = item[cur.key];
        }
      }
    }
    const copyItem = { ...copyData, yunzhupaasId: buildUUID() };
    state.dataForm.company_invoiceList.push(copyItem);
    state.childIndex = state.dataForm.company_invoiceList.length - 1;
    state.childIndex = -1;
  }

  function batchRemoveCompany_invoiceRow(showConfirm = false) {
    if (!state.selectedcompany_invoiceRowKeys.length) return createMessage.error('请选择一条数据');
    const handleRemove = () => {
      state.dataForm.company_invoiceList = state.dataForm.company_invoiceList.filter(o => !state.selectedcompany_invoiceRowKeys.includes(o.yunzhupaasId));
      nextTick(() => {
        state.selectedcompany_invoiceRowKeys = [];
      });
    };
    if (showConfirm) {
      createConfirm({
        iconType: 'warning',
        title: '提示',
        content: '此操作将永久删除该数据, 是否继续?',
        onOk: () => {
          handleRemove();
        },
      });
    } else {
      handleRemove();
    }
  }

  function company_invoiceExist() {
    let isOk = true;
    for (let i = 0; i < state.dataForm.company_invoiceList.length; i++) {
      const e = state.dataForm.company_invoiceList[i];
      if (!e.title_name) {
        createMessage.error('发票抬头名称' + t('sys.validate.arrayRequiredPrefix	', '请至少选择一个'));
        isOk = false;
        break;
      }
      if (!e.credit_code) {
        createMessage.error('纳税人识别号' + t('sys.validate.arrayRequiredPrefix	', '请至少选择一个'));
        isOk = false;
        break;
      }
      if (!e.tax_type) {
        createMessage.error('纳税人类别' + t('sys.validate.arrayRequiredPrefix	', '请至少选择一个'));
        isOk = false;
        break;
      }
    }
    return isOk;
  }

  function addCompanyBankRow() {
    let item = {
      bank_name: undefined,
      bank_account_name: undefined,
      bank_account_number: undefined,
      bank_province: undefined,
      remark: undefined,
      yunzhupaasId: buildUUID(),
    };
    state.dataForm.companyBankList.push(item);
    state.childIndex = state.dataForm.companyBankList.length - 1;
    state.childIndex = -1;
  }

  function removeCompanyBankRow(index, showConfirm = false) {
    if (showConfirm) {
      createConfirm({
        iconType: 'warning',
        title: '提示',
        content: '此操作将永久删除该数据, 是否继续?',
        onOk: () => {
          state.dataForm.companyBankList.splice(index, 1);
        },
      });
    } else {
      state.dataForm.companyBankList.splice(index, 1);
    }
  }

  function copyCompanyBankRow(index) {
    let item = cloneDeep(state.dataForm.companyBankList[index]);
    let copyData = {};
    for (let i = 0; i < unref(companyBankColumns).length; i++) {
      const cur = unref(companyBankColumns)[i];
      if (cur.key != 'index' && cur.key != 'action') {
        if (cur.children?.length && cur.yunzhupaasKey == 'complexHeader') {
          for (let j = 0; j < cur.children.length; j++) {
            copyData[cur.children[j].key] = item[cur.children[j].key];
          }
        } else {
          copyData[cur.key] = item[cur.key];
        }
      }
    }
    const copyItem = { ...copyData, yunzhupaasId: buildUUID() };
    state.dataForm.companyBankList.push(copyItem);
    state.childIndex = state.dataForm.companyBankList.length - 1;
    state.childIndex = -1;
  }

  function batchRemoveCompanyBankRow(showConfirm = false) {
    if (!state.selectedcompanyBankRowKeys.length) return createMessage.error('请选择一条数据');
    const handleRemove = () => {
      state.dataForm.companyBankList = state.dataForm.companyBankList.filter(o => !state.selectedcompanyBankRowKeys.includes(o.yunzhupaasId));
      nextTick(() => {
        state.selectedcompanyBankRowKeys = [];
      });
    };
    if (showConfirm) {
      createConfirm({
        iconType: 'warning',
        title: '提示',
        content: '此操作将永久删除该数据, 是否继续?',
        onOk: () => {
          handleRemove();
        },
      });
    } else {
      handleRemove();
    }
  }

  function companyBankExist() {
    let isOk = true;
    for (let i = 0; i < state.dataForm.companyBankList.length; i++) {
      const e = state.dataForm.companyBankList[i];
    }
    return isOk;
  }

  //数据选项--数据字典初始化方法
  function getenterprise_natureOptions() {
    getDictionaryDataSelector('9b542177a477488994ce09fff3c93901').then(res => {
      state.optionsObj.enterprise_natureOptions = res.data.list;
    });
  }
  //数据选项--数据字典初始化方法
  function getindustry_codeOptions() {
    getDictionaryDataSelector('d59a3cf65f9847dbb08be449e3feae16').then(res => {
      state.optionsObj.industry_codeOptions = res.data.list;
    });
  }
  //数据选项--数据字典初始化方法
  function getenterprise_scaleOptions() {
    getDictionaryDataSelector('enterpriseScale').then(res => {
      state.optionsObj.enterprise_scaleOptions = res.data.list;
    });
  }
  function getRelationDate(timeRule, timeType, timeTarget, timeValueData, dataValue) {
    let timeDataValue: any = null;
    let timeValue = Number(timeValueData);
    if (timeRule) {
      if (timeType == 1) {
        timeDataValue = timeValue;
      } else if (timeType == 2) {
        timeDataValue = dataValue;
      } else if (timeType == 3) {
        timeDataValue = new Date().getTime();
      } else if (timeType == 4 || timeType == 5) {
        const type = getTimeUnit(timeTarget);
        const method = timeType == 4 ? 'subtract' : 'add';
        timeDataValue = dayjs()[method](timeValue, type).valueOf();
      }
    }
    return timeDataValue;
  }
  function getRelationTime(timeRule, timeType, timeTarget, timeValue, formatType, dataValue) {
    let format = formatType == 'HH:mm' ? 'HH:mm:00' : formatType;
    let timeDataValue: any = null;
    if (timeRule) {
      if (timeType == 1) {
        timeDataValue = timeValue || '00:00:00';
        if (timeDataValue.split(':').length == 3) {
          timeDataValue = timeDataValue;
        } else {
          timeDataValue = timeDataValue + ':00';
        }
      } else if (timeType == 2) {
        timeDataValue = dataValue;
      } else if (timeType == 3) {
        timeDataValue = dayjs().format(format);
      } else if (timeType == 4 || timeType == 5) {
        const type = getTimeUnit(timeTarget + 3);
        const method = timeType == 4 ? 'subtract' : 'add';
        timeDataValue = dayjs()[method](timeValue, type).format(format);
      }
    }
    return timeDataValue;
  }
</script>
