<template>
  <div>
    <!-- 表单查询项 -->
    <div class="headerbox">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200">
        <el-row>
          <el-col :span="5">
            <el-form-item label="项目名称" style="width: 100%;">
              <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="建设起止时间">
              <el-date-picker v-model="queryParams.begainTime" type="month" placeholder="开始月份" value-format="yyyy-MM"
                style="width: 9rem;" :clearable="true">
              </el-date-picker>
              ~
              <el-date-picker v-model="queryParams.endTime" type="month" placeholder="结束月份" value-format="yyyy-MM"
                style="width: 9rem;" :clearable="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="现状分类">
              <el-select v-model="queryParams.xzfl" placeholder="现状分类" clearable>
                <el-option v-for="dict in dict.type.xzfl" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="评价等级">
              <el-select v-model="queryParams.pjdj" placeholder="请选择评价等级" clearable>
                <el-option v-for="dict in dict.type.qdpjdj" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search"  @click="handleQuery">查询</el-button>
              <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 表格内容区 -->
    <div class="tablebox">
      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange"
        @sort-change="handleSortChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            {{ (scope.$index + 1) + (queryParams.current - 1) * queryParams.size }}
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="name" />
        <el-table-column label="现状分类" align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.xzfl" :value="scope.row.xzfl" />
          </template>
        </el-table-column>
        <el-table-column label="项目法人单位" align="center" prop="xmfrdwxz" />
        <el-table-column label="项目建设起止时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.begainTime }} 至 {{ scope.row.endTime }}
          </template>
        </el-table-column>
        <el-table-column label="项目评价" align="center" width="130" sortable="custom" prop="pjdj">
          <template slot-scope="scope">
            {{ scope.row.pjdj }} / {{ scope.row.count }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right"  align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button  type="text" @click="handleInfo(scope.row, 'detail')">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { getProjectEvaluationList, updateProjectEvaluationList } from '@/api/ManageApi/index.js';
import { checkPermi, checkRole } from "@/utils/permission";

export default {
  dicts: [
    'xzfl', 'qdpjdj'
  ],
  data() {
    return {
      postList: [],
      loading: false,
      total: 0,
      queryParams: {
        current: 1,
        size: 10,
        name: '',
        begainTime: '',
        endTime: '',
        xzfl: '',
        pjdj: '',
        qdpjdj:'',
      },
      showSearch: true,
      dialogVisible: false,
      editForm: {
        id: '',
        name: '',
        dateRange: '',
        xzfl: '',
        pjdj: '',
        xmfrdwxz: '',
      },
      infoDialogVisible: false,
      infoForm: {
        id: '',
        name: '',
        dateRange: '',
        xzfl: '',
        pjdj: '',
        xmfrdwxz: '',
      }
    };
  },
  methods: {
    checkPermi,
    checkRole,
    handleSortChange({ prop, order }) {
      if (order === 'ascending') {
        this.postList.sort((a, b) => a[prop] - b[prop]);
      } else if (order === 'descending') {
        this.postList.sort((a, b) => b[prop] - a[prop]);
      }
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 10,
        name: '',
        begainTime: '',
        endTime: '',
        xzfl: '',
        pjdj: ''
      };
      this.handleQuery();
    },
    getList() {
      this.loading = true;

      const params = {
        current: this.queryParams.current,
        size: this.queryParams.size,
        name: this.queryParams.name,
        xzfl: this.queryParams.xzfl,
        pjdj: this.queryParams.pjdj,
        begainTime: this.queryParams.begainTime || null,
        endTime: this.queryParams.endTime || null
      };

      getProjectEvaluationList(params)
        .then(response => {
          this.postList = response.data.records;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleSelectionChange(selection) {
      // 处理选择项变化
    },
    handleInfo(row, type) {
      this.$store.commit("SET_CRUMBS",  "项目详情");
      const id = row.id || this.ids[0];
      this.$router.push({ path: `/manage-add/${id}`, query: { action: type } });
    },
    handleUpdate(row) {
      this.editForm = {
        ...row,
        dateRange: [row.begainTime, row.endTime]
      };
      this.dialogVisible = true;
    },
    updateProject() {
      const formData = {
        ...this.editForm,
        begainTime: this.editForm.dateRange[0],
        endTime: this.editForm.dateRange[1]
      };
      updateProjectEvaluationList(formData)
        .then(response => {
          this.$message.success('修改成功');
          this.dialogVisible = false;
          this.getList();
        })
        .catch(error => {
          console.error('修改项目失败:', error);
          this.$message.error('修改失败');
        });
    },
    handleDelete(row) {
      // 处理删除
    },
    formatMonthYear(date) {
      // if (!date) return '';
      return date;
      // // 确保传入的是完整日期，然后只显示年月部分
      // const fullDate = date.includes('-') ? date : `${date}-01`; // 如果只有年月，补上日期
      // return fullDate.split('-').slice(0, 2).join('-');
    }
  },
  mounted() {
    this.getList();
  }
};
</script>


<style scoped>
.headerbox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablebox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablehead {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.headbtn {
  display: flex;
}

.tablebtntwo {
  margin-top: 1rem;
  margin-bottom: 1rem;
}
</style>