<template>
	<div class="table-box">
		<ProTable
			ref="proTable"
			title="用户列表"
			:columns="columns"
			:requestApi="getTableList"
			:initParam="initParam"
			:dataCallback="dataCallback"
			:tool-button="false"
			:pagination="true"
			background
		>
			<!-- 表格操作 -->
			<template #downloadPath="scope">
				<el-button type="primary" link @click="handleEditItem(scope.row)">下载</el-button>
				<el-button type="primary" link v-copy="`${fileUrl + scope.row.downloadPath}`">复制apk地址</el-button>
			</template>
			<template #operation="{ row }">
				<el-button type="primary" link @click="onUpload(row)"
					><el-icon><RefreshRight /></el-icon>更新版本</el-button
				>
			</template>
		</ProTable>
		<el-dialog v-model="visible" title="更新版本" width="40%">
			<el-form :data="formData" center>
				<el-form-item label="版本名称:">
					<el-input v-model="formData.versionName" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="版本号:">
					<el-input v-model="formData.versionNum" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="版本描述:">
					<el-input v-model="formData.versionDesc" type="textarea" />
				</el-form-item>
				<el-form-item label="上传apk:">
					<el-upload
						:headers="headers"
						v-model:file-list="fileList"
						class="upload-demo"
						action="http://192.168.34.122:6688/xmgl/file/upload"
						multiple
						:limit="1"
						:on-success="uploadSuccess"
					>
						<el-button type="primary">点击上传</el-button>
						<template #tip>
							<div class="el-upload__tip">apk文件大小不能超过500KB.</div>
						</template>
					</el-upload>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="visible = false">取消</el-button>
					<el-button type="primary" @click="onSave(formData)">保存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="tsx" name="jxjSystem">
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { User } from "@/api/interface";
import { GlobalStore } from "@/stores";
import { ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import ProTable from "@/components/ProTable/index.vue";
import { CirclePlus, Delete, EditPen, Download, Upload, View, Refresh } from "@element-plus/icons-vue";
import { getAppList, editApp, addApp, exportApp } from "@/api/modules/jxjview";
import { fa } from "element-plus/es/locale";

const router = useRouter();
const globalStore = GlobalStore();
const fileUrl = ref("http://192.168.34.122:6688/xmgl/file/preview?fileUrl=");
const data = ref<string>("复制成功");
const headers = ref({ Authorization: "Bearer " + globalStore.token });
const fileList = ref([]);

const visible = ref(false);
const formData = ref({
	versionName: "",
	versionNum: "",
	versionDesc: "",
	fileName: "",
	downloadPath: "",
	versionId: 0
});

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();

// 修改数据按钮
const handleEditItem = async (row: any) => {
	ElMessageBox.confirm("确认下载数据?", "温馨提示", { type: "warning" }).then(() =>
		useDownload(exportApp, "用户列表", { fileUrl: row.downloadPath })
	);
};

// 表格配置项
const columns: ColumnProps[] = [
	{
		prop: "versionId",
		label: "序号"
	},
	{
		prop: "versionName",
		label: "版本名称"
		// search: { el: "input" }
	},
	// 多级 prop
	{ prop: "versionNum", label: "版本号" },
	{ prop: "versionDesc", label: "版本描述" },
	{ prop: "downloadPath", label: "apk地址" },
	{ prop: "operation", label: "操作", fixed: "right", width: 330 }
];

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
	type: 1
});

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，那么你可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
	// console.log(data);
	return {
		list: data.records,
		total: Number(data.total),
		pageNo: Number(data.current),
		pageSize: Number(data.size)
	};
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
	let newParams = JSON.parse(JSON.stringify(params));
	return getAppList(newParams);
};

const onUpload = (row: any) => {
	visible.value = true;
	formData.value.versionId = row.versionId;
	console.log(row);

	fileList.value = [{ name: row.fileName, url: row.downloadPath }];
	formData.value = reactive(row);
	// if (row.downloadPath) {
	// 	fileList.value.push({ name: row.versionName, url: row.downloadPath });
	// }
	// formData.value?.resetFields();
};
const onSave = async (params: any) => {
	const res = await editApp(params);
	visible.value = false;
	proTable.value.getTableList();
};
const uploadSuccess = (response: any) => {
	console.log(response);

	ElMessage.success("上传成功");
	formData.value.fileName = response.result.originalFilename;
	formData.value.downloadPath = response.result.url;
	// fileList.value = [{ name: response.result.originalFilename, url: response.result.downloadPath }];
};
</script>
