<template>
<el-container>
	<el-header>
		<div class="left-panel">
			<el-button type="primary" icon="el-icon-plus" @click="add">添加成员</el-button>
		</div>
		<div class="right-panel">
			<div class="right-panel-search">
				<el-input v-model="search.nickname" placeholder="成员昵称" clearable style="width: 150px;"></el-input>
				<el-select v-model="search.role" placeholder="成员角色" clearable style="width: 120px;">
					<el-option label="家庭主" value="owner"></el-option>
					<el-option label="管理员" value="admin"></el-option>
					<el-option label="成员" value="member"></el-option>
				</el-select>
				<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
			</div>
		</div>
	</el-header>
	<el-main class="nopadding">
		<scTable ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" @selection-change="selectionChange" :params="search">
			<el-table-column type="selection" width="55" />
			<el-table-column prop="id" label="ID" width="80" />
			<el-table-column prop="user.nickname" label="成员昵称" min-width="120" />
			<el-table-column prop="user.phone" label="手机号" width="120" />
			<el-table-column prop="family.name" label="所属家庭" width="150" />
			<el-table-column label="角色" width="100">
				<template #default="scope">
					<el-tag :type="getRoleColor(scope.row.role)">{{ getRoleName(scope.row.role) }}</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="joined_at" label="加入时间" width="180" />
			<el-table-column label="操作" width="180" fixed="right">
				<template #default="scope">
					<el-button-group>
						<el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
						<el-button type="primary" size="small" @click="show(scope.row)">查看</el-button>
						<el-popconfirm title="确定移除该成员吗？" @confirm="handleDelete(scope.row)">
							<template #reference>
								<el-button type="danger" size="small">移除</el-button>
							</template>
						</el-popconfirm>
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
</el-container>

<save ref="saveBox" v-if="dialog.save" @success="upsearch" @closed="dialog.save=false" />
</template>

<script>
import save from './save.vue'

export default {
	name: 'account.members',
	components: { save },
	data(){
		return {
			dialog: {
				save: false
			},
			list: {
				apiObj: this.$API.account.members.list,
				column: []
			},
			search: {
				nickname: '',
				role: ''
			},
			selection: []
		}
	},
	methods:{
		getRoleName(role){
			const map = {
				'owner': '家庭主',
				'admin': '管理员',
				'member': '成员'
			}
			return map[role] || role
		},
		getRoleColor(role){
			const map = {
				'owner': 'danger',
				'admin': 'warning',
				'member': 'primary'
			}
			return map[role] || 'info'
		},
		upsearch(){
			this.$refs.table.reload(this.search);
		},
		selectionChange(selection){
			this.selection = selection;
		},
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveBox.open('add').setData({})
			})
		},
		edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveBox.open('edit').setData(row)
			})
		},
		show(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveBox.open('show').setData(row)
			})
		},
		async handleDelete(row){
			const res = await this.$API.account.members.delete.post({id: row.id})
			if(res.code === 1){
				this.$message.success('移除成功')
				this.upsearch()
			}else{
				this.$message.error(res.message || '移除失败')
			}
		}
	}
}
</script>

<style scoped>
</style>
