<template>
	<cl-crud @load="onLoad">
		<el-row type="flex">
			<cl-refresh-btn />
			<cl-add-btn />
			<cl-multi-delete-btn />
			<cl-flex1 />
			<cl-search-key
				field="name"
				:field-list="[
					{
						label: '姓名',
						value: 'name'
					}
				]"
			></cl-search-key>
			<cl-adv-btn></cl-adv-btn>
			<cl-adv-search :items="items" :op-list="opList"></cl-adv-search>
		</el-row>

		<el-row>
			<cl-table v-bind="table">
				<!-- <template #column-icon="{ scope }">
					<el-image
					    style="width: 100px; height: 100px"
					    :src="scope.row.icon"
					    :preview-src-list="[scope.row.icon]"
					  />
				</template> -->
				<!-- 头像 -->
				<template #column-headImg="{ scope }">
					<cl-avatar
						shape="square"
						size="medium"
						:src="scope.row.headImg"
						:style="{ margin: 'auto' }"
					/>
				</template>
			</cl-table>
		</el-row>

		<el-row type="flex">
			<cl-flex1 />
			<cl-pagination />
		</el-row>

		<cl-upsert v-model="form" v-bind="upsert" />
	</cl-crud>
</template>

<script lang="ts">
import { CrudLoad, Table, Upsert } from "cl-admin-crud-vue3/types";
import { defineComponent, inject, reactive } from "vue";

export default defineComponent({
	name: "sys-role",

	setup() {
		const service = inject<any>("service");

		// 表单值
		const form = reactive<any>({
			relevance: 1
		});

		// 新增、编辑配置
		const upsert = reactive<Upsert>({
			width: "800px",

			items: [
				{
					prop: "headImg",
					label: "头像",
					span: 12,
					component: {
						name: "cl-upload",
						props: {
							text: "选择头像",
							icon: "el-icon-picture"
						}
					}
				},
				{
					prop: "departmentName",
					label: "部门",
					span: 12,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写部门"
						}
					}
				},
				{
					prop: "name",
					label: "姓名",
					span: 12,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写姓名"
						}
					},
					rules: {
						required: true,
						message: "姓名不能为空"
					}
				},
				{
					prop: "nickName",
					label: "昵称",
					span: 12,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写昵称"
						}
					}
				},
				{
					prop: "phone",
					label: "手机号码",
					span: 12,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写手机号码"
						}
					}
				},
				{
					prop: "email",
					label: "邮箱",
					span: 12,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写邮箱"
						}
					}
				},
				{
					prop: "remark",
					label: "备注",
					span: 24,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写备注",
							type: "textarea",
							rows: 4
						}
					}
				},
				{
					prop: "status",
					label: "状态",
					value: 1,
					component: {
						name: "el-radio-group",
						options: [
							{
								label: "开启",
								value: 1
							},
							{
								label: "关闭",
								value: 0
							}
						]
					}
				}
			]
		});

		// 表格配置
		const table = reactive<Table>({
			props: {
				"default-sort": {
					prop: "name",
					order: "descending"
				}
			},
			columns: [
				{
					type: "selection",
					width: 60
				},
				{
					prop: "headImg",
					label: "头像"
				},
				{
					prop: "name",
					label: "姓名",
					minWidth: 150
				},
				{
					prop: "nickName",
					label: "昵称",
					minWidth: 150
				},
				{
					prop: "departmentName",
					label: "部门名称",
					minWidth: 150
				},
				{
					prop: "phone",
					label: "手机号码",
					minWidth: 150
				},
				{
					prop: "game_count",
					label: "游戏数量",
					minWidth: 150
				},
				{
					prop: "remark",
					label: "备注",
					minWidth: 150
				},
				{
					prop: "status",
					label: "状态",
					minWidth: 120,
					dict: [
						{
							label: "启用",
							value: 1,
							type: "success"
						},
						{
							label: "禁用",
							value: 0,
							type: "danger"
						}
					]
				},
				{
					prop: "createTime",
					label: "创建时间",
					sortable: "custom",
					minWidth: 150
				},
				{
					type: "op",
					buttons: ["edit", "delete"],
					width: 160
				}
			]
		});

		// crud 加载
		function onLoad({ ctx, app }: CrudLoad) {
			// console.log(service);
			ctx.service(service.shuyouGameDirector).done();
			app.refresh();
		}

		return {
			form,
			upsert,
			table,
			onLoad
		};
	}
});
</script>
