<template>
	<div class="my-container">
		<transition-group name="fade" style="position: relative;width: 100%;display: flex;">
			<div class="bill-main" v-show="isShow==='ckb'" key="item">
				<module-view :propConfig="config" ref="moduleView" @pagination="getDataList" @search="getDataList"
					@resert="getDataList" @detail="openEdit" @onRefresh="onRefresh">
				</module-view>
				<!-- 注册/编辑弹窗 -->
				<el-dialog :title="popTitle+'仓库'" :visible.sync="dialogFormVisible" width="30%">
					<by-form :propConfig="addConfig" ref="addFormId"></by-form>
					<div slot="footer" class="dialog-footer">
						<el-button @click="dialogFormVisible = false">取 消</el-button>
						<el-button type="primary" @click="confirm">确 定</el-button>
					</div>
				</el-dialog>
			</div>
			<div class="bill-main" v-show="isShow==='cwb'" key="item2">
				<module-view :propConfig="config2" ref="moduleView2" @clickHandle="clickHandle2" @detail="openEdit2"
					@pagination="getDataList2" @search="getDataList2" @resert="getDataList2" @onRefresh="onRefresh2">
				</module-view>
				<!-- 注册/编辑弹窗 -->
				<el-dialog :title="popTitle+'仓位'" :visible.sync="dialogFormVisible2" width="30%">
					<by-form :propConfig="addConfig2" ref="addFormId2">
						<template v-slot:organizationName="{value}">
							<el-input placeholder="请选择所属机构名称" size="small" v-model="value.organizationName"
								class="input-organizationName" :readonly="true">
								<el-button slot="append" icon="el-icon-more" @click="handleSupplier"></el-button>
							</el-input>
						</template>
						<template v-slot:stock="{value}">
							<vxe-input v-if="isAdd" v-model="value.stock" style="width: 100%;" size="small" type="text"></vxe-input>
							<span v-else>{{ value.stock }}</span>
						</template>
					</by-form>
					<div slot="footer" class="dialog-footer">
						<el-button @click="dialogFormVisible2 = false">取 消</el-button>
						<el-button type="primary" @click="confirm2">确 定</el-button>
					</div>
				</el-dialog>
			</div>
		</transition-group>
		<supplier-modal ref="supplierModal" @confirm="confirmSupplier" :zIndex="9999" />
	</div>
