<template>
    <el-dialog v-model="dialogFormVisible" 
    :show-close="false" 
    :title="dialogTitle" width="340" 
    :close-on-press-escape="false" :close-on-click-modal="false" >
        <div v-loading="loadingGroup">
            <el-select v-model="params.GId" placeholder="Select" style="width: 100%;"
            @change="getGName">
                <el-option
                v-for="item in GroupList"
                :key="item.Id"
                :label="item.GName"
                :value="item.Id"
              >
              </el-option>
            </el-select>
            
        </div>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="submit" :loading="setLoading">
              确认{{dialogType==1?'复制':'移动'}}
            </el-button>
          </div>
        </template>
      </el-dialog>
</template>
<script lang="ts" setup>
    import { reactive, ref, onMounted, watch} from "vue";
    import { ElLoading, ElMessage, ElMessageBox } from "element-plus";
    import CloudDiskService from '@/services/CloudDiskService'
    import { ApiResult } from "@/configs/axios";
    const dialogFormVisible = ref(true)
    const dialogTitle = ref('')
    const setLoading = ref(false)
    const loadingGroup = ref(false)
    // const GroupList = ref([] as any)
    const currentGroup = ref('' as any)
    const props = defineProps({
        dialogType: {
            type: Number,
            required: true,
        },
        DetailsId: {
            type: String,
            required: '',
        },
        GroupId: {
            type: String,
            required: '',
        },
        GroupList:{
            type: Array,
            required: [],
        }
    })
    const params = reactive({
        Id: '' as String,
        GId: '' as String,
    })
    params.GId = props.GroupId
    const emit = defineEmits<{
        (event: 'close'): void,
        (event: 'success'): void,
    }>()
    dialogTitle.value = props.dialogType==1?'复制文件':'移动文件'
    
    const getGName = () =>{
        let GroupArr = GroupList.value.filter(x=>x.Id==params.GId)
        currentGroup.value = GroupArr[0].GName
    }
    const close = () =>{
        dialogFormVisible.value = false
        emit('close')
    }
    const submit = () => {
        params.Id = props.DetailsId
        let text = ''
        if(props.dialogType==1)text = `复制到${currentGroup.value}分组下`
        else text = `移动至${currentGroup.value}分组下`
        ElMessageBox.confirm(
            `${text}`,
            '提示',
            {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            }
        ).then(() => {
            setCopy()
        }).catch(() => {})
    }
    const setCopy = async () =>{
        setLoading.value = true
        let pageRes;
        if(props.dialogType==1) pageRes = await CloudDiskService.CloudFileCopy(params);
        else pageRes = await CloudDiskService.CloudFileMove(params);
        if (pageRes.data.resultCode==ApiResult.SUCCESS) {
            dialogFormVisible.value = false
            emit('success')
        }
        setLoading.value = false
    }
    const querySearchGroup = async () =>{
        if(loadingGroup.value) return
        loadingGroup.value = true
        try {
            // let datasRes = await CloudDiskService.GetPPTCloudLists('');
            // if (datasRes.data.resultCode==ApiResult.SUCCESS) {
            //     GroupList.value = datasRes.data.data.list
            //     GroupList.value.unshift({
            //         Id: '',
            //         GName: '全部',
            //         RecordNum: datasRes.data.data.TotalNum
            //     })
            //     let GroupArr = GroupList.value.filter(x=>x.Id==params.GId)
            //     if(GroupArr.length==0) params.GId = ''
            // }
            loadingGroup.value = false
        } catch (error) {
            loadingGroup.value = false
        }
        
    }
    querySearchGroup()
    let GroupArr = props.GroupList.filter(x=>x.Id==params.GId)
    if(GroupArr.length==0) params.GId = ''
</script>