<template>
	<cl-crud @load="onLoad">
		<el-row type="flex">
			<cl-refresh-btn />
			<cl-add-btn>新增单个</cl-add-btn>
			<el-button type="primary" size="mini" @click="reqFlag.visible = true"
				>批量上传</el-button
			>
			<cl-multi-delete-btn />
			<cl-flex1 />
			<cl-search-key
				field="card_text"
				:field-list="[
					{
						label: '卡号',
						value: 'card_text'
					}
				]"
			></cl-search-key>
		</el-row>

		<el-row>
			<cl-table v-bind="table"> </cl-table>
		</el-row>

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

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

		<!-- 批量上传 -->
		<cl-dialog
			title="卡号上传界面"
			v-model="reqFlag.visible"
			:props="{
				modal: true,
				customClass: 'cl-chat__dialog',
				'append-to-body': true,
				'close-on-click-modal': false,
				'close-on-press-escape': false
			}"
		>
			<div class="el-div">
				<h3 class="v-line-icon">批量上传卡号</h3>
				<el-scrollbar>
					<el-form
						:model="ruleForm"
						ref="ruleForm"
						label-width="100px"
						size="mini"
						class="demo-ruleForm"
						:disabled="saving"
					>
						<!-- <el-form-item label="上传时间" prop="uploadday">
							<el-col :span="12">
								<el-date-picker
									v-model="uploadday"
									type="date"
									placeholder="选择日期"
									:disabled-date="disabledDate"
									:shortcuts="shortcuts"
									format="YYYY-MM-DD"
									value-format="YYYY-MM-DD"
								>
								</el-date-picker>
							</el-col>
						</el-form-item> -->

						<el-form-item label="卡号类型" prop="type_id">
							<el-col :span="12">
								<special-accountType-select v-model="type_id" />
							</el-col>
						</el-form-item>

						<el-form-item label="选择文件" prop="">
							<el-col :span="24">
								<cl-upload
									v-model="urls"
									accept=".txt"
									list-type="text"
									:limi="1"
									limit-size="10"
								>
									<el-button size="small" style="width: 100%" type="info"
										>点击上传</el-button
									>
									<div slot="tip" class="el-upload__tip" style="color: #ff4200">
										Tips:
										只能上传txt文件，格式：XXXXXXXXX----XXXXXXXXXXXX----60.00
									</div>
								</cl-upload>
							</el-col>
						</el-form-item>
						<el-form-item label="单卡金额" prop="amount_all">
							<el-col :span="6">
								<el-input-number
									v-model="amount_all"
									:precision="2"
									:step="0.1"
									:max="100"
								/>
							</el-col>
						</el-form-item>
						<el-form-item label="提示备注" prop="remark">
							<el-col :span="6">
								<el-input v-model="remark" placeholder="可不填" />
							</el-col>
						</el-form-item>
						<el-form-item>
							<el-button
								size="mini"
								type="primary"
								:disabled="saving"
								@click="batchAdd"
							>
								批量上传
								<i class="el-icon-upload el-icon--right"></i>
							</el-button>
						</el-form-item>
					</el-form>
				</el-scrollbar>
			</div>
		</cl-dialog>
	</cl-crud>
</template>

