<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <span class="text-lg">{{pageName}}</span>
                <el-button type="primary" @click="addEvent">
                    {{ t('addResourceSharing') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="resourceSharingTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('resourceId')" prop="resource_id">
                        <el-input v-model="resourceSharingTable.searchParam.resource_id" :placeholder="t('resourceIdPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('userId')" prop="user_id">
                        <el-input v-model="resourceSharingTable.searchParam.user_id" :placeholder="t('userIdPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('roleId')" prop="role_id">
                        <el-input v-model="resourceSharingTable.searchParam.role_id" :placeholder="t('roleIdPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('sharedBy')" prop="shared_by">
                        <el-input v-model="resourceSharingTable.searchParam.shared_by" :placeholder="t('sharedByPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('sharedAt')" prop="shared_at">
                        <el-input v-model="resourceSharingTable.searchParam.shared_at" :placeholder="t('sharedAtPlaceholder')" />
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="loadResourceSharingList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card>

            <div class="mt-[10px]">
                <el-table :data="resourceSharingTable.data" size="large" v-loading="resourceSharingTable.loading">
                    <template #empty>
                        <span>{{ !resourceSharingTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="resource_id" :label="t('resourceId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="user_id" :label="t('userId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="role_id" :label="t('roleId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="shared_by" :label="t('sharedBy')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="shared_at" :label="t('sharedAt')" min-width="120" :show-overflow-tooltip="true"/>
                    

                    <el-table-column :label="t('operation')" fixed="right" min-width="120">
                       <template #default="{ row }">
                           <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                           <el-button type="primary" link @click="deleteEvent(row.id)">{{ t('delete') }}</el-button>
                       </template>
                    </el-table-column>

                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="resourceSharingTable.page" v-model:page-size="resourceSharingTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="resourceSharingTable.total"
                        @size-change="loadResourceSharingList()" @current-change="loadResourceSharingList" />
                </div>
            </div>

            <edit ref="editResourceSharingDialog" @complete="loadResourceSharingList" />
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getResourceSharingList, deleteResourceSharing } from '@/app/api/resource_sharing'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import Edit from '@/app/views/resource_sharing/components/resource-sharing-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let resourceSharingTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "resource_id":"",
      "user_id":"",
      "role_id":"",
      "shared_by":"",
      "shared_at":""
    }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    

/**
 * 获取资源共享列表
 */
const loadResourceSharingList = (page: number = 1) => {
    resourceSharingTable.loading = true
    resourceSharingTable.page = page

    getResourceSharingList({
        page: resourceSharingTable.page,
        limit: resourceSharingTable.limit,
         ...resourceSharingTable.searchParam
    }).then(res => {
        resourceSharingTable.loading = false
        resourceSharingTable.data = res.data.data
        resourceSharingTable.total = res.data.total
    }).catch(() => {
        resourceSharingTable.loading = false
    })
}
loadResourceSharingList()

const editResourceSharingDialog: Record<string, any> | null = ref(null)

/**
 * 添加资源共享
 */
const addEvent = () => {
    editResourceSharingDialog.value.setFormData()
    editResourceSharingDialog.value.showDialog = true
}

/**
 * 编辑资源共享
 * @param data
 */
const editEvent = (data: any) => {
    editResourceSharingDialog.value.setFormData(data)
    editResourceSharingDialog.value.showDialog = true
}

/**
 * 删除资源共享
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('resourceSharingDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteResourceSharing(id).then(() => {
            loadResourceSharingList()
        }).catch(() => {
        })
    })
}

    

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadResourceSharingList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>
