<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="type">
                    <el-select v-model="formData.type" clearable placeholder="请选择类型" class="flex-1">
                        <el-option label="月报" :value="3" />
                        <el-option label="周报" :value="2" />
                        <el-option label="日报" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="接受人员" prop="type_user">
                    <el-input v-model="formData.type_user_names" @click="userclick" clearable placeholder="点击选择接受人员" />
                </el-form-item>
                <el-form-item label="汇报工作内容" prop="works">
                    <el-input v-model="formData.works" clearable placeholder="请输入汇报工作内容" type="textarea" />
                </el-form-item>
                <el-form-item label="计划工作内容" prop="plans">
                    <el-input v-model="formData.plans" clearable placeholder="请输入计划工作内容" type="textarea" />
                </el-form-item>
                <el-form-item label="其他事项" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入其他事项" type="textarea" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <UploadAnnex :form-data="formData"></UploadAnnex>
                </el-form-item>
            </el-form>
            <div v-if="showDialog">
                <personnelselector ref="personnel" @confirm="submituser" type="2">
                </personnelselector>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="oaWorkEdit">
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { apiOaWorkAdd, apiOaWorkEdit, apiOaWorkDetail } from "@/api/oa_work";
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 showDialog = ref(false)
const personnel = ref(null)

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == "edit" ? "编辑汇报工作表" : "新增汇报工作表";
});

// 表单数据
const formData = reactive({
    id: "",
    type: "",
    type_user: "",
    type_user_names: "",
    works: "",
    plans: "",
    remark: "",
    annex: [],
});

const userclick = async () => {
    showDialog.value = true
    await nextTick()
    personnel.value.open()
}

const submituser = (e: any) => {
    formData.type_user = e.map((item: any) => {
        return item.id
    }).join(',')
    formData.type_user_names = e.map((item: any) => {
        return item.name
    }).join(',')
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    type: [
        {
            required: true,
            message: "请输入类型：1 日报 2周报 3月报",
            trigger: ["blur"],
        },
    ],
    type_user: [
        {
            required: true,
            message: "请输入接受人员ID",
            trigger: ["blur"],
        },
    ],
    works: [
        {
            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];
        }
    }
};

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

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate();
    const data = { ...formData };
    mode.value == "edit" ? await apiOaWorkEdit(data) : await apiOaWorkAdd(data);
    popupRef.value?.close();
    emit("success");
};

//打开弹窗
const open = (type = "add") => {
    mode.value = type;
    popupRef.value?.open();
};

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

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