<script lang="ts">
import { CrudLoad, Table, Upsert } from "cl-admin-crud-vue3/types";
import { defineComponent, inject, reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import { currentDate } from "../utils";
import { ElMessage, ElLoading, ElMessageBox } from "element-plus";
import specialAccountTypeselect from "../components/specialAccountType-select.vue";

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

	components: {
		"special-accountType-select": specialAccountTypeselect
	},

	data() {
		return {
			rules: {
				uploadtime: [{ required: true, message: "请选择上传时间", trigger: "blur" }]
			}
		};
	},

	setup() {
		const service = inject<any>("service");
		// const { refs, setRefs }: any = useRefs();
		const router = useRouter();

		const urls = ref<string>("");

		// 文件列表
		const fileList = ref<any[]>([]);
		// 文本内容
		const accounts = ref<any[]>([]);
		// 标致
		const reqFlag = reactive({
			visible: false,
			repeat: false,
			errorTips: false,
			preview: false,
			error: ""
		});
		const type_id = ref<string>("");
		const remark = ref<string>("");
		const amount_all = ref<number>(60);
		const repeatTips = ref<string>("");
		const repeatAccount = ref<string>("");
		// 保存状态
		const saving = ref<boolean>(false);
		// 表单值
		const form = reactive<any>({
			relevance: 1
		});

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

			items: [
				{
					prop: "card_text",
					label: "卡号",
					span: 10,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写卡号"
						}
					},
					rules: {
						required: true,
						message: "卡号不能为空"
					}
				},
				{
					prop: "card_password",
					label: "密码",
					span: 10,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写密码"
						}
					},
					rules: {
						required: true,
						message: "密码不能为空"
					}
				},
				{
					prop: "type_id",
					label: "卡类型",
					span: 10,
					component: {
						name: "specialAccountTypeselect",
						props: {
							placeholder: "卡类型"
						}
					},
					rules: {
						required: true,
						message: "卡类型不能为空"
					}
				},
				{
					prop: "amount_all",
					label: "总金额",
					span: 10,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写总金额"
						}
					}
				},
				{
					prop: "amount_balance",
					label: "余额",
					span: 10,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写余额"
						}
					}
				},
				{
					prop: "remark",
					label: "备注",
					span: 10,
					component: {
						name: "el-input",
						props: {
							placeholder: "请填写备注"
						}
					}
				}
			]
		});

		// 表格配置
		const table = reactive<Table>({
			props: {
				"default-sort": {
					prop: "id",
					order: "descending"
				}
			},
			columns: [
				{
					type: "selection",
					width: 60
				},
				{
					prop: "card_text",
					label: "卡号",
					sortable: "true",
					minWidth: 120,
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "card_password",
					label: "密码",
					minWidth: 120,
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "type_name",
					label: "卡类型",
					width: 120,
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "status",
					label: "状态",
					minWidth: 80,
					dict: [
						{
							label: "未使用",
							value: 0,
							type: "success"
						},
						{
							label: "已归还",
							value: 1,
							type: "warning"
						},
						{
							label: "已取出不锁",
							value: 2,
							type: "warning"
						},
						{
							label: "已取出并加锁",
							value: 3,
							type: "info"
						},
						{
							label: "失效",
							value: 4,
							type: "info"
						},
						{
							label: "余额不足",
							value: 5,
							type: "info"
						},
						{
							label: "无法使用",
							value: 6,
							type: "info"
						},
						{
							label: "已被删除",
							value: 7,
							type: "info"
						}
					]
				},
				{
					prop: "amount_all",
					label: "总金额",
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "amount_balance",
					label: "余额",
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "releaseTime",
					label: "释放时间",
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "get_time",
					label: "取走时间",
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "remark",
					label: "备注",
					width: 120,
					headerAlign: "left",
					align: "left"
				},
				{
					prop: "createTime",
					label: "上传时间",
					sortable: "custom",
					minWidth: 150,
					headerAlign: "left",
					align: "left"
				},
				{
					label: "操作",
					width: 100,
					type: "op"
				}
			]
		});

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

		// const state = reactive({
		// 	disabledDate(time) {
		// 		return time.getTime() > Date.now();
		// 	},
		// 	shortcuts: [
		// 		{
		// 			text: "今天",
		// 			value: new Date()
		// 		},
		// 		{
		// 			text: "昨天",
		// 			value: () => {
		// 				const date = new Date();
		// 				date.setTime(date.getTime() - 3600 * 1000 * 24);
		// 				return date;
		// 			}
		// 		},
		// 		{
		// 			text: "一周前",
		// 			value: () => {
		// 				const date = new Date();
		// 				date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
		// 				return date;
		// 			}
		// 		}
		// 	],
		// 	uploadday: new Date()
		// });

		// 批量新增
		async function batchAdd() {
			// console.log(type_id.value);
			if (urls.value == "") {
				ElMessage.error("文件为空");
				return;
			}
			saving.value = true;
			const loading = ElLoading.service({
				lock: true,
				text: "上传中,请勿关闭此页面...",
				spinner: "el-icon-loading",
				background: "rgba(0, 0, 0, 0.7)"
			});
			const params = {
				// uploadday: state.uploadday,
				type_id: type_id.value,
				amount_all: amount_all.value,
				remark: remark.value,
				fileaddress: urls
			};
			await service.shuyouSpecialAccount
				.batchAdd(params)
				.then((data) => {
					reqFlag.visible = false;
					loading.close();
					// if (data.code == -1) {
					// 	ElMessage.success(data.message);
					// } else {
					// 	ElMessage.success(data.message);
					// }
					ElMessageBox.alert(data.message, "提示", {
						confirmButtonText: "关闭"
					});
				})
				.catch((err: string) => {
					loading.close();
					ElMessageBox.alert(err, "提示", {
						confirmButtonText: "关闭"
					});
				});
			saving.value = false;
		}

		return {
			service,
			form,
			reqFlag,
			fileList,
			accounts,
			repeatTips,
			repeatAccount,
			saving,
			// ...toRefs(state),
			type_id,
			remark,
			upsert,
			amount_all,
			table,
			onLoad,
			batchAdd,
			urls
		};
	},

	methods: {
		// submitForm(formName) {
		//   this.$refs[formName].validate((valid) => {
		//     if (valid) {
		// 		// alert('submit!')
		// 		const params = {
		// 			id_name: '张飞'
		// 		}
		// 		alert(this.accounts[0].name)
		// 		this.$service.shuyou.idCard.data2(params);
		// 		// return this.request({
		// 		// 	url: "/data",
		// 		// 	method: "GET",
		// 		// 	params
		// 		// 	// 参数与 axios 一致
		// 		// });
		//     } else {
		// 		console.log('error submit!!')
		// 		return false
		//     }
		//   })
		// },
		// resetForm(formName) {
		// 	this.$refs[formName].resetFields()
		// 	this.accounts = []
		// },
		// openFile(file,fileList) {
		// 	this.reqFlag.errorTips = false;
		// 	this.fileList = fileList.slice(-1)
		// 	let fileName = file.name
		// 	var reader = new FileReader();
		// 	// alert(fileName)
		// 	let pos = fileName.lastIndexOf('.')
		// 	let lastName = fileName.substring(pos, fileName.length)
		// 	const isLt2M = file.size / 1024 / 1024 < 20
		// 	if (lastName.toLowerCase() !== '.txt') {
		// 		this.reqFlag.error = '请上传.txt文件类型'
		// 		this.reqFlag.errorTips = true;
		// 		this.accounts = []
		// 	} else if (!isLt2M) {
		// 		this.reqFlag.error = '文件大小不能超过10MB'
		// 		this.reqFlag.errorTips = true;
		// 		this.accounts = []
		// 	} else {
		// 		reader.onload = e => this.$emit('load', this.dealNum(reader.result))
		// 		reader.readAsText(file.raw)
		// 	}
		// },
		// dealNum (item) {
		//     // let word = item.replace(/\n|\r\n/g, '<br/>')
		//     let word = item
		//     var dataArr = word.split(/[\r\n]+/) // /[(\r\n)\r\n]+/
		//     dataArr.forEach((item, index) => {
		//         if (!item) {
		//             dataArr.splice(index, 1)// 删除空项
		//         }
		//     })
		//     let objList = []
		//     dataArr.forEach((item, index) => {
		//           let obj = {
		//             index: index,
		//             name: item
		//           }
		//           objList.push(obj)
		//     })
		//     this.accounts = objList
		//     this.reqFlag.preview = true
		// },
	}
});
</script>
<style>
.el-div {
	display: flex;
	flex-direction: column;
	height: 100%;
	position: relative;
	padding: 10px;
	box-sizing: border-box;
	background-color: #fff;
	overflow: hidden;
}
/* 红色短竖线 start */
.v-line-icon {
	position: relative;
	padding-left: 20px;
	margin-bottom: 20px;
}
.v-line-icon:before {
	position: absolute;
	content: "";
	width: 5px;
	height: 18px;
	left: 10px;
	top: 50%;
	margin-top: -9px;
	border-radius: 2px;
	background: #ff4200;
}

/* 红色短竖线 end */
</style>
