<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <search-form @search="handleSearch" />

      <div class="manage-header">
        <div class="header-left"></div>
        <div class="header-right">
          <el-button type="primary" @click="turnoverAdd" v-if="showAddBtn">新增</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, 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";
import { useProjectList } from "@/composables/useProjectList";

const { projectLzType } = defineProps({
  projectLzType: [String, Number],
});
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: "zbyzyjLen",
    label: "资本处置移交",
    showOverflowTooltip: true,
  },
  {
    prop: "qzlyzzyjLen",
    label: "期中履约终止移交",
    showOverflowTooltip: true,
  },

  {
    prop: "qmyjLen",
    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/getYjglList",
    data: {
      page: currentPage.value,
      pagesize: pageSize.value,
      ...params,
    },
    callback: (data) => {
      const countValidRows = (arr) => {
        if (!Array.isArray(arr)) return 0;
        return arr.reduce((count, item) => {
          if (Array.isArray(item?.fj) && item.fj.length > 0) {
            count++;
          }
          return count;
        }, 0);
      };
      tableData.value = data.rows.map((it) => {
        return {
          ...it,
          qmyjLen: `${countValidRows(it.qmyj)}行`,
          qzlyzzyjLen: `${countValidRows(it.qzlyzzyj)}行`,
          zbyzyjLen: `${countValidRows(it.zbyzyj)}行`,
        };
      });

      total.value = data.count;
      loading.value = false;
    },
  });
};
const { showAddBtn } = useProjectList(projectLzType, getProjectData);
// 分页
const handleSizeChange = (size) => {
  pageSize.value = size;
  currentPage.value = 1;
  getProjectData();
};
const handleCurrentPageChange = (page) => {
  currentPage.value = page;
  getProjectData();
};
const turnoverAdd = () => {
  router.push("/turnoverAdd");
};
const editStatement = (item) => {
  router.push({
    name: "turnoverAdd",
    query: {
      id: item.id,
    },
  });
};
const previewStatement = (item) => {
  router.push({
    name: "turnoverAdd",
    query: {
      isPreview: true,
      id: item.id,
    },
  });
};
const deleteStatement = (item) => {
  ElMessageBox.confirm("确认删除该项？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.$post({
        url: "/api/project/deleteYjgl",
        data: {
          id: item.id,
        },
        callback: (data) => {
          ElMessage.success("删除成功");
          getProjectData();
        },
      });
    })
    .catch(() => {});
};
</script>

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