<template>
    <div>
        <div>
            <el-dialog v-model="addBoser" :close-on-click-modal="false"  title="新建目录" width="30%" @close="diClose">
                <el-form :model="boserForm" label-width="120px">
                    <el-form-item label="目录名称">
                        <el-input v-model="boserForm.dirName" placeholder="请输入不多于32位字符" maxlength='32' />
                    </el-form-item>
                    <el-form-item label="是否使用模板">
                       <el-switch @change="switchChange" v-model="boserForm.useTemp" active-value="Y" inactive-value="N" />
                       <el-button v-if="boserForm.useTemp === 'Y'" style="margin-left:10px;" @click="checkTemp" type="info" plain>{{preTempTitle || '点击选择模板'}}</el-button>
                    </el-form-item>
                    <!-- <el-form-item label="目录分类">
                        <el-radio-group v-model="boserForm.dirType">
                            <el-radio :label="1">公开目录</el-radio>
                            <el-radio :label="2">组织目录</el-radio>
                        </el-radio-group>
                    </el-form-item> -->
                    <el-form-item label="是否加密" v-if="!(FolderRole && !free )">
                            <el-switch v-model="boserForm.isEncrypt" active-value="Y" inactive-value="N" />
                    </el-form-item>
                    <el-form-item label="加密等级" v-if="!(FolderRole && !free) && boserForm.isEncrypt === 'Y'">
                        <el-radio-group v-model="boserForm.encryptLevel">
                            <el-radio :label="item.levelCode" v-for="item in levelList" :key="item">{{item.levelName}}</el-radio>
                        </el-radio-group>
                    </el-form-item>
                     <el-form-item label="私有访问码" v-if="!(FolderRole && !free) && boserForm.isEncrypt === 'Y'&& needCode">
                            <el-input v-model="boserForm.accessCode" type="password" show-password />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="addBack">取消</el-button>
                        <el-button type="primary" @click="sureAddBoser">
                            确认
                        </el-button>
                    </span>
                </template>
            </el-dialog>
            <MenuTempList v-if="showTempList" :showTempList='showTempList' @getTempPreview="getTempPreview" @getAddTempId='getAddTempId' @closeTempList="closeTempList"></MenuTempList>
            <PreviewMenuList v-if="showPreTemp" :showPreTemp="showPreTemp" :preTemp="preTemp" @closePreTemp="closePreTemp"></PreviewMenuList>
        </div>
    </div>
</template>

