<template>
  <div class="tab-content">
    <div class="info-section">
      <div class="section-title-row">
        <div class="section-title-left">
          <span class="section-title-text">企业基本信息</span>
          <span class="section-title-divider">/</span>
          <span class="supplier-no">供应商编码：{{ detailData.supplierNo }}</span>
        </div>
        <el-button v-if="!isViewMode" type="primary" icon="Document" @click="emit('save')">保存</el-button>
      </div>

      <el-form :model="detailData" label-width="120px" class="detail-form">
        <el-row :gutter="12" class="form-row">
          <el-col :span="8">
            <el-form-item label="所属公司：" required>
              <el-select v-model="detailData.ownedCompany" placeholder="请选择" clearable filterable style="width: 100%;">
                <el-option v-for="company in companyOptions" :key="company.id" :label="company.companyName" :value="company.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="企业名称：" required>
              <el-input v-model="detailData.enterpriseName" placeholder="请输入企业名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工商名称：" required>
              <el-input
                v-model="detailData.businessName"
                placeholder="请输入工商名称"
                @blur="isAddMode && !isBasicInfoSaved && !isViewMode && emit('getBusinessInfo')"
                @keyup.enter="isAddMode && !isBasicInfoSaved && !isViewMode && emit('getBusinessInfo')"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12" class="form-row">
          <el-col :span="8">
            <el-form-item label="企业简称：" required>
              <el-input v-model="detailData.shortName" placeholder="请输入企业简称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商等级：" required>
              <el-select v-model="detailData.cooperateLevel" placeholder="请选择" clearable filterable style="width: 100%;">
                <el-option v-for="level in supplierLevelOptions" :key="level.id" :label="level.supplierLevelName" :value="String(level.id)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="企业规模：" required>
              <el-select v-model="detailData.membershipSize" placeholder="请选择" clearable filterable style="width: 100%;">
                <el-option v-for="scale in enterpriseScaleOptions" :key="scale.id" :label="scale.enterpriseScaleName" :value="scale.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12" class="form-row">
          <el-col :span="8">
            <el-form-item label="行业类别：" required>
              <el-select v-model="detailData.industrCategory" placeholder="请选择" clearable filterable style="width: 100%;">
                <el-option v-for="industry in industryCategoryOptions" :key="industry.id" :label="industry.industryCategoryName" :value="industry.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商类型：" required>
              <el-select v-model="detailData.supplierType" placeholder="请选择" clearable filterable style="width: 100%;">
                <el-option v-for="type in supplierTypeOptions" :key="type.id" :label="type.supplierTypeName" :value="type.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="固定电话：">
              <el-input
                v-model="detailData.fixedPhone"
                placeholder="请输入固定电话"
                type="tel"
                @input="onFixedPhoneInput"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12" class="form-row">
          <el-col :span="8">
            <el-form-item label="传真：">
              <el-input v-model="detailData.fax" placeholder="请输入传真" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="企业邮箱：">
              <el-input v-model="detailData.mailbox" placeholder="请输入企业邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开始时间：">
              <el-date-picker v-model="detailData.validityFromDate" type="date" placeholder="请选择" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12" class="form-row">
          <el-col :span="8">
            <el-form-item label="结束时间：">
              <el-date-picker v-model="detailData.validityToDate" type="date" placeholder="请选择" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮政编码：">
              <el-input v-model="detailData.postCode" placeholder="请输入邮政编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="网址：">
              <el-input v-model="detailData.url" placeholder="请输入网址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12" class="form-row">
          <el-col :span="8">
            <el-form-item label="详细地址：" required>
              <el-cascader v-model="selectedOfficeRegionProxy" :options="regionOptions" placeholder="请选择省市区" clearable filterable style="width: 100%;" @change="onOfficeRegionChange" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label=" " label-width="0">
              <el-input v-model="detailData.officeAddress" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="info-section">
      <div class="section-title">工商信息</div>

      <el-row :gutter="12" class="form-row">
        <el-col :span="8">
          <div class="form-item">
            <span class="label">企业工商名称：</span>
            <span class="value">{{ businessInfo.businessName || detailData.businessName || '' }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span class="label">登记机关：</span>
            <span class="value">{{ businessInfo.registrationAuthority || '' }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span class="label">成立日期：</span>
            <span class="value">{{ formatDate(businessInfo.establishmentDate) || '' }}</span>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="12" class="form-row">
        <el-col :span="8">
          <div class="form-item">
            <span class="label">登记状态：</span>
            <span class="value">{{ businessInfo.registrationStatus || '' }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span class="label">实缴资本：</span>
            <span class="value">{{ businessInfo.paidInCapital || '' }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span class="label">社会信用代码：</span>
            <span class="value">{{ businessInfo.socialCreditCode || detailData.socialCreditCode || '' }}</span>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="12" class="form-row">
        <el-col :span="8">
          <div class="form-item">
            <span class="label">法人姓名：</span>
            <span class="value">{{ businessInfo.legalPersonName || '' }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span class="label">注册资本：</span>
            <span class="value">{{ businessInfo.registeredCapital || '' }}</span>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="12" class="form-row">
        <el-col :span="24">
          <div class="form-item">
            <span class="label">工商地址：</span>
            <el-input v-model="businessInfo.businessAddress" placeholder="工商地址" style="width: 70%;" />
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="12" class="form-row">
        <el-col :span="12">
          <div class="form-item">
            <span class="label">营业执照：</span>
            <ImageUpload
              v-model="detailData.businessLicense"
              :limit="1"
              :disabled="isViewMode"
              :file-size="5"
              :file-type="['png', 'jpg', 'jpeg']"
              :is-show-tip="false"
              value-type="url"
            />
          </div>
        </el-col>
        <el-col :span="12">
          <div class="form-item">
            <span class="label">法人身份证照片：</span>
            <ImageUpload
              v-model="detailData.personImage"
              :limit="1"
              :disabled="isViewMode"
              :file-size="5"
              :file-type="['png', 'jpg', 'jpeg']"
              :is-show-tip="false"
              value-type="url"
            />
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="info-section">
      <div class="section-title-row">
        <div class="section-title-left">
          <span class="section-title-text">付款信息</span>
        </div>
        <el-button v-if="!isViewMode" type="primary" icon="Plus" @click="emit('addPayment')">新增付款信息</el-button>
      </div>
      <el-table :data="paymentInfoList" border style="width: 100%">
        <el-table-column prop="isture" label="是否主账号" align="center">
          <template #default="scope">
            <span>{{ Number(scope.row.isture) === 1 ? '是' : '否' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="bankName" label="开户银行" align="center" />
        <el-table-column prop="bankNo" label="银行账户" align="center" />

        <el-table-column label="操作" align="center" width="120">
          <template #default="scope">
            <el-button link type="primary" @click="emit('viewPayment', scope.row)">查看</el-button>
            <el-button v-if="!isViewMode" link type="primary" @click="emit('editPayment', scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>


    </div>
  </div>
</template>

<script setup lang="ts">
import ImageUpload from '@/components/ImageUpload/index.vue';

const props = defineProps<{
  detailData: any;
  companyOptions: any[];
  enterpriseScaleOptions: any[];
  industryCategoryOptions: any[];
  supplierLevelOptions: any[];
  supplierTypeOptions: any[];
  isAddMode: boolean;
  isViewMode: boolean;
  isBasicInfoSaved: boolean;
  businessInfo: any;
  businessInfoLoading: boolean;
  regionOptions: any[];
  selectedOfficeRegion: string[];
  paymentInfoList: any[];
  formatDate: (date: any) => string;
}>();

const emit = defineEmits<{
  (e: 'save'): void;
  (e: 'getBusinessInfo'): void;
  (e: 'officeRegionChange', value: string[]): void;
  (e: 'update:selectedOfficeRegion', value: string[]): void;
  (e: 'addPayment'): void;
  (e: 'viewPayment', row: any): void;
  (e: 'editPayment', row: any): void;
}>();

const selectedOfficeRegionProxy = computed({
  get: () => props.selectedOfficeRegion,
  set: (value) => emit('update:selectedOfficeRegion', value)
});

const onOfficeRegionChange = (val: unknown) => {
  emit('officeRegionChange', (val || []) as string[]);
};

const onFixedPhoneInput = (val: string) => {
  const next = (val || '').replace(/\D+/g, '');
  if (next !== props.detailData.fixedPhone) {
    props.detailData.fixedPhone = next;
  }
};
</script>

<style scoped>
.tab-content {
  --el-component-size: 24px;
  padding: 16px;
}

.detail-form :deep(.el-form-item__label) {
  white-space: nowrap;
}

.detail-form :deep(.form-row) {
  margin-bottom: 2px;
}

.detail-form :deep(.el-form-item) {
  margin-bottom: 8px;
}

.tab-content :deep(.form-row) {
  margin-bottom: 2px;
}

.tab-content :deep(.el-form-item) {
  margin-bottom: 8px;
}

.tab-content :deep(.info-section) {
  margin-bottom: 20px;
}

.tab-content :deep(.section-title),
.tab-content :deep(.section-title-row) {
  margin-bottom: 12px;
  padding-bottom: 8px;
}

.tab-content :deep(.section-title-text) {
  font-size: 14px;
}

.tab-content :deep(.supplier-no) {
  font-size: 13px;
}

.tab-content :deep(.el-form-item__label) {
  font-size: 13px;
  height: var(--el-component-size);
  line-height: var(--el-component-size);
  padding-top: 0;
  padding-bottom: 0;
}

.tab-content :deep(.form-item) {
  line-height: 24px;
  font-size: 13px;
}

.detail-form :deep(.el-input),
.detail-form :deep(.el-select),
.detail-form :deep(.el-date-editor),
.detail-form :deep(.el-cascader) {
  width: 100%;
}

.detail-form :deep(.el-input__wrapper),
.detail-form :deep(.el-select__wrapper),
.detail-form :deep(.el-date-editor),
.detail-form :deep(.el-cascader) {
  min-height: var(--el-component-size);
}

.detail-form :deep(.el-input__inner) {
  height: var(--el-component-size);
  line-height: var(--el-component-size);
}

.detail-form :deep(.el-form-item__content) {
  align-items: center;
  min-height: var(--el-component-size);
}

.detail-form :deep(.el-input__wrapper),
.detail-form :deep(.el-select__wrapper),
.detail-form :deep(.el-date-editor),
.detail-form :deep(.el-cascader) {
  height: var(--el-component-size);
  align-items: center;
  padding-top: 0;
  padding-bottom: 0;
}

.detail-form :deep(.el-select__selected-item),
.detail-form :deep(.el-select__input),
.detail-form :deep(.el-range-input) {
  line-height: var(--el-component-size);
}

:deep(.component-upload-image .el-upload--picture-card),
:deep(.component-upload-image .el-upload-list--picture-card .el-upload-list__item) {
  width: 64px;
  height: 64px;
}

:deep(.component-upload-image .el-upload-list--picture-card .el-upload-list__item-thumbnail) {
  width: 64px;
  height: 64px;
}
</style>
