<template>
	<el-dialog v-model="currentVisible" :title="currentMode === AddMode.ADD_TASK ? '新增' : '修改'" width="610" center
		align-center @close="handleClose" draggable>
		<el-form :inline="true" v-if="currentMode === AddMode.ADD_TASK">
			<el-form-item label="任务名称:">
				<el-input v-model="taskName" placeholder="请输入任务名称" />
			</el-form-item>
			<el-form-item label="所属爬虫:">
				<el-select v-model="spiderTypeValue" placeholder="请选择所属爬虫" style="width: 181px">
					<el-option v-for="item in spiderTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="执行频率(秒/次):">
				<Crontab v-model="cronExpression"  style="width: 250px;" />
			</el-form-item>
		</el-form>
		<el-form v-if="currentMode === AddMode.UPDATE_TASK">
			<el-form-item label="执行频率(秒/次):">
				<Crontab v-model="cronExpression"  />
			</el-form-item>
			<!-- <el-form-item label="启用/停止:" v-if="currentMode === AddMode.UPDATE_TASK">
				<el-switch v-model="taskSwitchValue" class="ml-2"
					style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" />
			</el-form-item> -->
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" @click="handleConfirm">
					确定
				</el-button>
			</div>
		</template>

	</el-dialog>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { defineProps } from 'vue';
import { watch } from 'vue'
import { addSpiderTask } from '@/api/spiderTask'
import { AddMode } from './enum'
import { Crontab } from '@/components/Crontab/index'
interface addDialogPropType {
	dialogVisible: boolean,
	mode: AddMode,
	jobId: string,
	cron: string,
	spiderType: string,
	options: string,
	// frequency: string,
	// taskStatus: boolean
}
const props = defineProps<addDialogPropType>()

// 向父组件传递dialog值
const emit = defineEmits(['update:dialogVisible', 'confirm', 'update:mode', 'getTaskList'])
// 组件的状态
const currentVisible = ref(props.dialogVisible)
const currentMode = ref(props.mode)
const taskName = ref('')
const taskFrequency = ref('')
const taskTime = ref('')
const taskSwitchValue = ref(false)
const spiderTypeValue = ref('')
const spiderTypeOptions = ref([
	{
		value: 'api_spider',
		label: 'api_spider'
	},
	{
		value: 'dsn_now',
		label: 'dsn_now'
	},
	{
		value: 'itu_space_explorer',
		label: 'itu_space_explorer'
	},
])
const taskParams = ref({
	scrapyd_server_id: '1',
	schedule_type: '0',
	project: 'spiders'
})
const cronExpression = ref('')
// const shortcuts = [
// 	{ text: '每工作日9点', value: '0 0 9 ? * MON-FRI' },
// 	{ text: '每季度第一天', value: '0 0 0 1 1/3 ?' }
// ]
// 关闭的方法
const handleClose = () => {
	taskName.value = ''
	taskFrequency.value = ''
	cronExpression.value = ''
	currentVisible.value = false
}
// 确定的方法
const handleConfirm = async () => {
	if (currentMode.value === AddMode.ADD_TASK) {
		await addSpiderTask({
			scrapyd_server_id: taskParams.value.scrapyd_server_id,
			schedule_type: taskParams.value.schedule_type,
			project: taskParams.value.project,
			spider: spiderTypeValue.value,
			cron: cronExpression.value,
			options: JSON.stringify({ jobName: taskName.value })
		})
		currentVisible.value = false
		emit('getTaskList')
	}else if (currentMode.value === AddMode.UPDATE_TASK) {
		await addSpiderTask({
			scrapyd_server_id: taskParams.value.scrapyd_server_id,
			schedule_type: taskParams.value.schedule_type,
			project: taskParams.value.project,
			spider: props.spiderType,
			job_id: props.jobId,
			cron: cronExpression.value,
			options: props.options
		})
		currentVisible.value = false
		emit('getTaskList')
	}
}

// 监听props变化，同步给组件内部
watch(() => props.dialogVisible,
	(newVal) => {
		currentVisible.value = newVal
	}
)

// 监听组件内部状态变化，同步给父组件，这样才能做到控制对话框组件的显隐
watch(() => currentVisible.value,
	(newVal) => {
		emit('update:dialogVisible', newVal)
	}
)

watch(() => props.mode,
	(newVal) => {
		currentMode.value = newVal
	}
)

watch(() => currentMode.value,
	(newVal) => {
		emit('update:mode', newVal)
	}
)
watch(
	[() => props.dialogVisible, () => props.mode, () => props.cron],
	([newVisible, newMode,newCron]) => {
		if (newVisible && newMode === AddMode.UPDATE_TASK) {
			cronExpression.value = newCron;
		}
		if (!newVisible) {
			// 清空表单
			cronExpression.value =  '' 
		}
	},
	{ immediate: true }
);
onMounted(() => {

})
</script>

<style>
/* 修改弹出框样式 */
.el-dialog {
	background: transparent;
	background-image: url("@/assets/picture/dialog1.png");
	background-size: 100% 100%;
	background-repeat: no-repeat;
	color: #ffffff;

	.el-dialog__title {
		color: #ffffff;
	}

	.el-input__inner {
		color: #ffffff;
	}
}

/* 修改表单样式 */
.el-form {
	.el-form-item__label {
		color: #ffffff;
	}
}
</style>
