<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="工作记录主题" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入工作记录主题" />
                </el-form-item>
                <el-form-item label="日期">
                    <el-date-picker v-model="formData.date" value-format="YYYY-MM-DD" placeholder="选择日期"
                        class="!flex flex-1"></el-date-picker>
                </el-form-item>
                <el-form-item label="开始时间" prop="start_time">
                    <el-time-picker v-model="formData.start_time" placeholder="选择开始时间" class="flex-1 !flex" />
                </el-form-item>
                <el-form-item label="结束时间" prop="end_time">
                    <el-time-picker v-model="formData.end_time" placeholder="选择结束时间" class="flex-1 !flex" />
                </el-form-item>
                <el-form-item label="工作类别" prop="cid">
                    <el-select v-model="formData.cid" clearable placeholder="请选择工作类别" class="flex-1">
                        <el-option label="接待客户" :value="7" />
                        <el-option label="拜访客户" :value="6" />
                        <el-option label="参加会议" :value="5" />
                        <el-option label="需求沟通" :value="4" />
                        <el-option label="需求调研" :value="3" />
                        <el-option label="撰写文档" :value="2" />
                        <el-option label="方案策划" :value="1" />
                        <el-option label="其他" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item label="工作类型" prop="labor_type">
                    <el-select v-model="formData.labor_type" clearable placeholder="请选择工作类别" class="flex-1">
                        <el-option label="案头工作" :value="1" />
                        <el-option label="外勤工作" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="formData.remark" clearable placeholder="请输入描述" type="textarea" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="oaScheduleEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiOaScheduleAdd, apiOaScheduleEdit, apiOaScheduleDetail } from '@/api/oa_schedule'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工作记录' : '新增工作记录'
})

// 表单数据
const formData = reactive({
    id: '',
    title: '',
    start_time: '',
    end_time: '',
    cid: '',
    labor_type: '',
    remark: '',
    date: ""
})

const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.end_time) < new Date(formData.start_time)) {
        callback(new Error('结束时间不能早于开始时间'))
    } else {
        callback()
    }
}

// 表单验证
const formRules = reactive<any>({
    title: [{
        required: true,
        message: '请输入工作记录主题',
        trigger: ['blur']
    }],
    start_time: [{
        required: true,
        message: '请输入开始时间',
        trigger: ['blur']
    }],
    end_time: [{
        required: true,
        message: '请输入结束时间',
        trigger: ['blur'],
    },
    {
        validator: chekcDate,
        trigger: ['blur']
    }
    ],
    cid: [{
        required: true,
        message: '请选择工作类别',
        trigger: ['blur']
    }],
    labor_type: [{
        required: true,
        message: '请输入工作类型',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入描述',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    formData.date = timeFormat(new Date(formData.start_time), 'yyyy-mm-dd')
}






const getDetail = async (row: Record<string, any>) => {
    const data = await apiOaScheduleDetail({
        id: row.id
    })
    setFormData(data)
}




function getHour(params) {
    const now = new Date(params);
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const formattedHours = hours < 10 ? '0' + hours : hours;
    const formattedMinutes = minutes < 10 ? '0' + minutes : minutes;
    const timeString = `${formattedHours}:${formattedMinutes}`;
    return String(' ' + timeString)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    formData.start_time = formData.date + ' ' + timeFormat(new Date(formData.start_time), 'hh:MM')
    formData.end_time = formData.date + ' ' + timeFormat(new Date(formData.end_time), 'hh:MM')
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiOaScheduleEdit(data)
        : await apiOaScheduleAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add', start_time = '') => {
    mode.value = type
    formData.date = start_time
    popupRef.value?.open()
}


// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
