<template>
    <div>
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
                    <el-form-item label="名称" prop="name">
                        <el-input style="width: 300px" v-model="form.name"/>
                    </el-form-item>
                    <el-form-item label="标识" prop="tag">
                        <el-input style="width: 300px" v-model="form.tag"/>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer>
                <div class="bodyBox">
                    <div style="display: flex;">
                        <el-button type="primary" @click="onSubmit('ruleForm')">
                            {{btn}}
                        </el-button>
                        <el-button @click="close">
                            取消
                        </el-button>
                    </div>
                </div>
            </el-footer>
        </el-container>
    </div>
</template>

<script>
    import {post, get} from '@/api/common'
    export default {
        name: "itemEdit",
        data() {
            return {
                form: {
                    name: '',
                    tag: '',
                    status: 0
                },
                rules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'},
                    ],
                    tag: [
                        {required: true, message: '请输入标签', trigger: 'blur'},
                    ]
                },
                btn: '新建',
            }
        },
        created() {
            document.title = '管理项登记'
            if (this.$route.query.id) {
                get('/strategyglobalinfo/page', {'id': this.$route.query.id}).then(res => {
                    res = res.data.list[0];
                    this.form = res;
                    console.log(this.form)

                });
                this.btn = '修改'
            }
        },
        methods: {
            // 提交按钮
            onSubmit: function (formName) {
                this.$confirm('您确定编辑该项？当前操作会影响所有相关测试', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            if (this.$route.query.id) {
                                post('/strategyglobalinfo/update/'+this.$route.query.id, this.form).then(res => {
                                    this.$message({
                                        message: res.message,
                                        type: 'success',
                                        duration: 3000,
                                        offset: 360
                                    })
                                    setTimeout(() => {window.close()}, 1200)
                                }).catch(error => {
                                    console.log(error)
                                })
                            } else {
                                let updata = {
                                    name : this.form.name,
                                    tag : this.form.tag,
                                    status : 0,
                                };
                                post('/strategyglobalinfo/create', updata).then(res => {
                                    console.log(res)
                                    this.$message({
                                        message: res.message,
                                        type: 'success',
                                        duration: 3000,
                                        offset: 360
                                    })
                                    setTimeout(() => {window.close()}, 1200)
                                }).catch(error => {
                                    console.log(error)
                                })
                            }
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },

            // 取消按钮
            close() {
                this.$confirm('此操作不会保存已编辑数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    window.location.href = 'about:blank'
                    window.close()
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            }
        }
    }
</script>

<style lang="scss">
    .avatar {
        width: 100%;
        max-width: 500px;
    }
</style>
