<template>
    <!-- 档案目录管理 -->
    <div>
        <el-card style="min-height: calc(100vh - 190px);">
        <TableModel :formLabel="formLabel" :showOperation="true" :showRightTools="true" ref="tableRef"
            :columnsList="columnsList" :request-api="getFileManageApi" :send-params="defaultParams">
            <template slot="dataSource" slot-scope="{ data }">
                <span>{{ data.dataSource === '1' ? '本系统上传' : '智慧现场' }}</span>
            </template>
            <template slot="btn">
                <el-button plain size="mini" type="primary" icon="el-icon-plus" v-hasPermi="['file:manage: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="['file:manage:query']"
                    @click="handleDetail(data)">
                    详情
                </el-button>
                <el-button plain size="mini" type="primary" icon="el-icon-edit" v-hasPermi="['file:manage:update']"
                    @click="handleUpdate(data)">
                    修改
                </el-button>
                <el-button plain size="mini" type="danger" icon="el-icon-delete" v-hasPermi="['file:manage: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" :projectId="projectId" />
        </el-card>
    </div>
</template>

<script>
import TableModel from '@/components/TableModel'
import { columnsList, formLabel } from './config'
import {
    delFileManageApi,
    getFileManageApi,
} from '@/api/archivesManagement/fileManager/fileManager.js'
import AddTableData from './addTableData'


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

    created() {

    },

    methods: {
        
        closeDialog() {
            this.isflag = false;
        },
        showColose() {
            this.isflag = false;
        },
        /** 新增按钮操作 */
        handleAdd() {
            console.log(this.selectedNode);
            
            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.label + '/' + this.selectedNode.parentName
            this.row.detailStatus = true;
            this.isflag = true;
        },
        /* 搜索操作 */
        handleQuery() {
            this.$refs.tableRef.getTableList()
        },
        /** 删除操作 */
        handleDelete(row) {
            this.$modal.confirm(`是否确认删除文件名称为"${row.contentName}"的数据项？`).then(() => {
                // 显示加载遮罩
                this.$modal.loading("正在删除，请稍候...");
                delFileManageApi({ 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("删除失败，请重试");
                    console.error('删除失败:', error);
                });
            }).catch(() => {
                // 用户取消删除，不需要处理
            });
        },
    },
    // 监听选中的节点
    watch: {
        selectedNode: {
            handler(newVal) {
                console.log(newVal);
                this.addBtnIsShow = !(newVal && Number(newVal.level) === 4)
                // 更新并下发默认请求参数（例如 parentId）
                const parentId = newVal && newVal.id ? newVal.id : 0
                this.defaultParams = { parentId }
                if (this.$refs.tableRef) {
                    this.$refs.tableRef.queryTableList({ parentId })
                }
            },
            immediate: true, // 表示立即执行
        },
    },
}
</script>
