<template>
	<div ref='search' class='bg-white pt-4 pl-5 pr-5 border app-container'>
		<!--条件组-->
		<div class='searchAlign'>
			<el-form :inline='true' :model='searchForm'>
				<GdSelect :label='$t("applyDivision")' prop='i_sch_divsion' placeholder="" type='dict' dictKey='40_001'
					style='width: 250px' @change="getData" :clearable="false" />
				<GdInput :label='$t("applyInfo")' prop='i_sch_field' placeholder="" style='width: 250px' :maxlength="50" />
				<GdSelect :label='$t("status")' prop='i_sch_apply_status' placeholder="" :options="statusList"
					style='width: 250px' />
				<GdInput :label='$t("applicant")' prop='i_sch_apply_user' placeholder="" style='width: 250px' />
				<GdTreeSelect :label='$t("applicantDepartment")' prop='i_sch_dept' type='organize' placeholder=""
					style='width: 300px' />
			</el-form>
			<el-button icon="Search" @click='getData'>{{ $t("search_btn") }}</el-button>
		</div>

		<!--功能按钮-->
		<SearchBtnBar class="pb-4" style="display: inline-block" :permission='permission' @OnClickDelete=''
			@OnClickExport='onClickExport' @OnClickImport='' @onClickAdd=''></SearchBtnBar>
	</div>


	<!-- <div class='mt-2 flex-grow'> -->
	<div class='mt-2 flex-grow app-container'>
		<el-table :data='tableData' :height="tableHeight" border>
			<el-table-column label='No' prop='no' width="60" align='center' />
			<el-table-column :label='$t("applyID")' prop='i_apply_id' width='180' align='center'>
				<template #default='scope'>
					<el-link type='primary' @click='handleLink(scope.row)'>
						{{ scope.row.i_apply_id }}
					</el-link>
				</template>
			</el-table-column>
			<el-table-column :label='$t("applyName")' prop='i_apply_name' align='center' />
			<el-table-column :label='$t("status")' prop='i_apply_status_name' align='center' />
			<el-table-column :label='$t("applicant")' prop='i_apply_user' align='center' />
			<el-table-column :label='$t("applicantCompany")' prop='i_apply_company_name' align='center' />
			<el-table-column :label='$t("applicantDepartment")' prop='i_apply_dept_name' align='center' />
			<el-table-column :label='$t("submitTime")' prop='i_apply_date' align='center' />
			<el-table-column :label='$t("operation")' align='center' v-if="searchForm.i_sch_divsion === '0'">
				<template #default="scope">
					<el-button text v-if="scope.row.i_apply_status === AuditStatus.PASSED" type="primary" icon='DocumentCopy'
						@click='handleCopy(scope.row)'>
						{{ $t("copyNew") }}
					</el-button>
				</template>
			</el-table-column>
		</el-table>
	</div>
	<!--分页-->
	<!-- <div ref='pagination' class='mt-2 flex flex-row-reverse'> -->
	<div ref='pagination' class='mt-2 flex flex-row-reverse app-container pagination-container'>
		<el-pagination v-model:current-page="searchForm.i_pageindex" v-model:page-size="searchForm.i_perpage"
			:page-sizes="pageSizeList" layout="total, sizes, prev, pager, next, jumper" :total="dataCount"
			@size-change="getData" @current-change="getData" />
	</div>
</template>

<script>
import { AuditStatus } from '@/model/enums'
import SearchBtnBar from '@/components/GdElement/BtnBar.vue';
import { createDownloadFile } from '@/utils/exportExcel';
import { getWorkFlowSearchableListInit, getWorkFlowSearchableList, exportWorkFlowSearchableList } from '@/api/workflowManagement'
import { ref, computed } from 'vue'
import { useGlobalPinia } from '@/pinia/global'
import { useWindowSize, useElementBounding } from '@vueuse/core'
import { ElMessage } from 'element-plus'
import router from '@/router';

