<template>
  <!-- 业绩库页面 -->
  <div class="app-container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <span>投标人主体库</span>
      <span class="separator">＞</span>
      <span>企业知识库</span>
      <span class="separator">＞</span>
      <span class="current">业绩库</span>
      <el-button type="text" class="back-btn" @click="handleBack">返回</el-button>
    </div>

    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" class="search-form">
        <!-- 项目名称 -->
        <el-form-item prop="proName" class="form-item">
          <label class="label-text">项目名称</label>
          <el-input
            v-model="queryParams.proName"
            placeholder="请输入项目名称进行查询"
            clearable
            maxlength="64"
            class="search-input"
            @keyup.enter.native="handleQuery"
          ></el-input>
        </el-form-item>

        <!-- 项目类型 -->
        <el-form-item prop="proType" class="form-item">
          <label class="label-text">项目类型</label>
          <el-select
            v-model="queryParams.proType"
            placeholder="请选择项目类型"
            clearable
            class="search-select"
            @change="handleQuery"
          >
            <el-option
              v-for="type in projectTypeOptions"
              :key="type.value"
              :label="type.label"
              :value="type.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 电压等级 -->
        <el-form-item prop="voltageLevel" class="form-item">
          <label class="label-text">电压等级</label>
          <el-select
            v-model="queryParams.voltageLevel"
            placeholder="请选择电压等级"
            clearable
            class="search-select"
            @change="handleQuery"
          >
            <el-option
              v-for="level in voltageLevelOptions"
              :key="level.value"
              :label="level.label"
              :value="level.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 合同签订时间 -->
        <el-form-item prop="contractSigningTime" class="form-item">
          <label class="label-text">合同签订时间</label>
          <el-date-picker
            v-model="queryParams.contractSigningTime"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="search-date"
            @change="handleQuery"
          ></el-date-picker>
        </el-form-item>

        <!-- 项目经理 -->
        <el-form-item prop="projectManager" class="form-item">
          <label class="label-text">项目经理</label>
          <el-select
            v-model="queryParams.projectManager"
            placeholder="请选择项目经理"
            clearable
            class="search-select"
            @change="handleQuery"
          >
            <el-option
              v-for="manager in projectManagerOptions"
              :key="manager.value"
              :label="manager.label"
              :value="manager.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 竣工时间 -->
        <el-form-item prop="completionTime" class="form-item">
          <label class="label-text">竣工时间</label>
          <el-date-picker
            v-model="queryParams.completionTime"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="search-date"
            @change="handleQuery"
          ></el-date-picker>
        </el-form-item>

        <!-- 按钮组 -->
        <el-form-item class="form-item button-group">
          <el-button type="primary" size="small" @click="handleQuery" class="search-btn">查询</el-button>
          <el-button size="small" @click="resetQuery" class="reset-btn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>


    <!-- 表格操作区（新增按钮居右） -->
    <div class="table-toolbar">
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
        v-hasPermi="['enterpriseLibrary:performance:add']"
        class="add-btn"
      >
        新增业绩
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="performanceList"
      border
      fit
      stripe
      highlight-current-row
      style="width: 100%; margin-top: 15px;"
    >
      <el-table-column prop="proName" label="项目名称" align="center" min-width="120"></el-table-column>
      <el-table-column prop="proType" label="项目类型" align="center" min-width="100"></el-table-column>
      <el-table-column prop="voltageLevel" label="电压等级" align="center" min-width="80"></el-table-column>
      <el-table-column prop="constructionSite" label="建设地点" align="center" min-width="100"></el-table-column>
      <el-table-column prop="constructionUnit" label="建设单位" align="center" min-width="120"></el-table-column>
      <el-table-column prop="contractSigningTime" label="合同签订时间" align="center" min-width="120"></el-table-column>
      <el-table-column
        prop="contractAmount"
        label="合同金额"
        align="center"
        min-width="100"
        :formatter="formatCurrency"
      ></el-table-column>
      <el-table-column prop="startTime" label="开工时间" align="center" min-width="120"></el-table-column>
      <el-table-column prop="completionTime" label="竣工时间" align="center" min-width="120"></el-table-column>
      <el-table-column prop="projectManager" label="项目经理" align="center" min-width="100"></el-table-column>
      <el-table-column label="操作" align="center" min-width="180" class="operation-column">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['enterpriseLibrary:performance:detail']"
          >
            查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['enterpriseLibrary:performance:edit']"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['enterpriseLibrary:performance:del']"
            style="color: #ff4d4f;"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { encryptWithSM4 } from '@/utils/sm'
import {
  listPerformance,
  delPerformance
} from "@/api/enterprise/performance";