<script>
import { ref, onMounted, toRef,toRaw,inject ,watch } from 'vue';
import documents from '../../../api/document/document';
import MenuTempList from '@/components/MenuTempList/MenuTempList.vue';
import PreviewMenuList from '@/components/PreviewMenuList/PreviewMenuList.vue';
import { getLevel } from '@/api/level/level.js';
import { ElMessage } from 'element-plus';
import { sm3 ,sm2} from "sm-crypto";
export default {
    props: {
        folderCase: {
            type: Boolean,
            required: true
        },
        oneOrTwo: {
            type: Number,
            required: true
        },
        impDirId: {
            type: Number,
            required: true
        },
        impSpaceId: {
            type: Number,
            required: true
        },
        newDirId: {
            type: Number,
            required: true
        },
        newSpaceId: {
            type: Number,
            required: true
        },
        thisFolderRole: {
            type: Object,
            required: true,
            default: ()=>{},
        },
        refreshFile: Function,
        getFirstFolder: Function,
    },
    setup(props, { emit }) {
        let { addBack, sureAddBoser, diClose } = toRef(props)
        const upFirstFolderData = inject("upFirstFolderData");
        let result = props.folderCase
        let free = props.oneOrTwo
        let spId = props.impSpaceId
        let drId = props.impDirId
        let newspid = props.newSpaceId
        let newdrid = props.newDirId
        const FolderRole = props.thisFolderRole
        console.log("FolderRole",props);
        let addBoser = ref(false)
        let boserForm = ref({
            dirName: "",
            useTemp:'N',
            dirType:1,
            isEncrypt:"N",
            encryptLevel:"L1",
            accessCode:''
        })
        const levelList = ref([])
        const needCode = ref(false) // 需要私有码
        const showTempList = ref(false)
        const tempId = ref()
        const showPreTemp = ref(false)
        const preTemp = ref()
        const preTempTitle = ref('')
        function inital() {
            console.log(free, 'free');
            addBoser.value = result
        }
        function backTo() {
            addBoser.value = false
            emit("getChildrenC", addBoser.value)
        }
        function addFloder() {
            if(boserForm.value.useTemp === "Y" && !tempId.value){
                ElMessage({
                    type: "error",
                    message: "请选择模板"
                })
                return
            }
            if (boserForm.value.dirName === '') {
                ElMessage({
                    type: "error",
                    message: "目录名称不能为空"
                })
            } else {
                if (free === 1) { //一级目录
                    if (boserForm.value.accessCode === '' && needCode.value) {
                        ElMessage({
                            type: "error",
                            message: "私有访问码不能为空"
                        })
                        return
                    }
                    if(boserForm.value.useTemp == 'N'){
                        documents.addDocument(
                        {
                            "dirName": boserForm.value.dirName,
                            "parentId": drId - 0,
                            "spaceId": spId - 0,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null,
                            "accessCode":boserForm.value.accessCode?sm3(boserForm.value.accessCode):''
                        }
                    ).then(res => {
                        if (res.code === 200) {
                            ElMessage({
                                message: '添加一级目录成功',
                                type: 'success',
                            })
                        }
                        addBoser.value = false
                        emit("getChildrenC", addBoser.value)
                        props.getFirstFolder()
                    })
                    }else{
                        documents.addDocumentByTemp(
                        {
                            "dirName": boserForm.value.dirName,
                            "parentId": drId - 0,
                            "spaceId": spId - 0,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null,
                            "accessCode":boserForm.value.accessCode?sm3(boserForm.value.accessCode):''
                        },tempId.value
                    ).then(res => {
                        if (res.code === 200) {
                            ElMessage({
                                message: '添加一级目录成功',
                                type: 'success',
                            })
                        }
                        addBoser.value = false
                        emit("getChildrenC", addBoser.value)
                        props.getFirstFolder()
                    })
                    }
                    
                    console.log('1111', 1111)
                } else {  //二级目录
                    let query = {}
                    if(FolderRole){ // 如果是父级有权限就直接用父级
                        query={
                            "dirName": boserForm.value.dirName,
                            "parentId": newdrid - 0,
                            "spaceId": newspid - 0,
                            "isEncrypt":"Y",
                            "encryptLevel":FolderRole.levelCode,
                            "accessCode":FolderRole.accessCode,
                            "remark":'extend'
                        }
                        console.log('query',query);
                        console.log('FolderRole',FolderRole);
                    }else{
                        if (boserForm.value.accessCode === '' && needCode.value) {
                            ElMessage({
                                type: "error",
                                message: "私有访问码不能为空"
                            })
                            return
                        }
                        query={
                            "dirName": boserForm.value.dirName,
                            "parentId": newdrid - 0,
                            "spaceId": newspid - 0,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null,
                            "accessCode":boserForm.value.accessCode?sm3(boserForm.value.accessCode):''
                        }
                    }
                    if(boserForm.value.useTemp == 'N'){
                        documents.addDocument(
                        query
                        ).then(res => {
                            if (res.code === 200) {
                                ElMessage({
                                    message: '添加成功',
                                    type: 'success',
                                })
                            }
                            addBoser.value = false
                            emit("getChildrenC", addBoser.value)
                            props.refreshFile()
                        })
                    }else{
                        documents.addDocumentByTemp(
                        query,tempId.value
                        ).then(res => {
                            if (res.code === 200) {
                                ElMessage({
                                    message: '添加成功',
                                    type: 'success',
                                })
                            }
                            addBoser.value = false
                            emit("getChildrenC", addBoser.value)
                            props.refreshFile()
                        })
                    }
                }
            }

        }
        const getLevelList = async ()=>{
            const res = await getLevel()
            if(res.rows){
                levelList.value = res.rows
            }
            // console.log('level',res);
        }
        const switchChange = (val)=>{
            if(val === "Y"){
                showTempList.value = true
            }else{
                preTempTitle.value = ''
                tempId.value = null
                showTempList.value = false
            }
        }
        const closeTempList = ()=>{
            showTempList.value = false
        }
        const closePreTemp = ()=>{
            showPreTemp.value = false
        }
        const getAddTempId = (id,name)=>{
            tempId.value = id
            preTempTitle.value = name
            showTempList.value = false
        }
        const getTempPreview = (item)=>{
            showPreTemp.value = true
    
            preTemp.value = JSON.parse(item.tmplContent)
        }
        const checkTemp = ()=>{
            showTempList.value = true
        }
        //监听加密等级
        watch(() => boserForm.value.encryptLevel, (newValue, oldValue) => {
            const thisRole = levelList.value.find(item=>item.levelCode == newValue)
            console.log('thisRole',thisRole);
            if(thisRole.roles.l0700){
                needCode.value = true
            }else{
                needCode.value = false
            }
        }, {
        deep: true
        });
        onMounted(() => {
            inital()
            getLevelList()
        })
        return {
            addBoser,
            boserForm,
            inital,
            addBack: backTo,
            sureAddBoser: addFloder,
            diClose: backTo,
            spId,
            drId,
            newdrid,
            newspid,
            levelList,
            getLevelList,
            FolderRole,
            free,
            needCode,
            switchChange,
            showTempList,
            closeTempList,
            getAddTempId,
            tempId,
            getTempPreview,
            showPreTemp,
            preTemp,
            closePreTemp,
            preTempTitle,
            checkTemp
        }
    },
    components:{
            MenuTempList,
            PreviewMenuList
        }
}
</script>

<style lang="scss" scoped></style>