<template>
	<cl-crud @load="onLoad">
		<el-row type="flex">
			<cl-refresh-btn />
			<cl-add-btn />
			<cl-multi-delete-btn />
			<!-- <demo-query-isabnormal /> -->
			<cl-flex1 />
			<cl-search-key
				field="pc_num"
				:field-list="[
					{
						label: '电脑编号',
						value: 'pc_num'
					}
				]"
			></cl-search-key>
		</el-row>

		<el-row>
			<cl-table v-bind="table" >
				<!-- <template #column-pc_position="{ scope }">
					<span v-if="scope.row.is_rent == 0">{{
						scope.row.pc_position
					}}</span>
					<span v-if="scope.row.is_rent == 1">
						{{scope.row.pc_position}}（{{scope.row.shop_name}}）
					</span>
				</template> -->
			</cl-table>
		</el-row>

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

		<cl-upsert v-model="form" v-bind="upsert">
			<template #slot-rent_start="{ scope }">
				<el-date-picker
					v-model="scope.rent_start"
					type="date"
					:@change="calculateRentDuration(scope)"
					placeholder="选择租机日期">
				</el-date-picker>
			</template>
			<template #slot-rent_duration="{ scope }">
				<el-input v-model="scope.rent_duration" placeholder="租机时长（天）" @change="calculateRentDuration(scope)"></el-input>
			</template>
		</cl-upsert>
	</cl-crud>
</template>

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

