<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <search-form :items="searchItems" @search="handleSearch" />
      <div class="manage-header">
        <div class="header-left"></div>
        <div class="header-right">
          <el-button type="primary" @click="investmentCecoveryAdd"
            >新增</el-button
          >
        </div>
      </div>
      <div class="manage-content" v-loading="loading">
        <common-table
          :autoHeight="true"
          :maxRows="10"
          :data="tableData"
          :columns="tableColumns"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize"
          :index="true"
          :indexLabel="'序号'"
          title=""
          :border="true"
          @size-change="handleSizeChange"
          @current-page-change="handleCurrentPageChange"
        >
          <template #operations="{ row, index }">
            <el-button
              link
              type="primary"
              size="small"
              @click="previewStatement(row)"
              >查看</el-button
            >
            <el-button
              link
              type="primary"
              size="small"
              @click="editStatement(row)"
              >编辑</el-button
            >
            <el-button
              link
              type="danger"
              size="small"
              @click="deleteStatement(row)"
              >删除</el-button
            >
          </template>
        </common-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import CommonTable from "@/components/common/commonTable.vue";
import SearchForm from "@/components/common/SearchForm.vue";

const searchItems = [
  {
    type: "input",
    label: "项目名称",
    prop: "projectName",
    placeholder: "请输入项目名称",
    span: 6,
    clearable: true,
  },
  {
    type: "input",
    label: "项目编号",
    prop: "projectCode",
    placeholder: "请输入项目编号",
    span: 6,
    clearable: true,
  },
  {
    type: "date",
    label: "年度",
    prop: "nd",
    placeholder: "请选择年度",
    span: 6,
    clearable: true,
  },
  {
    type: "input",
    label: "管理主体",
    prop: "glzt",
    placeholder: "请输入管理主体",
    span: 6,
    clearable: true,
  },
  {
    type: "input",
    label: "联系人",
    prop: "lxr",
    placeholder: "请输入联系人",
    span: 6,
    clearable: true,
  },

  {
    type: "dict-select",
    label: "项目阶段",
    prop: "xmjd",
    dictName: "xmjd",
    placeholder: "请选择项目阶段",
    span: 6,
    clearable: true,
  },

  {
    type: "dict-select",
    label: "项目区域",
    prop: "xmqy",
    dictName: "xmqy",
    placeholder: "请选择项目区域",
    span: 6,
    clearable: true,
  },
];
const handleSearch = (formData) => {
  currentPage.value = 1;

  getProjectData(formData);
};
const router = useRouter();
const { proxy } = getCurrentInstance();

let tableData = ref([]);
let tableColumns = ref([
  {
    prop: "projectName",
    label: "项目名称",
    showOverflowTooltip: true,
  },
  {
    prop: "nd",
    label: "年度",
    showOverflowTooltip: true,
  },

  {
    prop: "operations",
    label: "操作",
    width: 170,
    slot: "operations",
    fixed: "right",
    align: "center",
  },
]);
let loading = ref(false);
let total = ref(0);
let currentPage = ref(1);
let pageSize = ref(10);
// 获取列表数据
const getProjectData = (params = {}) => {
  loading.value = true;
  proxy.$post({
    url: "/api/project/getYyqtzhsList",
    data: {
      page: currentPage.value,
      pagesize: pageSize.value,
      ...params,
    },
    callback: (data) => {
      tableData.value = data.rows;
      total.value = data.count;
      loading.value = false;
    },
  });
};
// 分页
const handleSizeChange = (size) => {
  pageSize.value = size;
  currentPage.value = 1;
  getProjectData();
};
const handleCurrentPageChange = (page) => {
  currentPage.value = page;
  getProjectData();
};
const investmentCecoveryAdd = () => {
  router.push("/investmentCecoveryAdd");
};
const editStatement = (item) => {
  router.push({
    name: "investmentCecoveryAdd",
    query: {
      id: item.id,
    },
  });
};
const previewStatement = (item) => {
  router.push({
    name: "investmentCecoveryAdd",
    query: {
      isPreview: true,
      id: item.id,
    },
  });
};
const deleteStatement = (item) => {
  ElMessageBox.confirm("确认删除该项？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.$post({
        url: "/api/project/deleteYyqtzhs",
        data: {
          id: item.id,
        },
        callback: (data) => {
          ElMessage.success("删除成功");
          getProjectData();
        },
      });
    })
    .catch(() => {});
};
onMounted(() => {
  getProjectData();
});
</script>

<style scoped lang="less"></style>
