<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.keyword" placeholder="搜索关键词" clearable style="width: 150px;"></el-input>
				<el-select v-model="search.type" placeholder="记录类型" clearable style="width: 120px;">
					<el-option label="收入" value="income"></el-option>
					<el-option label="支出" value="expense"></el-option>
				</el-select>
				<el-date-picker
					v-model="search.date_range"
					type="daterange"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期"
					style="width: 260px;">
				</el-date-picker>
				<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="searchParams">
			<el-table-column type="selection" width="55" />
			<el-table-column prop="id" label="ID" width="80" />
			<el-table-column label="类型" width="80">
				<template #default="scope">
					<el-tag :type="scope.row.type === 'income' ? 'success' : 'danger'">
						{{ scope.row.type === 'income' ? '收入' : '支出' }}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="account.name" label="账户" width="120" />
			<el-table-column prop="category.name" label="分类" width="120" />
			<el-table-column prop="amount" label="金额" width="120" align="right">
				<template #default="scope">
					<span :style="{color: scope.row.type === 'income' ? '#67C23A' : '#F56C6C', fontSize: '16px', fontWeight: 'bold'}">
						{{ scope.row.type === 'income' ? '+' : '-' }}{{ scope.row.amount.toFixed(2) }}
					</span>
				</template>
			</el-table-column>
			<el-table-column prop="date" 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 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.records',
	components: { save },
	data(){
		return {
			dialog: {
				save: false
			},
			list: {
				apiObj: this.$API.account.records.list,
				column: []
			},
			search: {
				keyword: '',
				type: '',
				date_range: []
			},
			selection: []
		}
	},
	computed: {
		searchParams(){
			const params = {...this.search}
			if(params.date_range && params.date_range.length === 2){
				params.start_date = params.date_range[0]
				params.end_date = params.date_range[1]
			}
			delete params.date_range
			return params
		}
	},
	methods:{
		upsearch(){
			this.$refs.table.reload(this.searchParams);
		},
		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.records.delete.post({id: row.id})
			if(res.code === 1){
				this.$message.success('删除成功')
				this.upsearch()
			}else{
				this.$message.error(res.message || '删除失败')
			}
		}
	}
}
</script>

<style scoped>
</style>