</template>
<script lang="ts">
	import { Component, Prop, Vue, Watch } from "vue-property-decorator";
	import api from "@/api/currency";
	@Component
	export default class Depots extends Vue {
		isShow : any = 'ckb'
		timeNum = 0;
		timer : any = null
		timeNum2 = 0;
		timer2 : any = null
		popTitle : any = ''
		tableData : any = {}
		dialogFormVisible : boolean = false
		dialogFormVisible2 : boolean = false
		isAdd : any = false
		config : any = {
			attr:{
				calculateH:true
			},
			search: {
				attr: {
					size: 'mini'
				},
				columns: [
					[{
						span: 6,
						label: '仓库名称',
						prop: 'name',
						component: 'by-input',
						labelWidth: '70px',
						compConfig: {
							attr: {
								placeholder: '请输入名称',
								clearable: true
							},
						},
					}, {
						span: 6,
						label: '地址',
						prop: 'address',
						component: 'by-input',
						labelWidth: '70px',
						compConfig: {
							attr: {
								placeholder: '请输入地址',
								clearable: true
							}
						}
					},
					]
				]
			},
			tool: {
				tools: {
					search: true,
					refresh: true
				},
				customTools: [
					{
						name: '注册', icon: 'el-icon-plus', audit: [''], event: {
							click: () => {
								(this as any).onAdd();
							}
						}
					}
				]
			},
			table: {
				attr: {
					size: 'mini',
					align: 'left',
				},
				columns: [{
					title: '名称',
					field: 'name',
					isDetail: true,
				}, 
				{
					title: '第三方系统名称',
					field: 'financeType',
				},
				{
					title: '第三方仓库编码',
					field: 'financeStorehouseNo',
				},
				{
					title: '第三方仓库名称',
					field: 'financeStorehouseName',
				},
				{
					title: '地址',
					field: 'address',
				}, 
				{
					width: 100,
					title: '仓位',
					action: true,
					plugins: [{
						icon: 'el-icon-edit',
						name: '查看',
						audit: '',
						event: {
							click: (item : any) => {
								(this as any).changeTab(item)
							}
						}
					}]
				}, {
					width: 120,
					title: '操作',
					action: true,
					plugins: [{
						icon: 'el-icon-edit',
						name: '编辑',
						audit: '',
						event: {
							click: (item : any) => {
								(this as any).openEdit(item)
							}
						}
					}, {
						name: '删除',
						event: {
							click: (item : any) => (this as any).doDelete2(item, 'maindataStorehouse')
						}
					}]
				}]
			},
		}
		config2 : any = {
			attr:{
				calculateH:true
			},
			search: {
				attr: {
					size: 'mini'
				},
				columns: [
					[
						{
							span: 6,
							label: '名称',
							prop: 'name',
							component: 'by-input',
							compConfig: {
								attr: {
									placeholder: '请输入名称',
									clearable: true
								},
							},
						},
						{
							span: 6,
							label: '所属机构名称',
							prop: 'organizationName',
							component: 'by-input',
							compConfig: {
								attr: {
									placeholder: '请输入所属机构名称',
									clearable: true
								}
							}
						},
						{
							span: 6,
							label: '库存',
							prop: 'stock',
							component: 'by-input',
							compConfig: {
								attr: {
									placeholder: '请输入库存',
									clearable: true
								},
							},
						},
						{
							span: 6,
							label: '库存体积',
							prop: 'stockVolume',
							component: 'by-input',
							compConfig: {
								attr: {
									placeholder: '请输入库存体积',
									clearable: true
								},
							},
						},
					],
					[
						{
							span: 6,
							label: '库存上限数量',
							prop: 'maxStockNumber',
							component: 'by-input',
							compConfig: {
								attr: {
									placeholder: '请输入库存上限数量',
									clearable: true
								}
							}
						},
						{
							span: 6,
							label: '库存体积上限',
							prop: 'maxStockVolume',
							component: 'by-input',
							compConfig: {
								attr: {
									placeholder: '请输入库存体积上限',
									clearable: true
								}
							}
						},
					]
				]
			},
			tool: {
				tools: {
					return: true,
					search: true,
					refresh: true
				},
				customTools: [
					{
						name: '注册', icon: 'el-icon-plus', audit: [''], event: {
							click: () => {
								(this as any).onAdd2();
							}
						}
					},
					{
						name: '删除', icon: 'el-icon-delete', audit: [''], event: {
							click: () => {
								(this as any).onDelete('moduleView2', 'maindataStoringLocation')
							}
						}
					},
				]
			},
			table: {
				attr: {
					size: 'mini',
					seq: true,
					align: 'left',
					checkbox: true,
				},
				columns: [{
					width: 300,
					title: '仓位名称',
					field: 'name',
					isDetail: true,
				},
				{
					width: 250,
					title: '仓库名称',
					field: 'ckName',
				},
				{
					width: 200,
					title: '所属机构名称',
					field: 'organizationName',
				},
				{
					width: 150,
					title: '库存',
					field: 'stock'
				},
				{
					width: 150,
					title: '库存体积',
					field: 'stockVolume'
				},
				{
					width: 150,
					title: '库存数量上限',
					field: 'maxStockNumber'
				},
				{
					width: 150,
					title: '库存体积上限',
					field: 'maxStockVolume'
				}, {
					width: 120,
					title: '操作',
					action: true,
					plugins: [{
						icon: 'el-icon-edit',
						name: '编辑',
						audit: '',
						event: {
							click: (item : any) => {
								(this as any).openEdit2(item)
							}
						}
					}, {
						name: '删除',
						event: {
							click: (item : any) => (this as any).doDelete3(item, 'maindataStoringLocation')
						}
					}]
				}]
			},
		}
		addConfig = {
			attr: {
				size: 'small',
				rules: {
					name: [{
						required: true, message: '请输入名称', trigger: 'blur'
					}],
					address: [{
						required: true, message: '请输入地址', trigger: 'blur'
					}]
				}
			},
			columns: [
				[
					{
						span: 23,
						labelWidth: '110px',
						label: '名称',
						prop: 'name',
						component: 'by-input',
					},
					{
						span: 23,
						labelWidth: '110px',
						label: '第三方系统名称',
						prop: 'financeType',
						component: 'by-input',
					},
					{
						span: 23,
						labelWidth: '110px',
						label: '第三方仓库编码',
						prop: 'financeStorehouseNo',
						component: 'by-input',
					},
					{
						span: 23,
						labelWidth: '110px',
						label: '第三方仓库名称',
						prop: 'financeStorehouseName',
						component: 'by-input',
					},
					{
						span: 23,
						labelWidth: '110px',
						label: '地址',
						prop: 'address',
						component: 'by-input',
					},
				]
			]
		}
		addConfig2 = {
			attr: {
				size: 'small',
				rules: {
					name: [{
						required: true, message: '请输入名称', trigger: 'blur'
					}],
					organizationName: [{
						required: true, message: '请输入所属机构名称', trigger: 'blur'
					}],
					stock: [{
						required: true, message: '请输入库存', trigger: 'blur'
					}]
				}
			},
			columns: [
				[
					{
						labelWidth: '110px',
						span: 23,
						label: '名称',
						prop: 'name',
						component: 'by-input',
					},
					{
						labelWidth: '110px',
						span: 23,
						label: '所属机构名称',
						prop: 'organizationName',
						slot: true,
					},
					{
						labelWidth: '110px',
						span: 23,
						label: '库存',
						prop: 'stock',
						slot: true
					},
					{
						labelWidth: '110px',
						span: 23,
						label: '库存体积',
						prop: 'stockVolume',
						component: 'by-input',
					},
					{
						labelWidth: '110px',
						span: 23,
						label: '库存数量上限',
						prop: 'maxStockNumber',
						component: 'by-input',
					},
					{
						labelWidth: '110px',
						span: 23,
						label: '库存体积上限',
						prop: 'maxStockVolume',
						component: 'by-input',
					},
				]
			]
		}
		created() {
			this.timer = setInterval(() => {
				this.getDataList()
			}, 500)
		}
		// 仓库确认注册/编辑
		confirm() {
			(this as any).$refs.addFormId.validate().then(() => {
				let query = (this as any).$refs.addFormId.getValue();
				this.dialogFormVisible = false;
				if (this.popTitle === '注册') {
					let loading = this.$loading({ target: '.main-container' });
					api.saveList(query, 'maindataStorehouse').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success(this.popTitle + '成功');
							this.getDataList();
							this.$confirm('是否注册仓位', '提示', {
								confirmButtonText: '确定',
								cancelButtonText: '取消',
							}).then(() => {
								api.single({ id: res.data }, 'maindataStorehouse').then((data : any) => {
									this.changeTab(data.data);
									this.onAdd2();
								})
							}).catch(() => { });
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				} else if (this.popTitle === '编辑') {
					let loading = this.$loading({ target: '.main-container' });
					api.updateList(query, 'maindataStorehouse').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success(this.popTitle + '成功');
							this.getDataList();
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				}
			})
		}
		// 仓位确认注册/编辑
		confirm2() {
			(this as any).$refs.addFormId2.validate().then(() => {
				let query = (this as any).$refs.addFormId2.getValue();
				console.log('表单参数 ==> ', query);
				query.storehouseId = this.tableData.id;
				query.idDelete = 0;
				this.dialogFormVisible2 = false;
				if (this.popTitle === '注册') {
					let loading = this.$loading({ target: '.main-container' });
					api.saveList(query, 'maindataStoringLocation').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success(this.popTitle + '成功');
							this.getOneself(query.storehouseId);
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				} else if (this.popTitle === '编辑') {
					let loading = this.$loading({ target: '.main-container' });
					api.updateList(query, 'maindataStoringLocation').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success(this.popTitle + '成功');
							this.getOneself(query.storehouseId);
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				}
			})
		}
		// 通过id拿到当条数据
		getOneself(e : any) {
			api.single({ id: e }, 'maindataStorehouse').then((res : any) => {
				if (res.code === 200) {
					this.tableData = res.data;
					this.tableData.subList.map((v : any) => {
						v.ckName = this.tableData.name;
					});
					(this.$refs.moduleView2 as any).setTableValue(this.tableData.subList);
				}
			})
		}
		// 获取仓库列表数据
		getDataList() {
			if (!this.$refs.moduleView) {
				if (this.timeNum > 5) {
					clearInterval(this.timer)
				}
				this.timeNum++;
				return
			}
			clearInterval(this.timer)
			let query = (this.$refs.moduleView as any).getQuery();
			let loading = this.$loading({ target: '.main-container' });
			api.pageList(query, 'maindataStorehouse').then((res : any) => {
				loading.close();
				if (res.code === 200) {
					(this.$refs.moduleView as any).setTableValue(res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.moduleView as any).setPage(page)
				} else this.$message.error(res.msg)
			}).catch(() => loading.close());
		}
		// 获取仓位列表数据
		getDataList2() {
			if (!this.$refs.moduleView2) {
				if (this.timeNum2 > 5) {
					clearInterval(this.timer2)
				}
				this.timeNum2++;
				return
			}
			clearInterval(this.timer2)
			let query = (this.$refs.moduleView2 as any).getQuery();
			console.log(query);
			query.storehouseId = this.tableData.id;
			let loading = this.$loading({ target: '.main-container' });
			api.pageList(query, 'maindataStoringLocation').then((res : any) => {
				loading.close();
				if (res.code === 200) {
					res.data.records.map((v : any) => {
						v.ckName = this.tableData.name;
					});
					(this.$refs.moduleView2 as any).setTableValue(res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.moduleView2 as any).setPage(page)
				} else this.$message.error(res.msg)
			}).catch(() => loading.close());
		}
		// 工具栏方法
		clickHandle2(e : any) {
			if (e === 'onAdd') this.onAdd2();
			if (e === 'onReturn') this.isShow = 'ckb';
		}
		// 打开注册
		onAdd() {
			this.popTitle = '注册'
			this.dialogFormVisible = true;
			setTimeout(() => {
				if ((this as any).$refs.addFormId) (this as any).$refs.addFormId.setValue({});
			}, 0)
		}
		// 打开编辑
		openEdit(e : any) {
			this.popTitle = '编辑'
			this.dialogFormVisible = true;
			setTimeout(() => {
				if ((this as any).$refs.addFormId) (this as any).$refs.addFormId.setValue(e);
			}, 0)
		}
		// 打开注册
		onAdd2() {
			this.popTitle = '注册'
			this.dialogFormVisible2 = true;
			this.isAdd = true;
			setTimeout(() => {
				if ((this as any).$refs.addFormId2) (this as any).$refs.addFormId2.setValue({});
			}, 0)
		}
		// 打开编辑
		openEdit2(e : any) {
			this.popTitle = '编辑'
			this.dialogFormVisible2 = true;
			this.isAdd = false;
			console.log(e);
			setTimeout(() => {
				if ((this as any).$refs.addFormId2) (this as any).$refs.addFormId2.setValue(e);
			}, 0)
		}
		// 打开机构选择
		handleSupplier() {
			(this.$refs.supplierModal as any).setShow(true);
		}
		// 确定机构选择
		confirmSupplier(e : any) {
			if (e.length == 0) return this.$message.warning('请选择供应商！');
			let value : any = (this.$refs.addFormId2 as any).getValue();
			value.organizationName = e[0].name;
			value.organizationId = e[0].id;
			(this.$refs.addFormId2 as any).setValue(value);
		}
		// 查看仓位
		changeTab(item : any) {
			api.single({id:item.id},'maindataStorehouse').then((res:any) => {
				if(res.code===200){
					this.tableData = res.data;
					res.data.subList.map((v : any) => {
						v.ckName = res.data.name;
					})
					console.log('仓库表数据 ==> ', res.data);
					(this.$refs.moduleView2 as any).setTableValue(res.data.subList);
					this.isShow = 'cwb';
				}
			})
		}
		// 刷新仓库
		onRefresh() {
			(this as any).$refs.moduleView.clearSearch();
			this.getDataList();
		}
		// 刷新仓位
		onRefresh2() {
			(this as any).$refs.moduleView2.clearSearch();
			this.getDataList2();
		}
		// 仓库操作删除
		doDelete2(item : any, url : any) {
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				if (item.subList.length > 0) {
					this.$message.warning('此仓库下有仓位，不可删除！');
				} else {
					let loading = this.$loading({ target: '.main-container' });
					api.deleteList({ ids: item.id }, url).then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.getDataList();
							this.$message.success('删除成功');
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				}
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 仓位工具栏删除
		onDelete(moduleView : any, url : any) {
			let selectData = (this.$refs[moduleView] as any).getSelectData()
			let ids = '';
			if (selectData.length > 0) {
				selectData.map((v : any) => {
					ids += v.id + ','
				})
			} else return this.$message.warning('请选择删除数据');
			ids = ids.slice(0, ids.length - 1);
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.deleteList({ ids: ids }, url).then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.getDataList2();
						this.$message.success('删除成功');
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 仓位操作删除
		doDelete3(item : any, url : any) {
			console.log('仓位操作删除:', item, url);
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.deleteList({ ids: item.id }, url).then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.getDataList2();
						this.$message.success('删除成功');
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消删除'));
		}
	}
</script>
<style lang="scss" scoped>
	.my-container {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		padding: 0 16px;
		height: 100%;
		.search-btn {
			width: 100%;
			display: flex;
			justify-content: flex-end;
			margin-bottom: 20px;
		}

		.bill-main {
			width: calc(100% - 16px);
			box-sizing: border-box;
			margin-left: 16px;
			height: 100%;
		}

		@media screen and (min-width: 1500px) {
			.bill-main {
				min-width: 1610px;
			}
		}
	}
</style>