<template>
    <el-container>
        <el-main class="nopadding">
            <el-form label-width="100px" :model="formData" style="padding: 20px;">
                <el-form-item label="供应商名称" required>
                    <el-input v-model="formData.name" placeholder="请输入供应商名称" :readonly="localMode === 'show'"></el-input>
                </el-form-item>
                <el-form-item label="物料分类" required>
                    <el-input v-model="formData.material_cate" :readonly="localMode === 'show'"></el-input>
                </el-form-item>
                <el-form-item label="物料名称" required>
                    <el-input v-model="formData.material_name" :readonly="localMode === 'show'"></el-input>
                </el-form-item>
                <el-form-item label="调查表" required>
                    <sc-upload-filed
                        v-model="formData.survery_form"
                        :file_ = "formData.survery_form_"
                        :multiple="false"
                        :limit="1"
                        accept=".pdf,.jpg,.png,.jpeg"
                        :disabled="localMode ==='show'"
                        />
                </el-form-item>
                <el-form-item label="营业执照" required>
                    <sc-upload-filed
                        v-model="formData.business_license"
                        :file_ = "formData.business_license_"
                        :multiple="false"
                        :limit="1"
                        accept=".pdf,.jpg,.png,.jpeg"
                        :disabled="localMode ==='show'"
                        />
                </el-form-item>
                <el-form-item label="质量证书" required>
                    <sc-upload-filed
                        v-model="formData.quality_certificate"
                        :file_ = "formData.quality_certificate_"
                        :multiple="false"
                        :limit="1"
                        accept=".pdf,.jpg,.png,.jpeg"
                        :disabled="localMode ==='show'"
                        />
                </el-form-item>
            </el-form>
            <el-footer>
                
                <el-button type="danger"
                v-if="localMode=='edit'"
                v-auth="'supplieraudit.delete'"
                :loading="saveLoading"
                style="margin-right: 4px;"
                @click="handleDel"
                >删除</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 {
    components: {
        ticketd_b,
        ticketd
    },
    props: {
        mode: {
            type: String,
            default: 'show'
        },
        t_id: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
            formData: {
                survery_form_: null
            },
            localMode: this.mode,
            saveLoading: false,
        }
    },
    watch: {
        mode(newVal) {
            this.localMode = newVal;
        }
    },
    mounted() {
        if (this.t_id) {
            this.getTid();
        } else {
            this.localMode = "add";
        }
    },
    methods: {
        async getTid() {
            let res = await this.$API.pum.supplieraudit.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";
            }
        },
        handleDel() {
            this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			})
				.then(()=>{
                    this.$API.pum.supplieraudit.delete.req(this.formData.id).then(res=>{
                        this.$message.success("删除成功");
                        this.$emit('success');
                    })
                })
        },
        handleSave() {
            if (this.localMode == "add") {
                this.saveLoading = true;
                this.$API.pum.supplieraudit.create.req(this.formData).then(res=>{
                    this.saveLoading = true;
                    this.$message.success("提交成功");
                    this.$emit('success', this.localMode);
                }).catch(e=>{
                    this.saveLoading = false;
                })
            } else if (this.localMode == "edit") {
                this.$message.error("不支持编辑!")
            }
        },
    }
}
</script>