<template>
    <el-container>
        <el-main class="nopadding">
            <el-form label-width="100px" :rules="rules" :model="formData" style="padding: 20px;" :disabled="localMode === 'show'">
               <el-form-item label="员工信息" required>
                    <xtSelect
                        v-model="formData.empperson"
                        style="width: 230px;"
                        :apiObj = "$API.hrm.empperson.list"
                        v-model:label="formData.employee_name"
                        :edit="localMode === 'add'"
                        @change="handleChange">
                        <el-table-column prop="name" label="姓名"></el-table-column>
                    </xtSelect>
                </el-form-item>
               <el-form-item label="部门" required>
                    <el-cascader
                        v-model="formData.reg_dept"
                        :options="group"
                        :props="groupsProps"
                        :show-all-levels="false"
                        clearable
                        style="width: 230px"
                    ></el-cascader>
                </el-form-item>
            <el-form-item label="岗位" required>
                <xtSelect
                    :apiObj="apiObjPost"
                    v-model="formData.reg_post"
                    v-model:label="formData.reg_post_name"
                    style="width: 230px"
                    >
                    <el-table-column label="名称" prop="name"></el-table-column>
                </xtSelect>
            </el-form-item>
                <el-form-item label="试用日期" required>
                    <el-date-picker
                        v-model="formData.start_date"
                        type="datetime"
                        placeholder="试用日期"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        :readonly="localMode === 'show'"
                        style="width: 200px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="转正日期" required>
                    <el-date-picker
                        v-model="formData.end_date"
                        type="datetime"
                        placeholder="转正日期"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        :readonly="localMode === 'show'"
                        style="width: 200px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="应聘人员登记表" rquired style="white-space: nowrap;">
                    <sc-upload-file
                        v-model="formData.record_file"
                        :multiple="false"
                        :limit="1"
                        :accept="['.xlsx', '.xls','.pdf','.docx', '.doc', '.jpg', '.png', '.jpeg']"
                        @success = "fileUPSuccess"
                        :disabled="localMode ==='show'"
                    >
                        <el-button type="primary" icon="el-icon-upload"> </el-button>
                    </sc-upload-file>
                </el-form-item>
                <el-form-item label="员工转正申请表" rquired style="white-space: nowrap;">
                    <sc-upload-file
                        v-model="formData.apply_file"
                        :multiple="false"
                        :limit="1"
                        :accept="['.xlsx', '.xls','.pdf','.docx', '.doc', '.jpg', '.png', '.jpeg']"
                        @success = "fileUPSuccess"
                        :disabled="localMode ==='show'"
                    >
                        <el-button type="primary" icon="el-icon-upload"> </el-button>
                    </sc-upload-file>
                </el-form-item>
                <el-form-item label="PPT 汇报考核" rquired style="white-space: nowrap;">
                    <sc-upload-file
                        v-model="formData.ppt_file"
                        :multiple="false"
                        :limit="1"
                        :accept="['.xlsx', '.xls','.pdf','.docx', '.doc', '.jpg', '.png', '.jpeg']"
                        @success = "fileUPSuccess"
                        :disabled="localMode ==='show'"
                    >
                        <el-button type="primary" icon="el-icon-upload"> </el-button>
                    </sc-upload-file>
                </el-form-item>
                <el-form-item label="转正申请" required>
                    <el-input v-model="formData.reason" type="textarea" rows="2" style="width: 400px" placeholder="请输入请假理由"></el-input>
                 </el-form-item>
            </el-form>
            <el-footer>
                <el-button type="danger"
                v-if="localMode=='edit'"
                style="margin-right: 4px;"
                @click="handleDel"
                :loading="saveLoading"
                >删除</el-button>
                <ticketd_b 
                        v-if = "formData.ticket_ && localMode=='show'"
                        :t_id="formData.id" 
                        :ticket_="formData.ticket_"
                        :ticket_data="ticket_data"
                        @success="$emit('success', localMode)" 
                        ref="ticketd_b"
                    ></ticketd_b>
                     <el-button 
                        v-if="localMode!='show'"
                        type="primary"
                        style="margin-right: 4px;"
                        @click="handleSave"
                        :loading="saveLoading"
                        >提交审批</el-button>
            </el-footer>
        </el-main>
        <el-aside width="20%" v-if="formData.ticket_">
            <ticketd :ticket_="formData.ticket_" @success="$emit('success')"></ticketd>
        </el-aside>
    </el-container>
</template>

<script>
import ticketd_b from "@/views/wf/ticketd_b.vue";
import ticketd from '@/views/wf/ticketd.vue';
import { genTree } from "@/utils/verificate";

export default {
    name: "ProbationForm",
    components: {
        ticketd_b,
        ticketd
    },
    props: {
        mode: {
            type: String,
            default: 'show'
        },
        t_id: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
            formData: {},
            ticket_data: {},
            localMode: this.mode,
            group:[],
            apiObjPost: this.$API.system.post.list,
            saveLoading: false,
            rules: {
                empperson: [
                    { required: true, message: "请选择员工", trigger: "change" },
                ],
                reg_dept: [
                    { required: true, message: "请选择部门", trigger: "change" },
                ],
                reg_post: [
                    { required: true, message: "请选择岗位", trigger: "change" },
                ],
                start_date: [
                    { required: true, message: "请选择开始日期", trigger: "change" },
                ],
                end_date: [
                    { required: true, message: "请选择结束日期", trigger: "change" },
                ],
            },
            groupsProps: {
                multiple: false,
                emitPath: false,
                checkStrictly: true,
                },
        };
    },
    watch: {
        formData: {
            handle(val) {
                Object.assign(this.ticket_data, val);
            },
            deep:true,
            immediate: true
        }
    },
    mounted() {
        this.getGroup();
        if (this.t_id) {
            this.getTid();
        }else {
            this.localMode = "add";
        }
    },
    methods: {
        async getTid() {
            try {
                let res = await this.$API.hrm.leave.item.req(this.t_id);
                this.formData = res;
                if (res.ticket_ && res.ticket_.state_.type == 1 && res.create_by == this.$TOOL.data.get("USER_INFO").id) {
                    this.localMode = "edit";
                }
            } catch (error) {
                console.error('获取请假数据失败:', error);
            }
        },
        async getGroup() {
            let res = await this.$API.system.dept.list.req({ page: 0 });
            this.group = genTree(res);
        },
        handleDel() {
            this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			})
				.then(()=>{
                    this.$API.hrm.leave.delete.req(this.formData.id).then(res=>{
                        this.$message.success("删除成功");
                        this.$emit('success');
                    })
                })
        },
        async handleSave() {
            if (this.localMode == "add") {
                try {
                    let res = await this.$API.hrm.leave.create.req(this.formData);
                    this.$message.success("提交成功");
                    this.$emit('success', this.localMode);
                } catch (error) {
                    console.error('请假需求申请失败:', error);
                    throw error;
                }
            } else if (this.localMode == "edit") {
                this.$message.error("不支持编辑");
            }
        },
        },  
    };
</script>