<template>
  <GiPageLayout>
    <GiTable
      row-key="id"
      title="投标文件管理"
      :data="dataList"
      :columns="tableColumns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1500 }"
      :pagination="pagination"
      @page-change="onPageChange"
      @page-size-change="onPageSizeChange"
      @refresh="search"
    >
      <template #top>
        <GiForm 
          v-model="searchForm" 
          search 
          :columns="queryFormColumns" 
          size="medium" 
          @search="search" 
          @reset="reset"
        />
      </template>
      
      <template #toolbar-left>
        <a-space>
          <a-button type="primary" @click="openAddModal">
            <template #icon><icon-plus /></template>
            <template #default>新建投标</template>
          </a-button>
          <a-button @click="generateProposal">
            <template #icon><icon-file-text /></template>
            <template #default>生成标书</template>
          </a-button>
        </a-space>
      </template>
      
      <!-- 投标状态 -->
      <template #status="{ record }">
        <a-tag :color="getStatusColor(record.status)">
          {{ getStatusText(record.status) }}
        </a-tag>
      </template>
      
      <!-- 投标金额 -->
      <template #bidAmount="{ record }">
        <span class="font-medium text-blue-600">￥{{ record.bidAmount.toLocaleString() }}万</span>
      </template>
      
      <!-- 操作列 -->
      <template #action="{ record }">
        <a-space>
          <a-link @click="viewDetail(record)">详情</a-link>
          <a-link @click="editRecord(record)" v-if="record.status === 'draft'">编辑</a-link>
          <a-link @click="submitBid(record)" v-if="record.status === 'draft'">提交</a-link>
          <a-link @click="downloadBid(record)">下载</a-link>
        </a-space>
      </template>
    </GiTable>
  </GiPageLayout>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Message } from '@arco-design/web-vue'
import type { TableColumnData } from '@arco-design/web-vue'

// 搜索表单
let searchForm = reactive({
  projectName: '',
  bidCode: '',
  status: '',
  submitDate: '',
  page: 1,
  size: 10
})

// 查询条件配置
const queryFormColumns = [
  {
    field: 'projectName',
    label: '项目名称',
    type: 'input' as const,
    props: {
      placeholder: '请输入项目名称'
    }
  },
  {
    field: 'bidCode',
    label: '投标编号',
    type: 'input' as const,
    props: {
      placeholder: '请输入投标编号'
    }
  },
  {
    field: 'status',
    label: '投标状态',
    type: 'select' as const,
    props: {
      placeholder: '请选择投标状态',
      options: [
        { label: '草稿', value: 'draft' },
        { label: '已提交', value: 'submitted' },
        { label: '中标', value: 'won' },
        { label: '未中标', value: 'lost' },
        { label: '已撤回', value: 'withdrawn' }
      ]
    }
  }
]

// 表格列配置
const tableColumns: TableColumnData[] = [
  { title: '投标编号', dataIndex: 'bidCode', width: 150 },
  { title: '招标编号', dataIndex: 'tenderCode', width: 150 },
  { title: '项目名称', dataIndex: 'projectName', width: 250, ellipsis: true, tooltip: true },
  { title: '招标单位', dataIndex: 'tenderUnit', width: 200, ellipsis: true, tooltip: true },
  { title: '投标金额', dataIndex: 'bidAmount', slotName: 'bidAmount', width: 120 },
  { title: '工期(天)', dataIndex: 'duration', width: 100 },
  { title: '技术评分', dataIndex: 'techScore', width: 100 },
  { title: '商务评分', dataIndex: 'commercialScore', width: 100 },
  { title: '提交时间', dataIndex: 'submitTime', width: 160 },
  { title: '开标时间', dataIndex: 'openTime', width: 160 },
  { title: '投标状态', dataIndex: 'status', slotName: 'status', width: 100 },
  { title: '排名', dataIndex: 'ranking', width: 80 },
  { title: '负责人', dataIndex: 'manager', width: 100 },
  { title: '备注', dataIndex: 'remark', width: 200, ellipsis: true, tooltip: true },
  { title: '操作', slotName: 'action', width: 200, fixed: 'right' }
]

// 数据状态
const loading = ref(false)
const dataList = ref([
  {
    id: 1,
    bidCode: 'BD2024001',
    tenderCode: 'TD2024001',
    projectName: '华能新能源风电场叶片检测服务项目',
    tenderUnit: '华能新能源股份有限公司',
    bidAmount: 320,
    duration: 60,
    techScore: 92,
    commercialScore: 89,
    submitTime: '2024-02-09 17:30:00',
    openTime: '2024-02-15 09:00:00',
    status: 'won',
    ranking: 1,
    manager: '张项目经理',
    remark: '成功中标，技术方案优秀'
  },
  {
    id: 2,
    bidCode: 'BD2024002',
    tenderCode: 'TD2024002',
    projectName: '大唐风电场防雷检测项目',
    tenderUnit: '大唐新能源股份有限公司',
    bidAmount: 268,
    duration: 45,
    techScore: 85,
    commercialScore: 88,
    submitTime: '2024-02-14 16:45:00',
    openTime: '2024-02-20 10:00:00',
    status: 'submitted',
    ranking: 0,
    manager: '李项目经理',
    remark: '已提交投标文件，等待开标'
  },
  {
    id: 3,
    bidCode: 'BD2024003',
    tenderCode: 'TD2024003',
    projectName: '国电投海上风电智能监测系统',
    tenderUnit: '国家电力投资集团',
    bidAmount: 1150,
    duration: 120,
    techScore: 88,
    commercialScore: 85,
    submitTime: '2024-01-24 18:00:00',
    openTime: '2024-01-30 14:00:00',
    status: 'lost',
    ranking: 3,
    manager: '王项目经理',
    remark: '未中标，技术方案需要完善'
  }
])

const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 3,
  showTotal: true,
  showPageSize: true
})

// 获取状态颜色
const getStatusColor = (status: string) => {
  const colorMap: Record<string, string> = {
    'draft': 'gray',
    'submitted': 'blue',
    'won': 'green',
    'lost': 'red',
    'withdrawn': 'orange'
  }
  return colorMap[status] || 'gray'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const textMap: Record<string, string> = {
    'draft': '草稿',
    'submitted': '已提交',
    'won': '中标',
    'lost': '未中标',
    'withdrawn': '已撤回'
  }
  return textMap[status] || status
}

// 搜索和重置
const search = async () => {
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 1000)
}

const reset = () => {
  Object.assign(searchForm, {
    projectName: '',
    bidCode: '',
    status: '',
    submitDate: '',
    page: 1,
    size: 10
  })
  pagination.current = 1
  search()
}

// 分页处理
const onPageChange = (page: number) => {
  searchForm.page = page
  pagination.current = page
  search()
}

const onPageSizeChange = (size: number) => {
  searchForm.size = size
  searchForm.page = 1
  pagination.pageSize = size
  pagination.current = 1
  search()
}

// 操作方法
const openAddModal = () => {
  Message.info('新建投标功能开发中...')
}

const generateProposal = () => {
  Message.info('生成标书功能开发中...')
}

const viewDetail = (record: any) => {
  Message.info(`查看投标详情: ${record.projectName}`)
}

const editRecord = (record: any) => {
  Message.info(`编辑投标文件: ${record.projectName}`)
}

const submitBid = (record: any) => {
  Message.info(`提交投标: ${record.projectName}`)
}

const downloadBid = (record: any) => {
  Message.info(`下载投标文件: ${record.projectName}`)
}

onMounted(() => {
  search()
})
</script> 