<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 style="width: 150px;"></el-input>
				<el-select v-model="search.type" placeholder="账户类型" clearable style="width: 120px;">
					<el-option label="现金" value="cash"></el-option>
					<el-option label="银行卡" value="bank_card"></el-option>
					<el-option label="信用卡" value="credit_card"></el-option>
					<el-option label="支付宝" value="alipay"></el-option>
					<el-option label="微信" value="wechat"></el-option>
					<el-option label="其他" value="other"></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="name" label="账户名称" min-width="150" />
			<el-table-column label="账户类型" width="100">
				<template #default="scope">
					<el-tag :type="getTypeColor(scope.row.type)">{{ getTypeName(scope.row.type) }}</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="balance" label="余额" width="120" align="right">
				<template #default="scope">
					<span :style="{color: scope.row.balance >= 0 ? '#67C23A' : '#F56C6C'}">
						{{ scope.row.balance.toFixed(2) }}
					</span>
				</template>
			</el-table-column>
			<el-table-column prop="family_name" label="所属家庭" width="120" />
			<el-table-column prop="remark" label="备注" min-width="150" show-overflow-tooltip />
			<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.accounts',
	components: { save },
	data(){
		return {
			dialog: {
				save: false
			},
			list: {
				apiObj: this.$API.account.accounts.list,
				column: []
			},
			search: {
				name: '',
				type: ''
			},
			selection: []
		}
	},
	methods:{
		getTypeName(type){
			const map = {
				'cash': '现金',
				'bank_card': '银行卡',
				'credit_card': '信用卡',
				'alipay': '支付宝',
				'wechat': '微信',
				'other': '其他'
			}
			return map[type] || type
		},
		getTypeColor(type){
			const map = {
				'cash': 'success',
				'bank_card': 'primary',
				'credit_card': 'warning',
				'alipay': 'success',
				'wechat': 'success',
				'other': 'info'
			}
			return map[type] || '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.accounts.delete.post({id: row.id})
			if(res.code === 1){
				this.$message.success('删除成功')
				this.upsearch()
			}else{
				this.$message.error(res.message || '删除失败')
			}
		}
	}
}
</script>

<style scoped>
</style>
