<template>
	<div class="form-submit">
		<div class="i-layout-page-header">
			<PageHeader class="product_tabs" hidden-breadcrumb>
				<div slot="title">
					<router-link :to="{ path: `${roterPre}/erp/godown_list` }">
						<!-- <Button icon="ios-arrow-back" size="small" class="mr20"
              >返回</Button
            > -->
						<div class="font-sm after-line">
							<span class="iconfont iconfanhui"></span>
							<span class="pl10">返回</span>
						</div>
					</router-link>
					<span v-text="
              $route.params.id !== '0' ? '编辑仓库' : '添加仓库'
            " class="mr20 ml16"></span>
				</div>
			</PageHeader>
		</div>
		<Card :bordered="false" dis-hover class="ivu-mt">
			<Form class="formValidate mt20" ref="formValidate" :rules="ruleValidate" :model="formValidate"
				:label-width="labelWidth" :label-position="labelPosition" @submit.native.prevent>
				<Row :gutter="24" type="flex">
					<Col span="24">
					<FormItem label="仓库名称：" prop="title">
						<Input v-model="formValidate.title" placeholder="请输入仓库名称" v-width="'400'" />
					</FormItem>
					</Col>
					<Col span="24">
					<FormItem label="上级仓库：" prop="pid">
						<el-cascader placeholder="请选择上级仓库" class="input-add" size="mini" v-model="formValidate.pid"
							:options="data1" :props="props" @change="userSearchs" filterable clearable v-width="'400'">
						</el-cascader>
					</FormItem>
					</Col>

					<Col span="24">
					<FormItem label="经度：" prop="latitude">
						<Input v-model="formValidate.latitude" placeholder="请输入经度" v-width="'400'" />
					</FormItem>
					</Col>
					<Col span="24">
					<FormItem label="纬度：" prop="longitude">
						<Input v-model="formValidate.longitude" placeholder="请输入纬度" v-width="'400'" />
					</FormItem>
					</Col>
					<Col span="24">
					<FormItem label="省市区地址：" label-for="address" prop="address">
						<Cascader :data="addresData" :load-data="loadData" v-model="formValidate.addressSelect"
							@on-change="addchack" class="inputW" v-width="'400'"></Cascader>
					</FormItem>
					</Col>
					<Col span="24">
					<FormItem label="详细地址：" prop="detailed_address">
						<Input v-model="formValidate.detailed_address" placeholder="请输入详细地址" v-width="'400'" />
					</FormItem>
					</Col>
					<Col span="24">
					<FormItem label="手机号：" prop="phone">
						<Input v-model="formValidate.phone" placeholder="请输入手机号" v-width="'400'" />
					</FormItem>
					</Col>
					<Col span="24">
					<FormItem label="是否自营：">
					  <i-switch
					    v-model="formValidate.self_support"
					    :true-value="1"
					    :false-value="0"
					    size="large"
					  >
					    <span slot="open">是</span>
					    <span slot="close">否</span>
					  </i-switch>
					</FormItem>
					</Col>
				</Row>
			</Form>
		</Card>
		<Card :bordered="false" dis-hover class="fixed-card"
			:style="{ left: `${!menuCollapse ? '200px' : isMobile ? '0' : '80px'}` }">
			<Form>
				<FormItem>
					<Button type="primary" class="submission" @click="handleSubmit('formValidate')">保存</Button>
				</FormItem>
			</Form>
		</Card>
	</div>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import {
		getGodownList,
		createGodown,
		getGodownInfo
	} from "@/api/erp";
	import {
		keyApi,
		storeGetInfoApi,
		cityApi,
		storeUpdateApi,
		cascaderList
	} from '@/api/store';
	import {
		productSpecs,
		productSpecsInfo
	} from "@/api/product";
	import Setting from "@/setting";
	export default {
		name: "specsAdd",
		data() {
			return {
				addresData: [],
				props: {
					emitPath: false,
					multiple: false,
					checkStrictly: true,
					value: "id",
					label: "title"
				},
				roterPre: Setting.roterPre,
				data1: [],
				grid: {
					xl: 7,
					lg: 7,
					md: 12,
					sm: 24,
					xs: 24,
				},
				formValidate: {
					id: 0,
					title: "",
					pid:0,
					province: "",
					city: "",
					area: "",
					detailed_address: "",
					phone: "",
					self_support: 0,
					latitude: "",
					longitude: "",
					addressSelect: [],
					address: ""
				},
				ruleValidate: {
					title: [{
						required: true,
						message: "请输入仓库名称",
						trigger: "blur"
					}, ],
					latitude: [{
						required: true,
						message: "请输入经度",
						trigger: "blur"
					}, ],
					longitude: [{
						required: true,
						message: "请输入纬度",
						trigger: "blur"
					}, ],
					detailed_address: [{
						required: true,
						message: "请输入详细地址",
						trigger: "blur"
					}, ],
					address: [{
						required: true,
						message: '请选择省市区地址',
						trigger: 'blur',
					}, ],
					phone: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur',
					}, ],
				},
			};
		},
		computed: {
			...mapState("admin/layout", ["isMobile", "menuCollapse"]),
			labelWidth() {
				return this.isMobile ? undefined : 120;
			},
			labelPosition() {
				return this.isMobile ? "top" : "right";
			},
		},
		created() {
			this.getGodownList()
			let data = {
				pid: 0
			}
			this.cityInfo(data);
		},
		mounted() {
			// this.setCopyrightShow({
			// 	value: false
			// });
			if(this.$route.params.id != 0) {
				this.getGodownInfo(this.$route.params.id)
			}
		},
		destroyed() {
			// this.setCopyrightShow({
			// 	value: true
			// });
		},
		methods: {
			...mapMutations("admin/layout", ["setCopyrightShow"]),
			getGodownInfo(id) {
				getGodownInfo(id).then(res => {
					this.formValidate = res.data.info[0]
					this.formValidate.addressSelect = this.formValidate.addressSelect.map(item => item*1)
					console.log(this.formValidate.addressSelect);
				})
			},
			addchack(e, selectedData) {
				console.log(selectedData,'selectedData');
				e.forEach((i, index) => {
					if (index == 0) {
						this.formValidate.province = i
					} else if (index == 1) {
						this.formValidate.city = i
					} else if (index == 2) {
						this.formValidate.area = i
					} else {
						this.formValidate.street = i
					}
				})
				this.formValidate.address = (selectedData.map(o => o.label)).join('/')
				// console.log('this.formValidate.addressSelect',this.formValidate.addressSelect);
				
			},
			cityInfo(data) {
				cityApi(data).then(res => {
					this.addresData = res.data
				})
			},
			loadData(item, callback) {
				item.loading = true;
					cityApi({
						pid: item.value
					}).then(res => {
						item.children = res.data;
						item.loading = false;
						callback();
					});
				

			},
			getGodownList() {
				getGodownList().then(res => {
					console.log(res);
					this.data1 = res.data.data
				})
			},
			del(index) {
				this.data.splice(index, 1);
			},
			userSearchs(e) {

			},
			handleSubmit(name) {
				this.$refs[name].validate((valid) => {
					if (valid) {
						this.formValidate.id = this.$route.params.id;
						createGodown(this.formValidate.id,this.formValidate).then(res => {
							this.$Message.success(res.msg);
									this.$router.push({
										path: this.roterPre + "/erp/godown_list"
									});
						}).catch(err => {
							this.$Message.error(err.msg);
						})
						// productSpecs(this.formValidate)
						// 	.then((res) => {
						// 		this.$Message.success(res.msg);
						// 		this.$router.push({
						// 			path: this.roterPre + "/erp/godown_list"
						// 		});
						// 	})
						// 	.catch((err) => {
						// 		this.$Message.error(err.msg);
						// 	});
						console.log('this.formValidate:',this.formValidate);
					} else {
						// this.$Message.error("请输入参数模板名称");
					}
				});
			},
		},
	};
