<template>
  <div class="page-container">
    <div class="page-header">
      <PageTitle title="集团关联企业管理" />
      <el-button type="danger" @click="handleAdd">+ 新建</el-button>
    </div>

    <el-table :data="enterpriseList" border>
      <el-table-column label="序号" width="50" align="center">
        <template #default="{ $index }">{{ $index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="companyName" label="公司名称" min-width="180" show-overflow-tooltip />
      <el-table-column prop="contact" label="联系人" width="70" align="center" />
      <el-table-column prop="phone" label="电话" width="120" align="center" />
      <el-table-column prop="monthLimit" label="月度采购限额" min-width="95" align="center" />
      <el-table-column prop="yearLimit" label="年度采购限额" min-width="95" align="center" />
      <el-table-column prop="creditLimit" label="平台信用额度" min-width="95" align="center" />
      <el-table-column prop="remainLimit" label="实时剩余额度" min-width="95" align="center" />
      <el-table-column prop="purchaseStatus" label="采购情况" width="70" align="center">
        <template #default="{ row }">
          <span>{{ row.purchaseStatus || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="550px" destroy-on-close>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="120px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="formData.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="formData.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="月度采购限额">
          <el-input-number v-model="formData.monthLimit" :min="0" :precision="2" controls-position="right" style="width: 100%" />
        </el-form-item>
        <el-form-item label="年度采购限额">
          <el-input-number v-model="formData.yearLimit" :min="0" :precision="2" controls-position="right" style="width: 100%" />
        </el-form-item>
        <el-form-item label="平台信用额度">
          <el-input-number v-model="formData.creditLimit" :min="0" :precision="2" controls-position="right" style="width: 100%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="danger" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { PageTitle } from '@/components'

const dialogVisible = ref(false)
const formRef = ref()
const editingRow = ref<any>(null)

const formData = reactive({
  companyName: '',
  contact: '',
  phone: '',
  monthLimit: 0,
  yearLimit: 0,
  creditLimit: 0
})

const formRules = {
  companyName: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
  contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入电话', trigger: 'blur' }]
}

const dialogTitle = computed(() => editingRow.value ? '编辑关联企业' : '新建关联企业')

const enterpriseList = ref([
  { id: 1, companyName: '台州古德贸易有限公司', contact: '李婷', phone: '18062433912', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 2, companyName: '上海飞索半导体有限公司', contact: '孙思达', phone: '13442197054', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 3, companyName: '荆门格林沃德新材料有限公司', contact: '赵奕艳', phone: '15179893205', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 4, companyName: '大兴科里科技有限公司', contact: '王凡玄', phone: '15858669874', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 5, companyName: '滨州宏华国际贸易有限公司', contact: '周小艺', phone: '18402944521', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 6, companyName: '广西民锐建筑工程有限公司', contact: '李书萍', phone: '13905679687', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 7, companyName: '佳木斯晶雨轩商贸有限公司', contact: '吴彦漆', phone: '18928852431', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 8, companyName: '青海诺维信企业管理有限公司', contact: '周海', phone: '15558661691', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 9, companyName: '天津坤州港发物流有限公司', contact: '冯云', phone: '15108535283', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' },
  { id: 10, companyName: '乌兰察布清门科技有限公司', contact: '郑盈', phone: '13782318088', monthLimit: '0.00', yearLimit: '0.00', creditLimit: '0.00', remainLimit: '0.00', purchaseStatus: '' }
])

const handleAdd = () => {
  editingRow.value = null
  formData.companyName = ''
  formData.contact = ''
  formData.phone = ''
  formData.monthLimit = 0
  formData.yearLimit = 0
  formData.creditLimit = 0
  dialogVisible.value = true
}

const handleEdit = (row: any) => {
  editingRow.value = row
  formData.companyName = row.companyName
  formData.contact = row.contact
  formData.phone = row.phone
  formData.monthLimit = parseFloat(row.monthLimit) || 0
  formData.yearLimit = parseFloat(row.yearLimit) || 0
  formData.creditLimit = parseFloat(row.creditLimit) || 0
  dialogVisible.value = true
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确定要删除"${row.companyName}"吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const index = enterpriseList.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      enterpriseList.value.splice(index, 1)
    }
    ElMessage.success('删除成功')
  })
}

const handleSubmit = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return
  if (editingRow.value) {
    Object.assign(editingRow.value, {
      companyName: formData.companyName,
      contact: formData.contact,
      phone: formData.phone,
      monthLimit: formData.monthLimit.toFixed(2),
      yearLimit: formData.yearLimit.toFixed(2),
      creditLimit: formData.creditLimit.toFixed(2)
    })
    ElMessage.success('编辑成功')
  } else {
    enterpriseList.value.push({
      id: enterpriseList.value.length + 1,
      companyName: formData.companyName,
      contact: formData.contact,
      phone: formData.phone,
      monthLimit: formData.monthLimit.toFixed(2),
      yearLimit: formData.yearLimit.toFixed(2),
      creditLimit: formData.creditLimit.toFixed(2),
      remainLimit: '0.00',
      purchaseStatus: ''
    })
    ElMessage.success('新建成功')
  }
  dialogVisible.value = false
}
</script>

<style scoped lang="scss">
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  :deep(.page-title) {
    margin-bottom: 0;
  }
}

:deep(.el-table) {
  th.el-table__cell {
    background: #fafafa;
    font-weight: 500;
    color: #333;
  }
  .el-table__cell {
    padding: 10px 0;
  }
}
</style>
