<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="名称" prop="expressName" ref="expressName">
				<el-input v-model="form.expressName" placeholder="请输入名称"></el-input>
			</el-form-item>
			<el-form-item label="编码" prop="code" ref="code">
				<el-input v-model="form.code" placeholder="请输入编码"></el-input>
			</el-form-item>
			<el-form-item label="联系人姓名" prop="contactsName" ref="contactsName">
				<el-input v-model="form.contactsName" placeholder="请输入联系人姓名"></el-input>
			</el-form-item>
			<el-form-item label="联系人手机号" prop="contactsPhone" ref="contactsPhone">
				<el-input v-model="form.contactsPhone" placeholder="请输入联系人手机号"></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort" ref="sort">
				<el-input type="number" v-model.number="form.sort" min="0" placeholder="请输入排序"></el-input>
			</el-form-item>
			<el-form-item label="状态">
				<el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="1"
					inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button class="confirm_btn" @click="submitForm('ruleForm')">确 定</el-button>
				<el-button @click="returnPage">返回列表</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import bus from '@/common/bus';
	import TreeSelect from '../../components/tree_select';
	const __goodsCategory = 'usemall-goods-category';
	const __mpPage = 'usemall-app-wxmini-page';
	const __name = 'usemall-app-category';
	export default {
		components: {
			TreeSelect
		},
		data() {
			return {
				dataId: '',
				form: {
					createBy:'',//创建人
					createTime:'',//创建时间
					expressName: '',
					updateBy:'',//更新人
					updateTime:'',//更新时间
					sort:'',//排序
					state:'',//状态0禁用1启用
					id:'',
					code: '',
					contactsName:'',
					contactsPhone: '',
					delFlag: ''
				},
				imageValue: null,
				imageStyle: {
					height: '150px',
					width: '150px'
				},
				goodsOptions: [],
				defaultProps: {
					children: 'children',
					label: 'name'
				},
				defaultCheckedKeys: [],
				urlOptions: [],
				remark: '',
				mpPageUrl: '',
				rules: {
					expressName: [{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}],
					code: [{
						required: true,
						message: '请输入编码',
						trigger: 'change'
					}],
					sort: [{
						required: true,
						message: '请输入排序',
						trigger: 'change'
					}],
					contactsName: [{
						required: true,
						message: '请输入联系人姓名',
						trigger: 'change'
					}],
					contactsPhone: [{
						required: true,
						message: '请输入联系人手机号',
						trigger: 'change'
					}],
				},
				now_date:'',//当前时间
				req: {
					page: 1,
					rows: 30,
					orderby: 'sort asc',
				},
			};
		},
		methods: {
			loadData() {
				// this.$db[__goodsCategory].totree({
				// 	orderby: 'sort asc',
				// 	startWith: 'pid == ""'
				// }).then(res => {
				// 	if (res.code == 200) {
				// 		this.goodsOptions = res.datas;
				// 	}
				// });

				// this.$db[__mpPage].tolist().then(res => {
				// 	if (res.code == 200) {
				// 		res.datas.forEach(data => {
				// 			this.urlOptions.push(data);
				// 		});
				// 	}
				// });

				if (!this.dataId) {
					return;
				}
				this.$axios.get("/expressCompany/open/list",
				{
					params:{
						'curPage':this.req.page,
						'pageSize':this.req.rows,
						'order':'asc',
						'orderField':'sort'
					},
					headers:{
					}
				}).then(response => {
					let res = response
					if (res.success) {
						console.log(res)
						var arr=[]
						arr = res.data.list;
						for(var i=0;i<arr.length;i++){
							if(arr[i].id==this.$route.query.id){
								this.form=arr[i]
							}
						}
					} else {
					}
				}).catch(res =>{
				});	
			},
			submitForm(formName) {
				this.getNowDate()
				var _self=this
				_self.form.cid = _self.form.cid || '';
				_self.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					_self.$api.set_unvalidated_form_focus(_self, obj);
					if (valid) {
						if (!_self.dataId) {//添加
							_self.$axios.post("/expressCompany/admin/add",
							{
								'createBy':_self.form.createBy,//创建人
								'createTime':_self.form.createTime,//创建时间
								"expressName": _self.form.expressName,
								'updateBy':_self.form.updateBy,//更新人
								'updateTime':_self.form.updateTime,//更新时间
								'sort':_self.form.sort,//排序
								'state':_self.form.state,//状态0禁用1启用
								'id':_self.form.id,
								"code": _self.form.code,
								"contactsName": _self.form.contactsName,
								"contactsPhone": _self.form.contactsPhone,
								"delFlag": _self.form.delFlag
							},
							{
								headers:{
									'Mall-Token': uni.getStorageSync("token")
								}
							}).then(response => {
								let res = response
								if (res.success) {
									_self.getOpenerEventChannel().emit('loadData');
									_self.returnPage();
								} else {
									alert(res.message)
								}
							})
							// _self.$db[__name].add(_self.form).then(res => {
							// 	if (res.code == 200) {
							// 		_self.getOpenerEventChannel().emit('loadData');
							// 		_self.returnPage();
							// 	}
							// });
						} else {
							_self.$axios.put("/expressCompany/admin/update",
							{
								'createBy':_self.form.createBy,//创建人
								'createTime':_self.form.createTime,//创建时间
								"expressName": _self.form.expressName,
								'updateBy':_self.form.updateBy,//更新人
								'updateTime':_self.form.updateTime,//更新时间
								'sort':_self.form.sort,//排序
								'state':_self.form.state,//状态0禁用1启用
								'id':_self.form.id,
								"code": _self.form.code,
								"contactsName": _self.form.contactsName,
								"contactsPhone": _self.form.contactsPhone,
								"delFlag": _self.form.delFlag
							},
							{
								headers:{
									'Mall-Token': uni.getStorageSync("token")
								}
							}).then(response => {
								let res = response
								if (res.success) {
									this.getOpenerEventChannel().emit('loadData');
									this.returnPage();
								} else {
									alert(res.message)
								}
							})
							// this.$db[__name].update(this.dataId, this.form).then(res => {
							// 	if (res.code == 200) {
							// 		this.getOpenerEventChannel().emit('loadData');
							// 		this.returnPage();
							// 	}
							// });
						}
					}
				});
			},
			//获取当前时间
			getNowDate() {
			  var _this = this;
			  // this.timer = setInterval(function() {
				var aData = new Date();
				var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
				var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
				var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
				var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
				var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
				var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
				// console.log(aData.getTime())
				_this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
				// console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
			  // }, 86400000);
			},
			returnPage() {
				bus.$emit('tab-close');
				this.$router.push('/pages/order/kuaidi');
			},
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		},
	};
</script>

<style>
	.el-tree.common-tree.el-tree--highlight-current {
		width: initial !important;
	}
</style>

<style></style>
