<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">

			<el-row class="page-title-row mb15">
				<div class="page-title">{{ t('projectExitFeedbackLibrary.title') }}</div>
			</el-row>

			<el-row class="toolbar-row mb15" justify="space-between" align="middle">
				<!-- <div class="toolbar-left">
					<el-button icon="Download" @click="handleExport">{{ t('common.exportBtn') || '导出' }}</el-button>
					<el-button type="primary" plain icon="Delete" :disabled="!selectedRows.length" @click="handleBatchDelete">{{
						t('common.delBtn') || '删除'
					}}</el-button>
				</div> -->
				<el-form :model="queryForm" :inline="true" class="search-form" @submit.prevent>
					<el-form-item :label="t('projectExitFeedback.form.projectName')">
						<el-input v-model="queryForm.projectName" :placeholder="t('projectExitFeedback.placeholder.input')" style="width: 200px" />
					</el-form-item>
					<el-form-item :label="t('projectExitFeedback.form.exitExecutor')">
						<el-input v-model="queryForm.exitExecutor" :placeholder="t('projectExitFeedback.placeholder.input')" style="width: 200px" />
					</el-form-item>
					<!-- <el-form-item :label="t('projectExitFeedback.form.exitDescription')">
						<el-input v-model="queryForm.exitRemark" :placeholder="t('projectExitFeedback.placeholder.input')" style="width: 200px" />
					</el-form-item> -->
					<el-form-item>
						<el-button type="primary" icon="Search" @click="handleSearch">{{ t('common.queryBtn') || '查询' }}</el-button>
						<el-button icon="Refresh" @click="handleReset">{{ t('common.resetBtn') || '重置' }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>

			<el-table border :data="tableData" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="48" />
				<el-table-column type="index" :label="t('projectExitPlanLibrary.table.index')" width="60" />
				<el-table-column prop="projectName" :label="t('projectExitFeedback.form.projectName')" min-width="180" />
				<el-table-column prop="exitTime" :label="t('projectExitFeedback.form.exitTime')" min-width="160" />
				<el-table-column prop="exitExecutor" :label="t('projectExitFeedback.form.exitExecutor')" min-width="140" />
				<el-table-column prop="exitRemark" :label="t('projectExitFeedback.form.exitDescription')" min-width="160" />
				<el-table-column prop="exitAttachment" :label="t('projectExitFeedback.form.exitFiles')" min-width="200">
					<template #default="{ row }">
						<UploadFile :modelValue="row.exitAttachment ? JSON.parse(row.exitAttachment as string) : []" :fileSize="20" disabled type="simple" :limit="10" :isShowTip="false" />
					</template>
				</el-table-column>
				<el-table-column :label="t('projectExitPlanLibrary.table.action')" fixed="right" width="140">
					<template #default="{ row }">
						<el-button link type="primary" @click="handleView(row)">{{ t('common.viewBtn') || '查看' }}</el-button>
					</template>
				</el-table-column>
			</el-table>

			<div class="mt15 flex justify-end pagination-wrapper">
				<el-pagination
					background
					layout="sizes, prev, pager, next, jumper, total"
					:total="total"
					:page-sizes="[10, 20, 50, 100]"
					v-model:current-page="queryForm.page"
					v-model:page-size="queryForm.size"
					@size-change="handlePageSizeChange"
					@current-change="handlePageChange"
				/>
			</div>
		</div>
		<el-drawer v-model="visible" size="60%" :close-on-click-modal="false">
			<ProjectExitFeedbackDetail :model-value="detailForm" title="项目退出计划申请表"/>
		</el-drawer>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useMessage } from '/@/hooks/message';
import { getProjectExitPlanFeedbackPage } from '/@/api/investment/projectExitPlan';
import ProjectExitFeedbackDetail from '/@/components/investment/common/ProjectExitFeedbackDetail.vue';
import type { paramsDataProjectExitPlanFeedback, projectExitPlanFeedbackItem } from '../projectExitFeedback/interface/type';

interface ProjectExitPlanLibraryItem {
	id: number;
	projectName: string;
	exitSuggestion: string;
	exitMethod: string;
	executionTask: string;
	taskDescription: string;
	executor: string;
	taskStartTime: string;
	taskEndTime: string;
	applyDepartment: string;
	submitMatter: string;
}

const { t } = useI18n();
const message = useMessage();

const queryForm = ref<paramsDataProjectExitPlanFeedback>({
	projectName: '',
	exitExecutor: '',
	exitRemark: '',
	page: 1,
	size: 10,
});

const total = ref(0);
const originData = ref<ProjectExitPlanLibraryItem[]>([]);
const tableData = ref<projectExitPlanFeedbackItem[]>([]);
const selectedRows = ref<ProjectExitPlanLibraryItem[]>([]);
const detailForm = ref({})
const visible = ref(false)

const getDataList = () => {
	getProjectExitPlanFeedbackPage(queryForm.value).then((res) => {
		tableData.value = res.data.records;
		total.value = res.data.total;
	});
};

const handleSearch = () => {
	queryForm.value.page = 1;
	getDataList();
};

const handleReset = () => {
	queryForm.value.projectName = '';
	queryForm.value.exitExecutor = '';
	queryForm.value.exitRemark = '';
	queryForm.value.page = 1;
	getDataList();
};

const handleSelectionChange = (rows: ProjectExitPlanLibraryItem[]) => {
	selectedRows.value = rows;
};

const handleExport = () => {
	message.info(t('projectExitPlanLibrary.message.exportInfo'));
};

const handleBatchDelete = () => {
	if (!selectedRows.value.length) return;
	const ids = selectedRows.value.map((item) => item.id);
	originData.value = originData.value.filter((item) => !ids.includes(item.id));
	selectedRows.value = [];
	if (tableData.value.length === 0 && queryForm.value.page > 1) {
		queryForm.value.page -= 1;
	}
	getDataList();
	message.success(t('projectExitPlanLibrary.message.deleteSuccess'));
};

const handleView = (row: ProjectExitPlanLibraryItem) => {
	detailForm.value = row;
	visible.value = true;
};

const handleDownload = (row: ProjectExitPlanLibraryItem) => {
	message.success(`${t('projectExitPlanLibrary.message.downloadInfo')}：${row.projectName}`);
};

const handlePageChange = (page: number) => {
	queryForm.value.page = page;
	getDataList();
};

const handlePageSizeChange = (size: number) => {
	queryForm.value.size = size;
	queryForm.value.page = 1;
	getDataList();
};

const handleBack = () => {
	history.length > 1 ? history.back() : message.info(t('projectExitPlanLibrary.message.backInfo'));
};

getDataList();
</script>

<style scoped>
.page-breadcrumb {
	align-items: center;
	gap: 6px;
}

.breadcrumb-divider {
	color: var(--el-text-color-placeholder);
}

.breadcrumb-current {
	font-size: 14px;
	color: var(--el-text-color-secondary);
}

.page-title-row {
	display: flex;
	justify-content: center;
}

.page-title {
	font-size: 20px;
	font-weight: 600;
}

.toolbar-row {
	display: flex;
	flex-wrap: wrap;
	gap: 12px;
}

.toolbar-left {
	display: flex;
	align-items: center;
	gap: 12px;
}

.search-form {
	flex: 1;
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 6px;
	justify-content: flex-end;
}

.search-form :deep(.el-form-item) {
	margin-bottom: 0;
}

.pagination-wrapper {
	width: 100%;
}
</style>
