<template>
	<lay-container fluid="true" style="padding: 10px">
		<lay-row space="10">
			<lay-col span="24">
				<lay-card>
					<lay-form :model="searchObj" class="search-box">
						<lay-row class="search-items">
							<lay-form-item label="分类名称" prop="FName">
								<lay-input v-model="searchObj.FName"></lay-input>
							</lay-form-item>
							<lay-form-item label="分类状态" prop="FState">
								<lay-select v-model="searchObj.FState">
									<lay-select-option value="1" label="有效"></lay-select-option>
									<lay-select-option value="0" label="无效"></lay-select-option>
								</lay-select>
							</lay-form-item>
						</lay-row>
						<lay-form-item class="search-btn">
							<lay-button size="sm" type="primary" @click.stop="_clickSearch">搜索</lay-button>
							<lay-button size="sm" @click.stop="_clickReset">重置</lay-button>
						</lay-form-item>
					</lay-form>
					<lay-table :columns="dataColumn" id="id" :dataSource="dataList" v-model:selectedKeys="selectedKeys"
						:checkbox="checkbox" :default-toolbar="defaultToolbar" :page="dataList.length>0?pageInfo:null"
						@change="changePage">
						<template v-slot:toolbar>
							<lay-button size="sm" @click.stop="_getPageList(false,true)">刷新</lay-button>
							<lay-button size="sm" type="normal" @click.stop="_changeData()">新增</lay-button>
						</template>
						<template v-slot:FDepth="{ data }">
							{{depthName[data.FDepth]}}级分类
						</template>
						<template v-slot:operator="{ data }">
							<lay-button v-if="(objInfoObj&&objInfoObj.FDepth<maxDepth)||!objInfoObj" size="xs"
								type="normal" @click.stop="_childData(data)"> 子分类 </lay-button>
							<lay-button v-else size="xs" type="normal" @click.stop="_infoData(data)">基础资料
							</lay-button>
							<lay-button size="xs" type="primary" @click.stop="_changeData(data)">编辑分类</lay-button>
							<lay-button size="xs" type="danger" @click.stop="_deleteData(data.FID)">删除分类</lay-button>
						</template>
					</lay-table>
				</lay-card>
			</lay-col>
		</lay-row>
		<!-- 子分类 -->
		<lay-layer v-if="(objInfoObj&&objInfoObj.FDepth<maxDepth)||!objInfoObj" area="80%" v-model="showEditBox[0]"
			:title="editBoxTitle[0]">
			<type :objInfoObj="childInfoObj" @cancelClick="cancelClick"></type>
		</lay-layer>
		<!-- 基础资料 -->
		<lay-layer v-else area="90%" v-model="showEditBox[0]" title="基础资料">
			<EditMaterialInfo :objInfoObj="childInfoObj" @cancelClick="cancelClick"></EditMaterialInfo>
		</lay-layer>
		<!-- 编辑分类 -->
		<lay-layer area="600px" v-model="showEditBox[1]" :title="editBoxTitle[1]">
			<EditType :objInfoObj="childInfoObj" @cancelClick="cancelClick"></EditType>
		</lay-layer>
	</lay-container>

