<template>
	<el-main>
		<el-card shadow="never">
			<x-select-filter :data="data" :selected-values="selectedValues" :label-width="80" @on-change="change"></x-select-filter>
		</el-card>
		<el-card class="userlose" shadow="never" style="margin-top: 10px;">
			<el-container>
				<el-header v-auths="['user.group']">	
					<div class="left-panel">
						<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					</div>
				</el-header>
				<el-main class="nopadding">
				<x-table :api="api" ref="table" :params="filterData" :hidePagination="true">
					<el-table-column prop="id" fixed sortable label="GroupID" align="center" width="100" />

					<el-table-column prop="title" label="名称" width="180" />
					<el-table-column prop="alias" label="别名" width="180" />

					<el-table-column prop="status" label="状态" width="90">
						<template #default="scope">
							<x-status-indicator pulse type="success" v-if="scope.row.status==1" label="开启" />
							<x-status-indicator pulse type="info" v-if="scope.row.status==0" label="关闭" />
						</template>
					</el-table-column>
					<el-table-column prop="anicount" label="积分" width="100" v-if="filterData.type=='member'" />

					<el-table-column prop="anicount" label="主图" width="100" v-if="filterData.type=='member'">
						<template #default="scope">
							<el-image :preview-src-list="[scope.row.image.url]" :preview-teleported="true" hide-on-click-modal="true" lazy="true" style="width: 30px; height: 30px" fit="cover" :src="scope.row.image.url">
								<template #error>
						          	<div class="image-slot">
						          		无图
						          	</div>
						        </template>
							</el-image>
						</template>
					</el-table-column>


					<el-table-column prop="aid" label="控制台模块" width="100" v-if="filterData.type=='system'">
						<template #default="scope">
							<el-badge :value="scope.row.authority.grid ? scope.row.authority.grid.length:0"  type="warning"></el-badge>
						</template>
					</el-table-column>
					<el-table-column prop="aid" label="菜单" width="80" v-if="filterData.type=='system'">
						<template #default="scope">
							<el-badge :value="scope.row.authority.menu ? scope.row.authority.menu.length:0"  type="primary"></el-badge>
						</template>
					</el-table-column>

					<el-table-column prop="message" label="备注">
						<template #default="scope">
							<input class="el-input__inner" type="text" readonly :value="scope.row.message">
						</template>
					</el-table-column>

					<el-table-column label="操作" fixed="right" align="right" width="160">
					   <template #default="scope">
	   						<el-button-group>
	   							<el-button text type="primary" size="small" @click="group_show(scope.row, scope.$index)">查看</el-button>
	   							<el-button text type="primary" size="small" @click="group_edit(scope.row, scope.$index)" v-auths="['user.group']">编辑</el-button>
	   							<el-popconfirm title="确定删除吗？" @confirm="group_del(scope.row, scope.$index)" >
	   								<template #reference>
	   									<el-button text type="primary" size="small" v-auths="['user.group']">删除</el-button>
	   								</template>
	   							</el-popconfirm>
	   						</el-button-group>
	   					</template>
					</el-table-column>
				</x-table>
			</el-main>
				<!-- <pre>{{ filterData }}</pre> -->
			</el-container>
		</el-card>
	</el-main>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>


<style>
	.userlose .el-card__body{
		padding: 0px;
	}
</style>

<script>
	import xSelectFilter from '@/components/xSelectFilter'
	import saveDialog from './role/save'

	export default {
		name: 'selectFilter',
		components: {
			xSelectFilter,
			saveDialog,
		},
		data() {
			return {
				data: [
					{
						title: "用户组",
						key: "type",
						options: [
							{
								label: "系统组",
								value: "system",
								icon: "el-icon-flag"
							},
							{
								label: "用户组",
								value: "member",
								icon: "el-icon-bottom-left"
							},
							{
								label: "自定义会员组",
								value: "default",
								icon: "el-icon-circle-close"
							}
						]
					}
				],
				selectedValues: {
					type: ["system"]
				},
				filterData: {
					type:'system'
				},
				search: {},
				dialog: {
					save: false,
					permission: false
				},
				api:'system/group/lists'
			}
		},
		computed: {
		    filteredItems() {
		      	return this.items.filter(item => item.category === 'Category A');
		    }
		},
		mounted() {

		},
		methods: {
			//本地更新数据
			handleSaveSuccess(){
				this.$refs.table.refresh()
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open().setData({type:this.filterData.type})
				})
			},
			//编辑
			group_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			group_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//权限设置
			permission(){
				this.dialog.permission = true
				this.$nextTick(() => {
					this.$refs.permissionDialog.open()
				})
			},
			//删除
			async group_del(row){
				var reqData = {id: row.id}
				var res = await this.$http.post('system/group/delete',reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功");
					return false;
				}
				this.$alert(res.message, "提示", {type: 'error'})
			},
			change(selected){
				this.filterData = selected
				this.$refs.table.reload(this.filterData)
			},
			//表格内开关
			changeSwitch(val, row){
				row.status = row.status == '1'?'0':'1'
				row.$switch_status = true;
				setTimeout(()=>{
					delete row.$switch_status;
					row.status = val;
					this.$message.success("操作成功")
				}, 500)
			},
		}
	}
</script>

<style>
</style>