</script>

<style scoped lang="stylus">
	.table {
		/deep/.ivu-table-header table {
			border: 0 !important;
		}

		/deep/.ivu-table-header thead tr th:nth-of-type(1) {
			padding-left: 16px;
		}

		/deep/.ivu-table td:nth-of-type(1) {
			padding-left: 16px;
		}

		/deep/.ivu-table-cell {
			padding: 0 !important;
		}

		/deep/.ivu-table-border th,
		/deep/.ivu-table-border td {
			border-right: unset;
		}

		/deep/.ivu-table td {
			height: 59px;
		}
	}

	.form-submit {
		/deep/.ivu-card {
			border-radius: 0;
		}

		margin-bottom: 79px;

		.fixed-card {
			position: fixed;
			right: 0;
			bottom: 0;
			left: 200px;
			z-index: 99;
			box-shadow: 0 -1px 2px rgb(240, 240, 240);

			/deep/ .ivu-card-body {
				padding: 15px 16px 14px;
			}

			.ivu-form-item {
				margin-bottom: 0;
			}

			/deep/ .ivu-form-item-content {
				margin-right: 124px;
				text-align: center;
			}

			.ivu-btn {
				height: 36px;
				padding: 0 20px;
			}
		}
	}

	.after-line {
		display: inline-block;
		position: relative;
		margin-right: 16px;
	}

	.ml16 {
		margin-left: 16px;
	}
</style>