<template>
    <el-container>
        <el-main class="nopadding">
            <el-form label-width="110px" :model="formData" style="padding: 20px;">
                <el-form-item label="创建时间" v-if="localMode !== 'add'">
                    {{ formData.create_time }}
                </el-form-item>
                <el-form-item label="保管部门" required>
                   <xtSelect
                        v-model="formData.keep_dept"
                        style="width: 100%;"
                        :apiObj = "$API.system.dept.list"
                        v-model:label="formData.keep_dept_name"
                        :edit="localMode === 'add'"
                   >
                    <el-table-column prop="name" label="部门名称"></el-table-column>
                   </xtSelect>
                </el-form-item>
                <el-form-item label="启用日期" required>
                    <el-date-picker
                        v-model="formData.start_date"
                        type="date"
                        placeholder="请选择启用日期"
                        value-format="YYYY-MM-DD"
                        :disabled="localMode !== 'add'"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="入库明细" required>
                    <sc-form-table
                    v-model="formData.items"
                    :addTemplate="addTemplate"
                    placeholder="暂无数据"
                    >
                    <el-table-column prop="name" label="资产类别" width="180">
                        <template #default="scope">
                            <xtSelect :apiObj="$API.asm.assetcate.list" v-model="scope.row.cate">
                                <el-table-column prop="name"></el-table-column>
                            </xtSelect>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="资产名称" width="180">
                        <template #default="scope">
                            <el-input v-model="scope.row.name"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="specification" label="规格型号" width="180">
                        <template #default="scope">
                            <el-input v-model="scope.row.specification"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantity" label="数量" width="180">
                        <template #default="scope">
                            <el-input-number v-model="scope.row.quantity" :min="1" :precision="0" controls-position="right"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="canuse_year" label="可用年限" width="180">
                        <template #default="scope">
                            <el-input-number v-model="scope.row.canuse_year" :min="1" :precision="0" controls-position="right"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="original_value" label="资产原值" width="180">
                        <template #default="scope">
                            <el-input-number v-model="scope.row.original_value" :min="1" :precision="2" controls-position="right"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="supplier" label="供应商" width="180">
                        <template #default="scope">
                            <el-input v-model="scope.row.supplier"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="location" label="存放地点">
                        <template #default="scope">
                            <el-input v-model="scope.row.location"></el-input>
                        </template>
                    </el-table-column>
                    </sc-form-table>
                </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_"
                    :ticket_data="ticket_data"
                    @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: 'repair_form',
    components: {
        ticketd_b,
        ticketd
    },
    props: {
        mode: {
            type: String,
            default: 'show'
        },
        t_id: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
            formData: {
                type: "入库",
                items: [],
            },
            ticket_data: {},
            localMode: this.mode,
            saveLoading: false,
            addTemplate: { 
                name: null,
            }
        }
    },
    mounted() {
        if (this.t_id) {
            this.getTid();
        } else {
            this.localMode = "add";
        }
    },
    methods: {
        async getTid() {
            try {
                let res = await this.$API.em.repair.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.em.repair.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.em.repair.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>