<template>
    <div class="default-main">
        <div>
            <TableHeader ref="TableHeaderRef">
                <template #select>
                    <el-form-item label="icd名称">
                        <el-input
                            v-model="tableStore.table.params.searchValue"
                            maxlength="32"
                            show-word-limit
                            clearable
                            placeholder="请输入任务名称"
                        />
                    </el-form-item>
                </template>
                <template #operation>
                    <el-button icon="el-icon-Plus" type="primary" @click="add">新增</el-button>
                </template>
            </TableHeader>
            <Table ref="tableRef" />
        </div>
        <!-- 新增 -->
        <taskForm ref="taskFormRef" v-if="taskFormFlag" @submit="tableStore.index()" />
        <!-- 补招 -->
        <el-dialog v-model="dialogVisible" title="选择补招时间" width="500">
            <el-date-picker
                v-model="formDate.date"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="选择日期"
            ></el-date-picker>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="okRun">确认</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import taskForm from './form.vue'
import { getFileNameAndFilePath } from '@/api/system-boot/file'
import { ElMessage, ElMessageBox, ElDatePicker } from 'element-plus'
import { timerRun, runTimer, deleteTimer, stop, start } from '@/api/system-boot/csDictData'
import { delIcdPath } from '@/api/device-boot/icd'
defineOptions({
    name: 'icd'
})
const formDate = ref({
    date: '',
    id: ''
})
const taskFormRef = ref()
const dialogVisible = ref(false)
const taskFormFlag = ref(false)
const tableStore: any = new TableStore({
    url: '/device-boot/icd/pageIcdList',
    method: 'POST',
    isWebPaging: true,
    column: [
        {
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { field: 'name', title: 'icd名称' },
        { field: 'path', title: 'icd路径', minWidth: 250 },
        { field: 'createBy', title: '创建人' },
        { field: 'createTime', title: '创建时间' },
        { field: 'updateBy', title: '修改人' },
        { field: 'updateTime', title: '修改时间' },
        {
            title: '操作',
            width: '220',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',

                    click: async row => {
                        taskFormFlag.value = true
                        setTimeout(() => {
                            taskFormRef.value.open('编辑icd文件', row)
                        }, 10)
                    }
                },
                {
                    name: 'edit',
                    title: '下载',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: async row => {
                        getFileNameAndFilePath({ filePath: row.path }).then(res => {
                            const link = document.createElement('a')
                            link.href = res.data.url
                            link.download = res.data.name
                            document.body.appendChild(link)
                            link.click()
                            document.body.removeChild(link)
                        })
                    }
                },

                {
                    name: 'del',
                    text: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除？'
                    },
                    click: row => {
                        delIcdPath([row.id]).then(res => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],

    loadCallback: () => {
        taskFormFlag.value = false
    }
})
tableStore.table.params.searchValue = ''
tableStore.table.params.searchState = ''

provide('tableStore', tableStore)

const add = () => {
    taskFormFlag.value = true
    setTimeout(() => {
        taskFormRef.value.open('新增icd文件')
    }, 10)
}
const okRun = () => {
    runTimer(formDate.value).then(res => {
        ElMessage.success('操作成功')
        dialogVisible.value = false
        tableStore.index()
    })
}
onMounted(() => {
    tableStore.index()
})
</script>
