<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="单位名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.customerName`" :rules="formRules.customerName" class="mb-0px!">
            <el-input v-model="row.customerName" placeholder="请输入单位名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="统一社会信用代码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.unifiedCreditCode`" :rules="formRules.unifiedCreditCode" class="mb-0px!">
            <el-input v-model="row.unifiedCreditCode" placeholder="请输入统一社会信用代码" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="开票地址" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.invoicingAddress`" :rules="formRules.invoicingAddress" class="mb-0px!">
            <el-input v-model="row.invoicingAddress" placeholder="请输入开票地址" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="开票电话" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.invoicingTelephone`" :rules="formRules.invoicingTelephone" class="mb-0px!">
            <el-input v-model="row.invoicingTelephone" placeholder="请输入开票电话" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="开户行" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.companyBank`" :rules="formRules.companyBank" class="mb-0px!">
            <el-input v-model="row.companyBank" placeholder="请输入开户行" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="开户账号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.companyAccount`" :rules="formRules.companyAccount" class="mb-0px!">
            <el-input v-model="row.companyAccount" placeholder="请输入开户账号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="通讯地址" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.companyAddress`" :rules="formRules.companyAddress" class="mb-0px!">
            <el-input v-model="row.companyAddress" placeholder="请输入通讯地址" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="删除标记：1-正常，-1-已删除" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.deletedFlag`" :rules="formRules.deletedFlag" class="mb-0px!">
            <el-input v-model="row.deletedFlag" placeholder="请输入删除标记：1-正常，-1-已删除" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加</el-button>
  </el-row>
</template>
<script setup lang="ts">
import * as ContractApi from '@/api/crm/contract'

const props = defineProps<{
  contractId: undefined // 合同ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  contractId: [{ required: true, message: '合同ID不能为空', trigger: 'blur' }],
  customerName: [{ required: true, message: '单位名称不能为空', trigger: 'blur' }],
  unifiedCreditCode: [{ required: true, message: '统一社会信用代码不能为空', trigger: 'blur' }],
  invoicingAddress: [{ required: true, message: '开票地址不能为空', trigger: 'blur' }],
  invoicingTelephone: [{ required: true, message: '开票电话不能为空', trigger: 'blur' }],
  companyBank: [{ required: true, message: '开户行不能为空', trigger: 'blur' }],
  companyAccount: [{ required: true, message: '开户账号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.contractId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await ContractApi.getContractAAuthorizedCompanyListByContractId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    contractId: undefined,
    customerName: undefined,
    unifiedCreditCode: undefined,
    invoicingAddress: undefined,
    invoicingTelephone: undefined,
    companyBank: undefined,
    companyAccount: undefined,
    companyAddress: undefined,
    deletedFlag: undefined
  }
  row.contractId = props.contractId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>