export default defineComponent({
	name: "game-computer",

	components: {
		"demo-query-isabnormal": QueryIsAbnormal
	},

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

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

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

			items: [
				{
					prop: "pc_num",
					label: "电脑编号",
					span: 8,
					component: {
						name: "el-input",
						props: {
							placeholder: "租机方提供的电脑编号"
						}
					},
					rules: {
						required: true,
						message: "电脑编号不能为空"
					}
				},
				{
					prop: "pc_name",
					label: "电脑名称",
					span: 8,
					component: {
						name: "el-input",
						props: {
							placeholder: "脚本上传的电脑名"
						}
					},
					rules: {
						required: true,
						message: "电脑名称不能为空"
					}
				},
				{
					prop: "rent_person_id",
					label: "租赁人",
					span: 8,
					component: {
						name: "shuyouGameDirectorselect",
						props: {
							placeholder: "租赁人"
						}
					}
				},
				{
					prop: "shop_id",
					label: "供应商",
					span: 8,
					component: {
						name: "shuyouRentComputerShopSelect",
						props: {
							placeholder: "供应商"
						}
					},
					rules: {
						required: true,
						message: "供应商不能为空"
					}
				},
				// {
				// 	prop: "taskIdList",
				// 	label: "游戏任务",
				// 	span: 24,
				// 	component: {
				// 		name: "el-input",
				// 		props: {
				// 			placeholder: "游戏任务"
				// 		}
				// 	},
				// 	rules: {
				// 		required: true,
				// 		message: "游戏任务不能为空"
				// 	}
				// },
				{
					prop: "taskIdList",
					label: "游戏任务",
					span: 24,
					value: [],
					component: {
						name: "shuyouGameTaskNameselect",
						props: {
							props: {
								// "multiple-limit": 3
							}
						}
					},
					rules: {
						required: true,
						message: "游戏任务不能为空"
					}
				},
				{
					prop: "rent_start",
					label: "起租时间",
					span: 12,
					value: Date(),
					component: {
						name: "slot-rent_start"
						// name: "el-date-picker",
						// props: {
						// 	type: "date",
						// 	placeholder: "选择租机日期"
						// }
					},
					rules: {
						required: true,
						message: "起租时间不能为空"
					}
				},
				{
					prop: "rent_duration",
					label: "租机时长",
					value: 1,
					span: 12,
					component: {
						name: "slot-rent_duration"
					},
					rules: {
						required: true,
						message: "租机时长不能为空"
					}
				},
				{
					prop: "rent_end",
					label: "到期时间",
					span: 12,
					component: {
						name: "el-date-picker",
						props: {
							type: "date",
							readonly: true,
							placeholder: "选择到期时间"
						}
					}
				},
				{
					label: "租机价格",
					prop: "rent_price",
					value: 0.00,
					span: 12,
					component: {
						name: "el-input-number",
						props: {
							precision: "2",
							step: "1"
						}
					},
					rules: {
						required: true,
						message: "租机价格不能为空"
					}
				},
				{
					prop: "remark",
					label: "备注",
					span: 24,
					group: "base_msg",
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写备注",
							type: "textarea",
							rows: 3
						}
					}
				},
				{
					prop: "pay_method",
					label: "结算方式",
					value: 0,
					group: "base_msg",
					component: {
						name: "el-radio-group",
						options: [
							{
								label: "先付后用",
								value: 0
							},
							{
								label: "先用后付",
								value: 1
							},
							{
								label: "其他方式",
								value: 2
							}
						]
					}
				},
			]
		});

		// 表格配置
		const table = reactive<Table>({
			props: {
				"default-sort": {
					prop: "pc_num",
					order: "descending"
				}
			},
			columns: [
				{
					type: "selection",
					headerAlign: "left",
					align: "left",
					width: 40
				},
				{
					prop: "shop_name",
					label: "供应商",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 180,
				},
				{
					prop: "pc_num",
					label: "电脑编号",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 100,
				},
				{
					prop: "pc_name",
					label: "电脑名称",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 120
				},
				{
					prop: "rent_start",
					label: "起租时间",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 150
				},
				{
					prop: "rent_end",
					label: "到期时间",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 150
				},
				{
					prop: "rent_duration",
					label: "租机时常",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 120
				},
				{
					prop: "rent_price",
					label: "租机价格",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 120
				},
				{
					prop: "rent_price_day",
					label: "折算天租金",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 120
				},
				{
					prop: "rent_price_used",
					label: "已产生租金",
					sortable: "true",
					headerAlign: "left",
					align: "left",
					minWidth: 120
				},
				{
					prop: "directorNameList",
					label: "使用人",
					headerAlign: "left",
					align: "left",
					sortable: "custom",
					minWidth: 100
				},
				{
					prop: "pay_method",
					label: "结算方式",
					minWidth: 80,
					dict: [
						{
							label: "先付后用",
							value: 0,
							type: "primary"
						},
						{
							label: "先用后付",
							value: 1,
							type: "success"
						},
						{
							label: "其他方式",
							value: 2,
							type: "danger"
						}
					]
				},
				{
					prop: "rent_person_name",
					label: "租赁人",
					headerAlign: "left",
					align: "left",
					sortable: "custom",
					minWidth: 100
				},
				{
					prop: "remark",
					label: "备注",
					headerAlign: "left",
					align: "left",
					minWidth: 100
				},
				{
					prop: "updateTime",
					label: "更新时间",
					headerAlign: "left",
					align: "left",
					sortable: "custom",
					minWidth: 150
				},
				{
					label: "操作",
					type: "op"
				}
			]
		});

		// crud 加载
		function onLoad({ ctx, app }: CrudLoad) {
			// console.log(service)
			ctx.service(service.shuyouRentComputer).done();
			app.refresh();
		}
		// 计算租机到期时间
		function calculateRentDuration(scope: { rent_end: Date; rent_start: string; rent_duration: string; }) {
			scope.rent_end = new Date(Date.parse(scope.rent_start) + 1 * 24 * 60 * 60 * 1000 * parseInt(scope.rent_duration))
		}
		// 监听
		watch(
			() => form,
			(newVal, oldValue) => {
				// console.log(newVal, oldValue);
				if(form.is_rent == 1){
					form.pc_position = "租机"
				}
			},
			{
				deep: true,
				immediate: true
			}
		);
		return {
			form,
			upsert,
			table,
			onLoad,
			calculateRentDuration
		};
	}
});
</script>
