<template>
    <div>
        <list ref="list">
            <Header slot="add" v-if="tool.checkAuth($route.name, 'insert')">
                <el-button icon="el-icon-plus" type="primary" size="small" @click="openDialog('新增分类')">新建分类</el-button>
            </Header>

            <template v-slot:release="scope">
                <release :disabled="tool.checkAuth($route.name, 'release')" @onSuccess="onSuccess"
                    :value="scope.data.column.data.isenable == '1' ? true : false" :data="scope.data.column.data" />
            </template>

            <template v-slot:edit="scope" v-if="tool.checkAuth($route.name, 'update')">
                <el-button type="text" size="mini" @click="openDialog('编辑分类', scope.data.data)">编辑</el-button>
            </template>

            <template v-slot:insert="scope" v-if="tool.checkAuth($route.name, 'insert')">
                <el-button type="text" size="mini" @click="openDialog('新增分类', scope.data.data)">新增子类</el-button>
            </template>

            <template v-slot:del="scope" v-if="tool.checkAuth($route.name, 'delete')">
                <on-del :data="scope.data.data" @onSuccess="onSuccess" />
            </template>
        </list>

        <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">

            <el-form :model="form">
                <el-form-item label="分类名称：" :label-width="'120px'">
                    <el-input v-model="form.classname" size="small" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="warning" @click="handleRequest">确 定</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>

import Header from '@/SManagement/archives_upload/components/Header';//业务端上传

import list from './modules/list';
import onDel from './modules/delete.vue';
import release from './modules/release.vue';


export default {
    components: { list, Header, onDel, release },
    data() {
        return {
            dialogFormVisible: false,
            dialogTitle: "",
            form: {
                classname: "",
                parentid: 0,
                "sat_sharematerial_classid": 0,
                issystem: true,
                isenable: true
            }

        }
    },
    methods: {
        onSuccess() {
            this.$refs.list.selectList();
        },
        openDialog(title, data) {
            this.dialogTitle = title;
            this.dialogFormVisible = true;
            if (title == '新增分类') {
                this.form.parentid = (data) ? data.sat_sharematerial_classid : 0;
                this.form.sat_sharematerial_classid = 0;
                this.form.classname = '';

            } else {
                if (data) {
                    this.form = { ...data, issystem: true };
                };
            }
        },
        handleRequest() {
            if (this.form.classname == '') return this.$message.error('分类名称不可为空!');

            this.$api.requested({
                "classname": "webmanage.saletool.sharematerial.sharematerialClass",
                "method": "insertOrUpdate",
                "content": this.form
            }).then(res => {
                this.tool.showMessage(res);
                this.dialogFormVisible = false;
                res.code === 1 ? this.onSuccess() : '';
            })
        },
    }
}
</script>


<style scoped>
/deep/.header .left .el-button .el-icon-plus {
    margin-left: -6px;
}
</style>