<!-- 查询管理-其它查询-电子协议管理 -->
<template>
    <app-view>
        <app-filter :option="filterOption">

        </app-filter>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { formatDate } from '@/filters'
import { useRequest } from '@/hooks/request'
import { queryesignrecord } from '@/services/api/account'
import { ElMessage } from 'element-plus'
import { shallowRef } from 'vue'
import { useDataFilter } from '@/hooks/datatable-v2'
import { i18n } from '@/stores'
import { useOperation } from '@/hooks/operation';
import { useEnum } from '@/hooks/enum'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'

const { global: { t } } = i18n

const { getEnumOptions, getEnumTypeName } = useEnum('esignrecordstatus') // 签署状态
const esigntemplatetypeEnums = useEnum('esigntemplatetype') // 模板类型
const esigntypeEnums = useEnum('esigntype') // 协议类型

const dateValue = shallowRef<string[] | null>([])

const { componentMap, componentId, record, closeComponent } = useOperation<Model.SignRecordReq>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryesignrecord, {
    manual: true,
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'userfullname', label: 'query.other.esign.userfullname' },
    { field: 'loginid', label: 'query.other.esign.loginid' },
    { field: 'esigntype', label: 'query.other.esign.esigntype', formatValue: (val) => esigntypeEnums.getEnumTypeName(val) },
    { field: 'templatetype', label: 'query.other.esign.templatetype', formatValue: (val) => esigntemplatetypeEnums.getEnumTypeName(val) },
    { field: 'templatename', label: 'query.other.esign.templatename' },
    { field: 'recordstatus', label: 'query.other.esign.recordstatus', formatValue: (val) => getEnumTypeName(val) },
    { field: 'updatetimestr', label: 'query.other.esign.updatetimestr', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Model.SignRecordReq>({
    filters: [
        {
            field: 'userid',
            label: t('query.other.esign.userid'),
            placeholder: t('query.other.esign.tips1')
        },
        {
            field: 'recordstatus',
            label: t('query.other.esign.recordstatus'),
            placeholder: t('query.other.esign.tips2'),
            options: () => getEnumOptions()
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    return qs
}

const onSearch = () => {
    const qs = processRequiredParams()
    run(qs)
}

const onReset = () => {
    dateValue.value = []
    resetFilters()
}

</script>