export default {
	// name: 'WFApplyListAll',
	name: 'WfApplyListAll',
	components: { SearchBtnBar },
	data() {
		return {
			startDate: null,
			endDate: null,
			searchForm: {
				i_sch_divsion: '0',
				i_sch_field: undefined,
				i_sch_apply_status: undefined,
				i_sch_apply_user: undefined,
				i_sch_dept: undefined,
				i_perpage: 10,
				i_pageindex: 1
			},
			permission: '',
			tableData: [],
			dataCount: 1,
			pageSizeList: [],
			AuditStatus,
			statusList: []
		}
	},
	mounted() {
		getWorkFlowSearchableListInit().then(res => {
			if (res.resultCode == '0') {
				const { applyStatusList, perpageList, i_perpage, ...permission } = res.data
				this.permission = permission
				this.statusList = applyStatusList
				this.pageSizeList = perpageList.map(page => { return page.value })
				this.searchForm.i_perpage = Number(i_perpage) || 10
				this.getData()
			}
		})
	},
	activated() {
		this.getData()
	},
	setup() {
		const windowSize = useWindowSize();
		const search = ref();
		const searchSize = useElementBounding(search);
		const pagination = ref();
		const paginationSize = useElementBounding(pagination);
		const global = useGlobalPinia();
		// const tableHeight = computed(() => windowSize.height.value - searchSize.height.value - global.header - global.mainPadding - 16 - paginationSize.height.value + 'px');
		const tableHeight = computed(() => windowSize.height.value - searchSize.height.value - global.header - global.mainPadding - 241 - paginationSize.height.value + 'px');
		return { tableHeight, search, pagination, windowSize, global }
	},
	methods: {
		//获取列表数据
		getData() {
			getWorkFlowSearchableList(this.searchForm).then(res => {
				if (res.resultCode === '0') {
					this.tableData = res.dataList
					this.dataCount = Number(res.i_total) || 0
				}
			})
		},
		getPeriod(row) {
			return (row.i_use_s_ymd || "") + '~' + (row.i_use_e_ymd || "")
		},
		handleLink({ i_form_id, i_apply_id }) {
			// this.$router.push({ name: 'WFApplyDetail', query: { formId: i_form_id, dataId: i_apply_id } })
			router.push({path:'wfApplyDetail', query: { formId: i_form_id, dataId: i_apply_id }})
		},
		handleCopy({ i_form_id, i_apply_id, i_form_status }) {
			if (i_form_status === '2') {
				ElMessage.warning(this.$t('401040_0002'))
				return
			}
			// this.$router.push({ name: 'WFApplyCreate', query: { formId: i_form_id, dataId: i_apply_id, isCopy: '1' } })
			router.push({path:'wfApplyCreate', query: { formId: i_form_id, dataId: i_apply_id, isCopy: '1' }})
		},
		async onClickExport() {
			const table_info = [
				{ label: 'no', value: 'No', index: 1 },
				{ label: 'i_apply_id', value: this.$t("applyID"), index: 2 },
				{ label: 'i_apply_name', value: this.$t("applyName"), index: 3 },
				{ label: 'i_apply_status_name', value: this.$t("status"), index: 4 },
				{ label: 'i_apply_user', value: this.$t("applicant"), index: 5 },
				{ label: 'i_apply_company_name', value: this.$t("applicantCompany"), index: 6 },
				{ label: 'i_apply_dept_name', value: this.$t("applicantDepartment"), index: 7 },
				{ label: 'i_apply_date', value: this.$t("submitTime"), index: 8 },
			];
			const res = await exportWorkFlowSearchableList({
				...this.searchForm,
				table_info: table_info,
			});

			createDownloadFile(res);
		}
	},
}
</script>

<style scoped>
.searchBar {
	display: flex;
	align-items: center;
	align-content: center;
}

.datePicker {
	width: 250px;
}

.searchAlign {
	display: flex;
	align-content: flex-start;
}

.el-card {
	--el-card-padding: 10px !important;
}
</style>
