<template>
    <div class="default-main">
        <div>
            <TableHeader ref="TableHeaderRef">
                <template #select>
                    <el-form-item label="任务名称">
                        <el-input v-model="tableStore.table.params.searchValue" maxlength="32" show-word-limit clearable
                            placeholder="请输入任务名称" />
                    </el-form-item>
                    <el-form-item label="任务状态:">
                        <el-select v-model="tableStore.table.params.searchState" clearable placeholder="请选择任务状态">
                            <el-option label="运行" :value="1"></el-option>
                            <el-option label="停止" :value="2"></el-option>
                        </el-select>
                    </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" @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 '@/views/system/scheduledTasks/form.vue'
import { formatDate } from '@/utils/formatTime'
import { ElMessage, ElMessageBox, ElDatePicker } from 'element-plus'
import { timerRun, runTimer, deleteTimer, stop, start } from '@/api/system-boot/csDictData'
defineOptions({
    name: 'Distributedphotovoltaic/templateConfiguration'
})
const formDate = ref({
    date: '',
    id: ''
})
const taskFormRef = ref()
const dialogVisible = ref(false)
const selectedTime = ref('')
const tableStore: any = new TableStore({
    url: '/system-boot/timer/list',
    method: 'POST',
    isWebPaging: true,
    column: [
        { field: 'timerName', title: '任务名称', },
        { field: 'actionClass', title: '任务执行器' },
        { field: 'cron', title: '定时任务表达式' },
        { field: 'remark', title: '备注' },
        // {
        //     field: 'jobStatus', title: '状态', width: '100',
        //     render: 'tag',
        //     custom: {
        //         0: 'danger',
        //         1: 'success',
        //         2: 'danger'
        //     },
        //     replaceValue: {
        //         0: '未启动',
        //         1: '运行中',
        //         2: '未启动',
        //     }
        // },
        {
            title: '状态',
            render: 'switch',
            width: 100,
            field: 'jobStatus',
            activeText: '运行中',
            activeValue: '1',
            inactiveText: '未启动',
            inactiveValue: '2',
            onChangeField: (row: any, value: any) => {
                if (row.jobStatus == 1) {
                    stop({ id: row.id }).then(res => {
                        ElMessage.success(res.message)
                        tableStore.index()
                    })
                }else{
                    start({ id: row.id }).then(res => {
                        ElMessage.success(res.message)
                        tableStore.index()
                    })  
                }


            }
        },
        { field: 'sort', title: '排序', width: '80' },
        {
            title: '操作',
            width: '220',
            render: 'buttons',
            buttons: [
                {
                    name: 'primary',
                    title: '执行',
                    type: 'primary',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'primary',
                        title: '是否确认执行该定时任务'
                    },
                    click: row => {
                        timerRun({ id: row.id }).then(res => {
                            ElMessage.success('执行成功')
                            tableStore.index()

                        })
                    }
                },
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',

                    click: async row => {
                        taskFormRef.value.open('编辑定时任务', row)
                    }
                },
                {
                    name: 'edit',
                    title: '补招',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',

                    click: async row => {
                        formDate.value.date = formatDate(new Date(), 'YYYY-MM-DD')
                        formDate.value.id = row.id
                        dialogVisible.value = true
                    }
                },
                {
                    name: 'del',
                    text: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除？'
                    },
                    click: row => {
                        deleteTimer([row.id]).then(res => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                },




            ]
        }
    ],

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

provide('tableStore', tableStore)

const add = () => {

    setTimeout(() => {
        taskFormRef.value.open('新增定时任务')
    }, 10)
}
const okRun = () => {
    runTimer(formDate.value).then(res => {
        ElMessage.success('操作成功')
        dialogVisible.value = false
        tableStore.index()
    })
}
onMounted(() => {
    tableStore.index()
})
</script>
