<template>
	<pi-table ref="tableRef" :api="api.system.crontab_log.list" :params="search" @selection-change="selectionChange">
		<template #do>
			<el-button v-auth="'crontab:log:del'" type="danger" plain icon="el-icon-delete"
					   :disabled="selection.length===0" @click="batch_del">删除</el-button>
			<el-button v-auth="'crontab:log:empty'" type="danger" plain icon="el-icon-refresh-right"
					   @click="batch_empty">清空</el-button>
		</template>
		<template #search>
			<pi-select v-model="search.crontab_id" :api="api.system.crontab.option" placeholder="任务名称" clearable
					   :props="{label: 'crontab_name', value:'crontab_id'}" style="width: 200px;"></pi-select>
			<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
		</template>
		<el-table-column type="selection" width="50"></el-table-column>
		<el-table-column label="日志编号" prop="log_id"></el-table-column>
		<el-table-column label="任务名称" prop="crontab_name"></el-table-column>
		<el-table-column label="调用目标" prop="callback"></el-table-column>
		<el-table-column label="执行结果" prop="result"></el-table-column>
		<el-table-column label="执行状态" prop="status"></el-table-column>
		<el-table-column label="执行耗时" prop="duration"></el-table-column>
		<el-table-column label="执行时间" prop="create_time"></el-table-column>
		<el-table-column label="操作" fixed="right" align="right" width="100">
			<template #default="scope">
				<el-button-group>
					<el-popconfirm title="确定删除吗？" @confirm="del(scope.row, scope.$index)">
						<template #reference>
							<el-button v-auth="'crontab:log:del'" text type="danger" size="small">删除</el-button>
						</template>
					</el-popconfirm>
				</el-button-group>
			</template>
		</el-table-column>
	</pi-table>
</template>

<script setup>
import api from "@/api/index";
import {getCurrentInstance, ref} from "vue";
import piSelect from "@/components/piSelect"
import {useRoute} from 'vue-router'

defineOptions({
	name: "monitorCrontabLog"
})

const {proxy} = getCurrentInstance()
const tableRef = ref(null)
const route = useRoute()

let search = ref({
	crontab_id: route.query.crontab_id ? parseInt(route.query.crontab_id) : null
})
let selection = ref([])

//搜索
function upsearch() {
	tableRef.value.upData(search.value)
}

function selectionChange(e){
	selection.value = e;
}

async function batch_empty() {
	proxy.$confirm(`确定清空所有日志吗？`, '提示', {
		type: 'warning'
	}).then(async () => {
		const loading = proxy.$loading();
		const res = await api.system.crontab_log.empty(search.value);
		tableRef.value.refresh()
		loading.close();
		proxy.$message.success(res.msg)
	})
}

async function batch_del() {
	proxy.$confirm(`确定删除选中的 ${selection.value.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
		type: 'warning'
	}).then(async () => {
		const loading = proxy.$loading();
		const res = await api.system.crontab_log.del({ids: selection.value.map(item => item.log_id)});
		tableRef.value.refresh()
		loading.close();
		proxy.$message.success(res.msg)
	})
}

async function del(row) {
	const loading = proxy.$loading();
	const res = await api.system.crontab_log.del({ids: [row.log_id]});
	tableRef.value.refresh()
	loading.close();
	proxy.$message.success(res.msg)
}
</script>