</template>
<script>
	import {
		ref,
		watch
	} from "vue";
	import {
		deleteDataById,
		getPageList
	} from "/src/api/api/common";

	import '@layui/layui-vue/es/checkbox/index.css';
	import EditType from './components/EditType.vue';
	import EditMaterialInfo from './components/EditMaterialInfo.vue';

	export default {
		components: {
			EditType,
			EditMaterialInfo
		},
		setup() {
			let dataColumn = [{
					type: "number",
					title: "序号",
					width: "56"
				}, {
					title: "分类名称",
					key: "FName"
				},
				{
					title: "分类层级",
					key: "FDepth",
					customSlot: "FDepth",
					width: "160"
				},
				{
					title: "分类状态",
					key: "FState",
					width: "130"
				},
				{
					title: "创建时间",
					key: "FAddDate",
					width: "200"
				},
				{
					title: "最后修改时间",
					key: "FEditDate",
					width: "200"
				},
				{
					title: "操作",
					key: "operator",
					customSlot: "operator",
					width: "300"
				}
			];
			dataColumn.forEach((item) => {
				item.align = "center";
			});
			const selectedKeys = ref(["1"]);
			const checkbox = ref(false);
			const defaultToolbar = ref(true);
			const showEditBox = ref([false, false]);
			const searchObj = ref({});
			const postData = ref({});
			const pageInfo = ref({
				total: 0,
				limit: 10,
				current: 1
			});
			const dataList = ref([]);
			const editBoxTitle = ref(["", ""]);
			const childInfoObj = ref({});
			return {
				editBoxTitle,
				showEditBox,
				selectedKeys,
				checkbox,
				defaultToolbar,
				dataColumn,
				dataList,
				pageInfo,
				searchObj,
				postData,
				depthName: ["零", "一", "二", "三", "四"],
				maxDepth: 1, //+1层
				childInfoObj
			};
		},
		props: {
			objInfoObj: {
				type: Object,
				default: () => {
					FDepth: 1
				}
			}
		},
		mounted() {
			this._getPageList(true);
		},
		methods: {
			changePage(obj) {
				this.pageInfo.current = obj.current;
				this.pageInfo.limit = obj.limit;
				this._getPageList();
			},
			//获取数据集合
			async _getPageList(isFirst, showOk) {
				this.pageInfo.total = 0;
				if (isFirst) {
					this.pageInfo.current = 1;
					this.pageInfo.total = 0;
				}
				this.postData.FPageIndex = this.pageInfo.current;
				this.postData.FPageSize = this.pageInfo.limit;
				this.postData.FDepth = this.objInfoObj ? this.objInfoObj.FDepth + 1 : 1;
				this.postData.FParentID = this.objInfoObj ? this.objInfoObj.FID : -1;
				this.postData.FPageOrderBy = "FID";
				let result = await getPageList(this.postData, "MaterialType");
				result.Data.List = result.Data.List || [];
				result.Data.List.forEach((item) => {
					item.FAddDate = item.FAddDate == null ? "" : this.dateFormat(item.FAddDate);
					item.FEditDate = item.FEditDate == null ? "" : this.dateFormat(item.FEditDate);
					item.FState = item.FState == 1 ? "有效" : "无效";

					if (this.objInfoObj) {
						item.ParentName = this.objInfoObj.FName
					}
				});
				this.pageInfo.total = result.Data.Total || 0;
				this.dataList = result.Data.List;
			},
			//日期处理
			dateFormat(dataStr) {
				return dataStr.replace('T', ' ').split('.')[0];
			},
			//删除弹窗
			_deleteData(userId) {
				let $this = this;
				layer.confirm("你确定要删除此分类吗？", {
					btn: [{
							text: '确定',
							callback: function(id) {
								$this.__deleteData(id, userId);
							}
						},
						{
							text: '取消',
							callback: function(id) {
								layer.close(id);
							}
						}
					]
				});
			},
			//删除信息
			async __deleteData(layId, userId) {
				let result = await deleteDataById(userId, 'MaterialType');
				layer.close(layId);
				if (result && result > 0) {
					layer.msg("操作成功", {
						time: 1000,
						icon: 1
					})
				} else {
					layer.msg("操作失败", {
						time: 1000,
						icon: 2
					})
				}
				this._getPageList(true);
			},
			//子分类
			_childData(dataObj) {
				this.editBoxTitle[0] = dataObj.FName + "子分类";
				this.showEditBox[0] = true;
				this.childInfoObj = dataObj;
			},
			//基础资料
			_infoData(dataObj) {
				this.showEditBox[0] = true;
				this.childInfoObj = dataObj;
			},
			//修改信息
			_changeData(dataObj) {
				this.editBoxTitle[1] = (dataObj ? "修改" : "添加") + "分类";
				if (!dataObj) {
					dataObj = dataObj || {};
					dataObj.FDepth = this.objInfoObj ? this.objInfoObj.FDepth + 1 : 1;
					dataObj.FParentID = this.objInfoObj ? this.objInfoObj.FID : -1;
				}
				this.childInfoObj = dataObj || {};
				this.showEditBox[1] = true;
				return false;
			},
			//取消操作
			cancelClick(isRefresh) {
				this.showEditBox = [false, false];
				if (isRefresh) {
					this._getPageList(true);
				}
			},
			//检索-检索
			_clickSearch() {
				this.postData = {};
				for (let key in this.searchObj) {
					let val = this.searchObj[key];
					if (val != null && val != "") this.postData[key] = val;
				}
				this._getPageList(true);
			},
			//检索-重置
			_clickReset() {
				this.searchObj = {};
				this._clickSearch();
			}
		}
	}
</script>
