<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { columns } from '@/views/financial-user/debt/debt-tracking/utils/columns';
import PureTableBar from '@/components/TableBar/src/bar';
import AddFill from '@iconify-icons/ri/add-circle-line';
import PureTable from '@pureadmin/table';
import { deleteIds, onAdd, onDelete, onDeleteBatch, onSearch, onUpdate } from '@/views/financial-user/debt/debt-tracking/utils/hooks';
import Delete from '@iconify-icons/ep/delete';
import EditPen from '@iconify-icons/ep/edit-pen';
import Refresh from '@iconify-icons/ep/refresh';
import { $t } from '@/plugins/i18n';
import { useRenderIcon } from '@/components/CommonIcon/src/hooks';
import { FormInstance } from 'element-plus';
import { selectUserinfo } from '@/components/Table/Userinfo/columns';
import { useAdminUserStore } from '@/store/system/adminUser';
import { debtTracking, debtType } from '@/enums/bill/debtTracking';
import { useDebtTrackingUserStore } from '@/store/financialUser/debtTrackingUser';

const tableRef = ref();
const formRef = ref();
// 用户信息列表
const userDataList = ref();
// 搜索用户加载
const loading = ref(false);
const adminUserStore = useAdminUserStore();
const debtTrackingUserStore = useDebtTrackingUserStore();

/** 当前页改变时 */
const onCurrentPageChange = async (value: number) => {
	debtTrackingUserStore.pagination.currentPage = value;
	await onSearch();
};

/** 当分页发生变化 */
const onPageSizeChange = async (value: number) => {
	debtTrackingUserStore.pagination.pageSize = value;
	await onSearch();
};

/** 选择多行 */
const onSelectionChange = (rows: Array<any>) => {
	deleteIds.value = rows.map((row: any) => row.id);
};

/** 重置表单 */
const resetForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	await onSearch();
};

onMounted(() => {
	onSearch();
});
</script>

<template>
	<div class="main">
		<el-form ref="formRef" :inline="true" :model="debtTrackingUserStore.form" class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto">
			<!-- 债务人姓名 -->
			<el-form-item :label="$t('debtorName')" prop="debtorName">
				<el-input v-model="debtTrackingUserStore.form.debtorName" :placeholder="`${$t('input')}${$t('debtorName')}`" class="!w-[180px]" clearable />
			</el-form-item>

			<!-- 债务金额 -->
			<el-form-item :label="$t('debtAmount')" prop="debtAmount">
				<el-input v-model="debtTrackingUserStore.form.debtAmount" :placeholder="`${$t('input')}${$t('debtAmount')}`" class="!w-[180px]" clearable />
			</el-form-item>

			<!-- 债务类型 -->
			<el-form-item :label="$t('debtType')" prop="debtType">
				<el-select v-model="debtTrackingUserStore.form.debtType" :placeholder="$t('debtType')" class="!w-[180px]" clearable filterable remote remote-show-suffix>
					<el-option v-for="item in debtType" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>

			<!-- 债务状态 -->
			<el-form-item :label="$t('debtStatus')" prop="debtStatus">
				<el-select
					v-model="debtTrackingUserStore.form.debtStatus"
					:placeholder="$t('debtStatus')"
					class="!w-[180px]"
					clearable
					filterable
					remote
					remote-show-suffix
				>
					<el-option v-for="item in debtTracking" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>

			<!-- 还款截止日期 -->
			<el-form-item :label="$t('dueDate')" prop="dueDate">
				<el-date-picker
					v-model="debtTrackingUserStore.form.dueDate"
					:end-placeholder="$t('endTime')"
					:placeholder="$t('dueDate')"
					:start-placeholder="$t('startTime')"
					class="!w-[210px]"
					time-format="YYYY-MM-DD"
					type="date"
					value-format="YYYY-MM-DD"
				/>
			</el-form-item>

			<!-- 搜索和重置 -->
			<el-form-item>
				<el-button :icon="useRenderIcon('ri:search-line')" :loading="debtTrackingUserStore.loading" type="primary" @click="onSearch">
					{{ $t('search') }}
				</el-button>
				<el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)"> {{ $t('buttons.reset') }}</el-button>
			</el-form-item>
		</el-form>

		<PureTableBar :columns="columns" :title="$t('debtTracking')" @fullscreen="tableRef.setAdaptive()" @refresh="onSearch">
			<template #buttons>
				<el-button :icon="useRenderIcon(AddFill)" type="primary" @click="onAdd"> {{ $t('addNew') }}</el-button>

				<!-- 批量删除按钮 -->
				<el-button v-show="deleteIds.length > 0" :icon="useRenderIcon(Delete)" type="danger" @click="onDeleteBatch">
					{{ $t('delete_batches') }}
				</el-button>
			</template>

			<template v-slot="{ size, dynamicColumns }">
				<pure-table
					ref="tableRef"
					:adaptiveConfig="{ offsetBottom: 96 }"
					:columns="dynamicColumns"
					:data="debtTrackingUserStore.datalist"
					:header-cell-style="{ background: 'var(--el-fill-color-light)', color: 'var(--el-text-color-primary)' }"
					:loading="debtTrackingUserStore.loading"
					:pagination="debtTrackingUserStore.pagination"
					:size="size"
					adaptive
					align-whole="center"
					border
					highlight-current-row
					row-key="id"
					showOverflowTooltip
					table-layout="auto"
					@page-size-change="onPageSizeChange"
					@selection-change="onSelectionChange"
					@page-current-change="onCurrentPageChange"
				>
					<template #debtType="{ row }">
						<component :is="debtType.find(item => item.value === row.debtType).el" />
					</template>

					<template #debtStatus="{ row }">
						<component :is="debtTracking.find(item => item.value === row.debtStatus).el" />
					</template>

					<template #createUser="{ row }">
						<el-button v-show="row.createUser" link type="primary" @click="selectUserinfo(row.createUser)">
							{{ row.createUsername }}
						</el-button>
					</template>

					<template #username="{ row }">
						<el-button v-show="row.username" link type="primary" @click="selectUserinfo(row.userId)">
							{{ row.username }}
						</el-button>
					</template>

					<template #updateUser="{ row }">
						<el-button v-show="row.updateUser" link type="primary" @click="selectUserinfo(row.updateUser)">
							{{ row.updateUsername }}
						</el-button>
					</template>

					<template #operation="{ row }">
						<el-button :icon="useRenderIcon(EditPen)" :size="size" class="reset-margin" link type="primary" @click="onUpdate(row)"> {{ $t('modify') }} </el-button>
						<el-popconfirm :title="`${$t('delete')}?`" @confirm="onDelete(row)">
							<template #reference>
								<el-button :icon="useRenderIcon(Delete)" :size="size" class="reset-margin" link type="primary">
									{{ $t('delete') }}
								</el-button>
							</template>
						</el-popconfirm>
					</template>
				</pure-table>
			</template>
		</PureTableBar>
	</div>
</template>
