<template>
	<pi-table ref="tableRef" :params="search" :api="api.system.dict_data.list" @selection-change="selectionChange">
		<template #extend>
			<el-select v-model="search.status" style="width: 200px;" placeholder="是否启用" clearable>
				<el-option label="启用" :value="1"></el-option>
				<el-option label="禁用" :value="0"></el-option>
			</el-select>
		</template>
		<template #do>
			<el-button v-auth="'dict_data:add'" type="primary" icon="el-icon-plus" @click="add"></el-button>
			<el-button v-auth="'dict_data:edit'" type="success" icon="el-icon-edit" @click="edit"
					   :disabled="selection.length!==1"></el-button>
			<el-button v-auth="'dict_data:del'" type="danger" plain icon="el-icon-delete"
					   :disabled="selection.length===0" @click="batch_del"></el-button>
		</template>
		<template #search>
			<el-select v-model="search.dict_id" style="width: 200px;" placeholder="字典ID" clearable>
				<el-option v-for="item in dict" :label="item.dict_name" :value="item.dict_id"></el-option>
			</el-select>
			<el-input v-model="search.dict_label" placeholder="字典标签" clearable style="width: 200px;"></el-input>
			<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
		</template>
		<el-table-column type="selection" width="50"></el-table-column>
		<el-table-column label="ID" prop="data_id"></el-table-column>
		<el-table-column label="字典标签" prop="dict_label"></el-table-column>
		<el-table-column label="字典键值" prop="dict_value"></el-table-column>
		<el-table-column label="是否启用" prop="status">
			<template #default="scope">
				<el-tag v-if="scope.row.status" type="primary">是</el-tag>
				<el-tag v-else type="info">否</el-tag>
			</template>
		</el-table-column>
		<el-table-column label="是否默认" prop="is_default">
			<template #default="scope">
				<el-tag v-if="scope.row.is_default" type="primary">是</el-tag>
				<el-tag v-else type="info">否</el-tag>
			</template>
		</el-table-column>
		<el-table-column label="排序" prop="rank"></el-table-column>
		<el-table-column label="备注" prop="remark"></el-table-column>
		<el-table-column label="创建时间" prop="create_time"></el-table-column>
		<el-table-column label="更新时间" prop="update_time"></el-table-column>
		<el-table-column label="操作" fixed="right" align="right" width="170">
			<template #default="scope">
				<el-button-group>
					<el-button text type="primary" size="small" @click="show(scope.row, scope.$index)">查看
					</el-button>
					<el-button v-auth="'dict_data:edit'" text type="success" size="small"
							   @click="edit(scope.row, scope.$index)">编辑
					</el-button>
					<el-popconfirm title="确定删除吗？" @confirm="del(scope.row, scope.$index)">
						<template #reference>
							<el-button v-auth="'dict_data:del'" text type="danger" size="small">删除</el-button>
						</template>
					</el-popconfirm>
				</el-button-group>
			</template>
		</el-table-column>
	</pi-table>
	<save-dialog v-if="dialogShow" ref="dialogRef" @success="tableRef.refresh()"
				 @closed="dialogShow=false"></save-dialog>
</template>

<script setup>
import saveDialog from './save'
import api from "@/api/index";
import {getCurrentInstance, nextTick, ref, onMounted} from "vue";
import {useRoute} from 'vue-router'

defineOptions({
	name: "dictData"
})

const {proxy} = getCurrentInstance()
const tableRef = ref(null)
const dialogRef = ref(null)
const route = useRoute()
const props = defineProps({
	id: {type: String}
})

let dialogShow = ref(false)
let selection = ref([])
let search = ref({
	dict_id: parseInt(props.id),
	dict_label: null,
	status: null,
})
let dict = ref([])

onMounted(() => {
	loadDict()
})

async function loadDict() {
	const res = await api.system.dict.option()
	dict.value = res.data
}

//添加
function add() {
	dialogShow.value = true
	nextTick(() => {
		dialogRef.value.setDicts(dict.value).open('add', {dict_id: search.value.dict_id})
	})
}

//编辑
function edit(row) {
	dialogShow.value = true
	nextTick(() => {
		if (row instanceof PointerEvent) {
			row = selection.value[0]
		}
		dialogRef.value.setDicts(dict.value).open('edit', row)
	})
}

//查看
function show(row) {
	dialogShow.value = true
	nextTick(() => {
		dialogRef.value.setDicts(dict.value).open('show', row)
	})
}

//删除
async function del(row) {
	const loading = proxy.$loading();
	const res = await api.system.dict_data.del({ids: [row.data_id]});
	tableRef.value.refresh()
	loading.close();
	proxy.$message.success(res.msg)
}

//批量删除
async function batch_del() {
	proxy.$confirm(`确定删除选中的 ${selection.value.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
		type: 'warning'
	}).then(async () => {
		const loading = proxy.$loading();
		const res = await api.system.dict_data.del({ids: selection.value.map(item => item.data_id)});
		tableRef.value.refresh()
		loading.close();
		proxy.$message.success(res.msg)
	})
}

//表格选择后回调事件
function selectionChange(e) {
	selection.value = e;
}

//搜索
function upsearch() {
	tableRef.value.upData(search.value)
}
</script>
