<template>
    <el-container>
        <el-main class="nopadding">
            <el-form label-width="80px" :model="formData" style="padding: 20px;" :disabled="localMode === 'show'">
                <el-form-item label="员工信息" required>
                    {{ formData.employee_name }}（{{ formData.belong_dept_name }} - {{ formData.post_name }}）
                </el-form-item>
                <el-form-item label="需求岗位" required>
                    <el-input v-model="formData.post_need"></el-input>
                </el-form-item>
                <el-form-item label="需求人数" required>
                    <el-input-number v-model="formData.count_need" :min="1" :precision="0"></el-input-number>
                </el-form-item>
                <el-form-item label="工资报酬" required>
                    <el-input-number v-model="formData.salary" :min="2000" :precision="0"></el-input-number>
                </el-form-item>
                <el-form-item label="到岗日期" required>
                    <el-date-picker
                        v-model="formData.arrival_date"
                        type="date"
                        placeholder="预计到岗日期"
                        value-format="YYYY-MM-DD"
                        :readonly="localMode === 'show'"
                    ></el-date-picker>
                </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_"
                        :t_id="formData.id" 
                        :ticket_="formData.ticket_"
                        @success="$emit('success', localMode)" 
                        ref="ticketd_b"
                    ></ticketd_b>
                     <el-button 
                        v-else
                        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'

export default {
    name: 'EnpNeedForm',
    components: {
        ticketd_b,
        ticketd
    },
    props: {
        mode: {
            type: String,
            default: 'show'
        },
        t_id: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
            formData: {
            },
            localMode: this.mode,
            saveLoading: false
        }
    },
    mounted() {
        if (this.t_id) {
            this.getTid();
        } else {
            this.initFormData();
        }
    },
    methods: {
        async initFormData() {
            try {
                let res = await this.$API.hrm.employee.read.req();
                this.formData.employee_name = res.name;
                this.formData.belong_dept_name = res.belong_dept_name;
                this.formData.post_name = res.post_name;
                this.formData.employee = res.id;
                this.localMode = "add";
            } catch (error) {
                console.error('初始化表单数据失败:', error);
            }
        },
        async getTid() {
            try {
                let res = await this.$API.hrm.resignation.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);
            }
        },
        handleDel() {
            this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			})
				.then(()=>{
                    this.$API.hrm.resignation.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.resignation.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>