<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";
	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否）
				},
				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 res =  await addNewStore(this.fromdata);
				if(res.code==0){
					this.$api.msg(res.msg);
				}else if(res.code==-1){
					this.$api.msg("暂时不允许修改");
				}else{
					this.$api.msg(res.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;
					}
				});
			},
		}
	}
</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>
