<template>
    <!-- 档案目录管理 -->
    <div>
        <el-card style="min-height: calc(100vh - 125px);">
        <TableModel :formLabel="formLabel" :showOperation="true" :showRightTools="true" ref="tableRef"
            :columnsList="columnsList" :request-api="getArchivalCatalogueListApi" :send-params="defaultParams">
            <template slot="btn">
                <el-button plain size="mini" type="primary" icon="el-icon-plus" v-hasPermi="['archive:catalogue:add']"
                    @click="handleAdd" :disabled="addBtnIsShow">
                    新增
                </el-button>
            </template>

            <template slot="handle" slot-scope="{ data }">
                <el-button plain size="mini" type="success" icon="el-icon-warning-outline" v-hasPermi="['archive:catalogue:query']"
                    @click="handleDetail(data)">
                    详情
                </el-button>
                <el-button plain size="mini" type="primary" icon="el-icon-edit" v-hasPermi="['archive:catalogue:edit']"
                    @click="handleUpdate(data)">
                    修改
                </el-button>
                <el-button plain size="mini" type="danger" icon="el-icon-delete" v-hasPermi="['archive:catalogue:del']"
                    @click="handleDelete(data)">
                    删除
                </el-button>
            </template>
        </TableModel>
        <!-- 新增/编辑 -->
        <AddTableData v-if="isflag" :isAdd="isAdd" :rowData="row" @handleQuery="handleQuery" :title="title"
            @closeDialog="closeDialog" @showColose="showColose" :dataForm="row" :width="600" />
        </el-card>
    </div>
</template>

<script>
import TableModel from '@/components/TableModel'
import { columnsList, formLabel } from './config'
import {
    delArchiveApi,
    getArchivalCatalogueListApi,
} from '@/api/archivesManagement/index'
import AddTableData from './addTableData'


export default {
    name: 'RightTable',
    props:{
        selectedNode:{
            type:Object,
            default:null
        }
    },
    components: {
        TableModel,
        AddTableData
    },
    data() {
        return {
            formLabel,
            columnsList,
            getArchivalCatalogueListApi,
            title: "",
            isflag: false,
            isAdd: '',
            row: {},
            loading: false,
            addBtnIsShow:true,
            defaultParams: {}
        }
    },

    created() {

    },

    methods: {
        
        closeDialog() {
            this.isflag = false;
        },
        showColose() {
            this.isflag = false;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.title = "新增";
            this.isAdd = 'add';
            this.isflag = true;
            this.row = this.selectedNode;
            this.row.detailStatus = false;
            this.row.belongName = this.selectedNode.parentName + '/' + this.selectedNode.label
            
        },

        /** 修改操作 */
        handleUpdate(row) {
            this.title = "修改";
            this.isAdd = 'edit';
            this.row = row;
            this.row.belongName = this.selectedNode.parentName + '/' + this.selectedNode.label
            this.row.detailStatus = false;
            this.isflag = true;
        },
        
        /** 详情操作 */
        handleDetail(row) {
            this.title = "详情";
            this.isAdd = 'detail';
            this.row = row;
            this.row.belongName = this.selectedNode.parentName + '/' + this.selectedNode.label
            this.row.detailStatus = true;
            this.isflag = true;
        },
        /* 搜索操作 */
        handleQuery() {
            this.$refs.tableRef.getTableList()
        },
        /** 删除操作 */
        handleDelete(row) {
            this.$modal.confirm(`是否确认删除文件名称为"${row.contentName}"的数据项？`).then(() => {
                // 显示加载遮罩
                this.$modal.loading("正在删除，请稍候...");
                delArchiveApi({ id: row.id }).then(res => {
                    this.$modal.closeLoading();
                    if (res.code === 200) {
                        this.$modal.msgSuccess("删除成功");
                        this.handleQuery();
                    } else {
                        this.$modal.msgError(res.msg);
                    }
                }).catch(error => {
                    this.$modal.closeLoading();
                    this.$modal.msgError(error);
                });
            }).catch(() => {
                // 用户取消删除，不需要处理
            });
        },
    },
    // 监听选中的节点
    watch: {
        selectedNode: {
            handler(newVal) {
                // console.log(newVal);
                this.addBtnIsShow = !(newVal && Number(newVal.level) === 3)
                // 更新并下发默认请求参数（例如 parentId）
                const parentId = newVal && newVal.id ? newVal.id : 0
                this.defaultParams = { parentId }
                if (this.$refs.tableRef) {
                    this.$refs.tableRef.queryTableList({ parentId })
                }
            },
            immediate: true, // 表示立即执行
        },
    },
}
</script>
