<template>
	<!-- 店铺信息 编辑修改页面 设置店铺信息 -->
	<view class="shopinfoss">
		
		<view class="logobox">
			<image v-if="fromdata.logo!='' && fromdata.logo!=null" @tap="uploadimgs" class="islogimg" :src="staticUrl+fromdata.logo" mode=""></image>
			<view class="islogimg" v-else  @tap="uploadimgs">上传logo</view>
		</view>
		
		<view class="biao">
			<view class="cu-form-group ">
				<view class="title">联系人：</view>
				<input class="title" style="flex-grow: 1;" placeholder="请输入联系人" v-model="fromdata.contact"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title">店铺电话：</view>
				<input class="title" style="flex-grow: 1;" placeholder="请输入店铺电话" v-model="fromdata.tel"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title">店铺地址：</view>
				<input class="title" style="flex-grow: 1;" placeholder="请输入店铺地址" v-model="fromdata.addr"></input>
			</view>
			<view class="cu-form-group" @click="biaozu">
				<view class="title">标注位置：</view>
				<input :placeholder="address" name="input" disabled></input>
				<text v-if="!address" class='cuIcon-locationfill text-orange'></text>
			</view>
			
			<view class="biao_05 cu-form-group">
				<view class="title">营业时间：</view>
				<view class="time flex" >
					<view class="biao_05_2"> 
						<view class="uni-list-cell-db flex">
							<picker mode="time" :value="time"  @change="bindTimeChange" style="margin-top: 6px;">
								<view class="uni-input">{{time}}</view>
							</picker>
						</view>
					</view>
					<view class="biao_05_2 flex" style="margin: 10upx 10px 0 0;">--</view>
					<view class="uni-list-cell-db flex">
						<picker mode="time" :value="time_close"  @change="bindTimeChange_close" style="margin-top: 6px;">
							<view class="uni-input">{{time_close}}</view>
						</picker>
					</view>
				</view>
			</view>
			<view class="cu-form-group ">
				<view class="title">介绍：</view>
			</view>
			<view class="cu-form-group">
				<textarea maxlength="-1"  v-model="fromdata.details" placeholder="请输入介绍"></textarea>
				<!-- :disabled="modalName!=null" -->
			</view>
			<view class="cu-form-group ">
				<view class="title">是否营业：</view>
				<view class="title" >
					<switch @change="SwitchAchange" :class="switchA?'checked':''"  :checked="switchA?true:false" color="#e54d42"></switch> 
				</view>
			</view>
			
		</view>
		
		
		<view class="p_btn">
			<view class=" flex flex-direction">
				<button @tap="addNewStoreFun()" class="cu-btn bg-red margin-tb-sm lg">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import uniIcon from "@/components/uni-icon/uni-icon.vue"
	import wPicker from "@/components/w-picker/w-picker.vue";
	import config from "@/utils/config.js";
	import {addNewStore,shopDetail} from "@/utils/api/api.js";
	
	import QQMapWX from "@/utils/libs/qqmap-wx-jssdk.min.js";
	var qqmapsdk = new QQMapWX({
	  key: 'KROBZ-N2ORJ-VYBFJ-F76TY-ASPMT-JFBFH'
	});
	
	
	export default {
		data() {
			return {
				content: [],
				list:[],
				address: '',
				time: '09:00',
				time_close: '19:00',
				modalName:"",//？
				switchA:false,
				
				//提交的表单
				fromdata:{
					contact	:"",//联系人
					tel:"",//电话
					lat:"",///经纬度
					lng:"",//经纬度
					details:"",//介绍
					addr:"",//地址
					business_time:"",//时间
					logo:"",//店铺logo
					closed:1,//是否营业（0是，1否）
					// city_code: "",//城市代码
					// area_code: "",//区号
				},
				
				
				//addr: "南昌市"
				//contact: ""
				//business_time: "9:00-18:00"
				//logo: ""
				//lat: 28.68503
				//lng: 115.89925
				

				//可能少的
				//address: "",//详细地址
				cate_id: 0,//分了id
				shop_name: "",//店铺名称
				makeruid: "",
				city_code: 360100,//城市代码
				area_code: 360102,//区号
				cate_name: "品牌服装",//分类名称 文字
				
				
				shopinfos:{},
				
				
			};
		},
		onLoad() {
			this.content=this.$api.json.content;
			
			//获取之前设置过的信息
			this.getshopDetail();
		},
		components: {
			uniIcon,
			wPicker
		},
		//计算属性
		computed:{
			staticUrl(){
				return this.$store.state.staticUrl;
			},
			uploaddata(){
				return {model:"shopphoto",user_token:this.$store.state.token,shop_id:this.$store.state.userInfo.shop_id}
			}
		},
		methods: {
			//获取之前设置过的信息
			async getshopDetail(){
				let res = await shopDetail({
					
				});
				if(res.code==0){
					this.shopinfos = res.data;
					
					this.fromdata.contact = this.shopinfos.contact;
					this.fromdata.tel = this.shopinfos.tel;
					this.fromdata.lat = this.shopinfos.lat;
					this.fromdata.lng = this.shopinfos.lng;
					this.fromdata.details = this.shopinfos.details;
					this.fromdata.addr = this.shopinfos.addr;
					this.fromdata.business_time = this.shopinfos.business_time;//营业时间 需要特殊处理
					this.fromdata.logo = this.shopinfos.logo;
					this.fromdata.closed = this.shopinfos.closed;//开关 需要特殊处理
					
					this.switchA = this.shopinfos.closed==0?true:false;//开关状态
					
					
					//console.info(this.fromdata.business_time.split("---"),"business_time");
					if(this.fromdata.business_time.indexOf("---")!=-1){
						let timearr = this.fromdata.business_time.split("---");
						if(timearr.length!=0){
							this.time = timearr[0];
							this.time_close=timearr[1];
						}
					}else if(this.fromdata.business_time.indexOf("--")!=-1){
						let timearr = this.fromdata.business_time.split("--");
						if(timearr.length!=0){
							this.time = timearr[0];
							this.time_close=timearr[1];
						}
					}else if(this.fromdata.business_time.indexOf("-")!=-1){
						let timearr = this.fromdata.business_time.split("-");
						if(timearr.length!=0){
							this.time = timearr[0];
							this.time_close=timearr[1];
						}
					}
					

					
					// contact	:"",//联系人
					// tel:"",//电话
					// lat:"",///经纬度
					// lng:"",//经纬度
					// details:"",//介绍
					// addr:"",//地址
					// business_time:"",//时间
					// logo:"",//店铺logo
					// closed:1,//是否营业（0是，1否）
				}else{
					this.$api.msg(res.msg);
				}
			},
			//上传图片方法
			uploadimgs(){
				uni.chooseImage({
					count:1,
				    success: (chooseImageRes) => {
				        const tempFilePaths = chooseImageRes.tempFilePaths;
				        uni.uploadFile({
				            url: config.SiteBaseUrl+'FileUpload/uploadify', //仅为示例，非真实的接口地址
				            filePath: tempFilePaths[0],
				            name: 'img',
				            formData: this.uploaddata,
				            success: (uploadFileRes) => {
				                console.log(uploadFileRes.data,"上传成功后~");
								
								let resdata = JSON.parse(uploadFileRes.data);
								console.info(resdata,"resdata");
								
								this.fromdata.logo = resdata.data;
				            }
				        });
				    }
				});
			},
			//提交修改
			 async addNewStoreFun(){
				
				this.fromdata.business_time = this.time + "---" + this.time_close;//营业时间
				
				// console.info(this.fromdata);
				// return false;
				
				let objs = {...this.shopinfos,...this.fromdata};
				 
				let res =  await addNewStore(this.fromdata);//this.fromdata
				if(res.code==0){
					this.$api.msg(res.msg);
				}else{
					this.$api.msg(res.msg);
				}
				
				
				// else if(res.code==-1){
				// 	this.$api.msg("暂时不允许修改");
				// }
			},
			textareaAInput(e){
				
			},
			SwitchAchange(e){
				console.info(e.detail.value);
				if(e.detail.value){
					this.fromdata.closed=0;//营业
				}else{
					this.fromdata.closed=1;//打烊
				}
			},
			TimeChange(e) {
				this.time = e.detail.value
			},
			bindTimeChange: function(e) {
				this.time = e.target.value
			},
			bindTimeChange_close: function(e) {
				this.time_close = e.target.value
			},



			switch1Change: function(e) {
				if (e.target.value) {
					this.list.shop_state = true //   1   checked的值为布尔值
				} else {
					this.list.shop_state = false  //   0
				} 
			},
			//选择位置
			biaozu() {
				const that = this
				
				//微信
				//#ifdef MP-WEIXIN 
				uni.getSetting({
					success(res) {
						if (!res.authSetting['scope.userLocation']) {
							uni.authorize({
								scope: 'scope.userLocation',
								success() {
									that.chooseLocation()
									return;
								}
							})
						} else {
							that.chooseLocation()
							return;
						}
					}
				})
				//#endif
				
				//不是微信
				//#ifndef MP-WEIXIN 
				that.chooseLocation()
				//#endif
			},
			//选中定位 获取经纬度
			chooseLocation() {
				const that = this
				uni.chooseLocation({
					success: (res)=> { 
						that.address = res.name
						that.list.position = res
						
						this.fromdata.lat = res.latitude;
						this.fromdata.lng = res.longitude;
						
						
						
						console.info(res);
						
						qqmapsdk.reverseGeocoder({
							location: {
								latitude: res.latitude,
								longitude: res.longitude
							},
							success: (restwo)=>{
								//debugger
								//console.log(res)
								let province = res.result.ad_info.province;
								let city = res.result.ad_info.city;
								//console.log(res,"res");
								// this.province=Number(res.result.ad_info.adcode)-Number(res.result.ad_info.adcode)%1000;//省级ID
								// this.citys=Number(res.result.ad_info.adcode)-Number(res.result.ad_info.adcode)%100;//市级ID
								// this.countys=Number(res.result.ad_info.adcode);//区县级ID
								
								// this.fromdata.city_code=Number(res.result.ad_info.adcode)-Number(res.result.ad_info.adcode)%100;//城市代码
								// this.fromdata.area_code=Number(res.result.ad_info.adcode);//区号
								
								//console.info(restwo);
							},
							fail: (err)=>{
								console.log(err)
							}
						})
						
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.logobox{
		padding: 15upx 0;
		text-align: center;
		.islogimg{
			width: 200upx;
			height: 200upx;
			line-height: 200upx;
			text-align: center;
			margin: 0 auto;
		}
	}
</style>