export default {
  name: 'Performance',
  components: {},
  data() {
    return {
      // 加载状态
      loading: false,
      // 业绩列表
      performanceList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: '',
        proType: '',
        voltageLevel: '',
        contractSigningTime: [],
        projectManager: '',
        completionTime: []
      },
      // 项目类型选项
      projectTypeOptions: [
        { label: '输变电工程', value: '输变电工程' },
        { label: '配电工程', value: '配电工程' },
        { label: '发电工程', value: '发电工程' }
      ],
      // 电压等级选项
      voltageLevelOptions: [
        { label: '110kV', value: '110kV' },
        { label: '220kV', value: '220kV' },
        { label: '500kV', value: '500kV' },
        { label: '1000kV', value: '1000kV' }
      ],
      // 项目经理选项
      projectManagerOptions: [
        { label: '赵远兆', value: '赵远兆' },
        { label: '张明', value: '张明' },
        { label: '李华', value: '李华' }
      ]
    }
  },

  created() {
    this.getList()
  },

  methods: {
    // 获取业绩列表
    getList() {
      this.loading = true
      // 处理日期范围参数
      const params = { ...this.queryParams }
      if (params.contractSigningTime && params.contractSigningTime.length) {
        params.contractSignStartTime = params.contractSigningTime[0]
        params.contractSignEndTime = params.contractSigningTime[1]
      }
      if (params.completionTime && params.completionTime.length) {
        params.completionStartTime = params.completionTime[0]
        params.completionEndTime = params.completionTime[1]
      }
      delete params.contractSigningTime
      delete params.completionTime

      listPerformance(params).then(response => {
        this.performanceList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(error => {
        this.loading = false
        this.$message.error('获取数据失败：' + error.message)
      })
    },

    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    // 重置
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        proName: '',
        proType: '',
        voltageLevel: '',
        contractSigningTime: [],
        projectManager: '',
        completionTime: []
      }
      this.handleQuery()
    },

    // 新增业绩
    handleAdd() {
      this.$router.push({
       name: "PerformanceForm",
        query: {
          type: encryptWithSM4('add'),
          id: encryptWithSM4('')
        },
        title: '新增业绩'
      })
    },

    // 查看详情 - 跳转至详情页
    handleView(row) {
      this.$router.push({
        name: "PerformanceDetail",
        query: {
          id: encryptWithSM4((row.performanceId ?? '').toString())
        },
        title: '业绩详情'
      })
    },

    // 编辑业绩 - 跳转至编辑页
    handleEdit(row) {
      this.$router.push({
        name: "PerformanceEdit",
        query: {
          type: encryptWithSM4('edit'),
          id: encryptWithSM4((row.performanceId ?? '').toString())
        },
        title: '编辑业绩'
      })
    },

    // 删除业绩
    handleDelete(row) {
      this.$confirm(`确定要删除【${row.proName}】吗？`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPerformance(row.performanceId).then(() => {
          this.$message.success('删除成功')
          this.getList() // 重新加载列表
        }).catch(error => {
          this.$message.error('删除失败：' + error.message)
        })
      })
    },

    // 分页大小改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },

    // 格式化金额
    formatCurrency(row, column, cellValue) {
      if (!cellValue) return '0.00'
      return parseFloat(cellValue).toFixed(2)
    },

    // 返回上级页面
    handleBack() {
      const obj = { path: "/enterpriseLibrary/performance" }
      this.$tab.closeOpenPage(obj)
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 15px 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

// 面包屑导航
.breadcrumb {
  display: flex;
  align-items: center;
  padding: 10px 0;
  margin-bottom: 15px;
  color: #606266;
  font-size: 14px;

  .separator {
    margin: 0 8px;
    color: #c0c4cc;
  }

  .current {
    color: #409eff;
  }

  .back-btn {
    margin-left: auto;
    color: #606266;
    padding: 5px 10px;

    &:hover {
      color: #409eff;
    }
  }
}

// 搜索区域样式
.search-container {
  background: #fff;
  border-radius: 4px;
  padding: 12px 15px;
  margin-bottom: 15px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  width: 100%;
}

.form-item {
  display: flex;
  align-items: center;
  margin-right: 20px;
  margin-bottom: 8px;

  &.button-group {
    margin-left: 10px;
    margin-right: 0;
  }
}

.label-text {
  color: #606266;
  font-size: 14px;
  margin-right: 8px;
  white-space: nowrap;
}

// 输入控件样式
.search-input {
  width: 160px;
}

.search-select {
  width: 160px;
}

.search-date {
  width: 220px;

  ::v-deep .el-range-separator {
    width: 20px;
  }
}

// 按钮样式
.search-btn {
  background-color: #409eff;
  border-color: #409eff;
  color: #fff;
  padding: 6px 15px;
}

.reset-btn {
  background-color: #fff;
  border-color: #dcdfe6;
  color: #606266;
  padding: 6px 15px;
  margin-left: 8px;
}

// 表格操作区
.table-toolbar {
  background: #fff;
  border-radius: 4px;
  padding: 10px 15px;
  margin-bottom: 15px;
  text-align: right;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);

  .add-btn {
    background-color: #409eff;
    border-color: #409eff;
  }
}

// 数据表格
.el-table {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

  ::v-deep .el-table__header-wrapper th {
    background-color: #f5f7fa;
    font-weight: 600;
    color: #333;
  }

  ::v-deep .el-table__body tr:hover > td {
    background-color: #f0f8ff !important;
  }

  ::v-deep .el-table__body tr.current-row > td {
    background-color: #e6f3ff !important;
  }
}

.pagination-container {
  display: flex;
  justify-content: center;
  padding: 20px;
  margin-top: 15px;
  position: sticky;
  bottom: 0;
  z-index: 10;

  .el-pagination {
    .el-pagination__total {
      color: #606266;
      font-weight: 500;
    }

    .el-pagination__sizes {
      .el-select {
        .el-input__inner {
          border-color: #dcdfe6;
          border-radius: 4px;
        }
      }
    }

    .el-pager li {
      border-radius: 4px;
      margin: 0 2px;

      &.active {
        background: #409EFF;
        color: #fff;
      }

      &:hover {
        color: #409EFF;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 4px;
      margin: 0 2px;

      &:hover {
        color: #409EFF;
      }
    }

    .el-pagination__jump {
      .el-input__inner {
        border-color: #dcdfe6;
        border-radius: 4px;
      }
    }
  }
}
</style>
