<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.name" placeholder="家庭名称" clearable></el-input>
				<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="name" label="家庭名称" min-width="150" />
			<el-table-column prop="description" label="描述" min-width="200" />
			<el-table-column label="头像" width="80">
				<template #default="scope">
					<el-avatar v-if="scope.row.avatar" :src="scope.row.avatar" :size="40"></el-avatar>
					<el-avatar v-else :size="40">家</el-avatar>
				</template>
			</el-table-column>
			<el-table-column label="成员数量" width="100">
				<template #default="scope">
					<el-tag type="primary">{{ scope.row.members_count || 0 }}</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="账户数量" width="100">
				<template #default="scope">
					<el-tag type="success">{{ scope.row.accounts_count || 0 }}</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="created_at" label="创建时间" width="180" />
			<el-table-column prop="updated_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.families',
	components: { save },
	data(){
		return {
			dialog: {
				save: false
			},
			list: {
				apiObj: this.$API.account.family.list,
				column: []
			},
			search: {
				name: ''
			},
			selection: []
		}
	},
	mounted(){
	},
	methods:{
		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.family.delete.post({id: row.id})
			if(res.code === 1){
				this.$message.success('删除成功')
				this.upsearch()
			}else{
				this.$message.error(res.message || '删除失败')
			}
		}
	}
}
</script>

<style scoped>
</style>
