<template>
    <div class="exportAlgorithmsPanel">
        <el-row>
            <el-col :span="16" :offset="4">
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="160px"
                >
                    <el-form-item label="算法名称:" prop="algorithmName">
                        <el-input
                            placeholder="请输入"
                            maxlength="60"
                            v-autoTrim="{ obj: form, key: 'algorithmName' }"
                            v-model="form.algorithmName"
                        >
                        </el-input>
                    </el-form-item>
                    <el-form-item label="算法描述:" prop="description">
                        <el-input
                            placeholder="请输入"
                            maxlength="200"
                            v-autoTrim="{ obj: form, key: 'description' }"
                            v-model="form.description"
                        >
                        </el-input>
                    </el-form-item>

                    <el-form-item label="导入算法:" prop="minioPath">
                        <el-input
                            placeholder="请输入"
                            maxlength="200"
                            disabled
                            v-autoTrim="{ obj: form, key: 'minioPath' }"
                            v-model="form.minioPath"
                        >
                        </el-input>
                        <upload
                            class="upload"
                            ref="upload"
                            :limit="1"
                            type="algorithmFile"
                            @attachmentChange="attachmentChange"
                        ></upload>
                    </el-form-item>
                </el-form>

                <div class="btns">
                    <el-button
                        v-if="form.share === '0' || form.share === ''"
                        type="primary"
                        @click="save(false)"
                        >保存</el-button
                    >
                    <el-button
                        v-if="form.share === '0' || form.share === '1'"
                        type="primary"
                        @click="save(true)"
                        >另存为</el-button
                    >
                    <el-button type="primary" plain @click="cancel"
                        >取消</el-button
                    >
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import upload from "@/components/upload/upload";

export default {
    name: "exportAlgorithms", // 导入算法详情
    components: { upload },
    data() {
        return {
            form: {
                id: "",
                algorithmName: "", // 算法名称
                description: "", // 算法描述
                uploadMode: "1", // 方式
                minioPath: "", // 上传文件地址
                share: "",
            },
            rules: {
                algorithmName: [
                    { required: true, message: "请输入", trigger: "blur" },
                ],
                description: [
                    { required: true, message: "请输入", trigger: "blur" },
                ],
                minioPath: [
                    { required: true, message: "请上传", trigger: "change" },
                ],
            },
            // attachmentList: [],
        };
    },

    methods: {
        attachmentChange(obj) {
            // this.attachmentList = obj;
            console.log(obj);
            if (obj.length > 0) {
                this.form.minioPath = obj[0].fileName;
                this.$refs.form.clearValidate("minioPath");
            } else {
                this.form.minioPath = "";
            }
        },
        save(isAdd) {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    if (isAdd) {
                        this.form.id = "";
                    }

                    this.$axios({
                        method: "post",
                        url: this.$api.algorithmsLibrary.addOrUpdate,
                        data: {
                            ...this.form,
                        },
                    }).then((res) => {
                        if (res.code == 200) {
                            this.$message.success("保存成功");
                            this.cancel();
                        } else {
                            this.$message.error(res.message || "保存失败");
                        }
                    });
                }
            });
        },
        cancel() {
            this.$router.replace({ path: "/algorithmsLibraryList" });
        },
    },

    mounted() {
        let id = this.$route.query.id;
        if (id) {
            this.form.id = id;
            this.form.share = this.$route.query.share;

            this.$axios({
                method: "post",
                url: this.$api.algorithmsLibrary.selectDetailsById,
                data: {
                    id,
                },
            }).then((res) => {
                if (res.code == 200 && res.info) {
                    this.form = res.info;
                } else {
                    this.$message.error(res.message || "获取信息失败");
                }
            });
        }
    },
};
</script>

<style lang='less' scoped>
.exportAlgorithmsPanel {
    padding: 45px 30px 0;

    .el-form {
        /deep/ .el-input,
        .el-select {
            width: 100%;
        }
    }

    .testLinkPanel {
        margin-left: 160px;
    }

    .btns {
        padding-top: 90px;
        text-align: center;
    }

    .upload {
        margin-top: 15px;
    }
}
</style>