<style scoped>
.comp-classpage input,.comp-classpage textarea,.comp-classpage select,.comp-classpage button{padding:0;}
hr {margin: 0;border: 0.17rem solid #e6e6e9;}
.comp-classpage{height:100%;height: 100vh;}
.Key_UpdateName{padding:5% 6%;}
.Key_UpdateName div {margin-top:0;margin-bottom:0;padding:0.6rem 0;height:2rem;line-height:2rem;display:flex;border-bottom: 1px solid #E9E9E9;}
.Key_UpdateName div span{display:inline-block;text-align:center;padding:0 0.4rem; padding-top:0.2rem;padding-left: 0;}
.Key_UpdateName div input{background-color: #fff;border:0px;flex:1;outline:none;color:#000;}
.Key_UpdateName img{width:0.9rem;height:1rem;}

.item-btn {display:inline-block;width:88%;height:2.5rem;padding:0;margin:0 6%;margin-top:0px;margin-bottom:0px;font-size:1rem;
line-height:2.5rem;text-align:center;color:#fff;
background:#3A8AFB;border:none;border-radius:0.2rem;
margin-top:1.5rem;margin-bottom:1rem; font-weight: bold;}
input[type=button],button, input[type=submit], input[type=file], button {cursor:pointer;outline:0px; -webkit-appearance:none; }

</style>
<template>
<div class="container comp-classpage jfBaseSize" v-cloak v-bind:style="vh?'height:100vh':'height: 100%;'">
    <div id="Key_UpdateName" class="Key_UpdateName">
      <div>
        <span><img src="http://image.supercarrier8.com/h5_key/icon-user-gray.png" /></span>
        <input type="text"  placeholder="请输入昵称" class="inp" name="NickName" id="NickName" v-model="NickName" autocomplete="off">
      </div>
    </div>
<button class="item-btn"  @click="saveNickname()">保存</button>
<ReturnHome ref="returnhome"></ReturnHome>
</div>
</template>
<script> 
	import $ from 'jquery'
	import channelHelper from "@/assets/js/channelHelper.js";
	import JfapiHelper from "@/assets/js/JfapiHelper.js";
	import MintUI from 'mint-ui';
	import ReturnHome from "@/components/channel/temp001/ReturnHome";
	import { Toast } from 'mint-ui';
	export default {
		name: 'Key_UpdateName',
		  components: {ReturnHome},
    provide(){       
     return{           
        goPage:this.goPage       
     }   
    },
		props: ["cfg", "comp", "selectedPage"],
		data() {
			var that = this;
			//从缓存中得到登录信息
			var loginUserInfo = null;
			var PetName="";var RoleId="";
			var tempLoginInfo = localStorage.getItem("loginInfo");
			if ($.trim(tempLoginInfo) != "") {
				loginUserInfo = eval('(' + tempLoginInfo + ')');
				PetName=loginUserInfo.PetName;
			}
			var wxUserInfo=this.sup8.context.wxUser;
			if(wxUserInfo&&PetName=="")
			{
				PetName=wxUserInfo.nickname;
			}
			return {
				loginUserInfo:loginUserInfo,
				NickName: PetName,
				vh:false,
			};
		},
		methods: {
			goPage(type){
               this.$parent.changeSelectedPage(type);
            },
			//保存姓名
			saveNickname() {
				var location=this.$parent;
				var that = this;
				var eno=that.sup8.context.enterpriseno;
				var confirmBtn = $(".item-btn");
				var changeNicknameObj = $("#NickName");
				if ($.trim(changeNicknameObj.val()) == "") {
					MintUI.Toast('请输入姓名！');
					return false;
				}
				var tempNickName = $.trim(changeNicknameObj.val());
				//判断是否登录
				if (!JfapiHelper.CheckLogin(this.$parent,eno,that.sup8.context.entranceid)) {
					return false;
				}
				console.log(that.loginUserInfo);
				//更新姓名
				var url = "/channel/ChangeUserName";
				var dataParams = {
					EnterpriseNo: eno,
					PetName: tempNickName,
					UserToken: that.loginUserInfo.UserToken,
					LanguageType:JfapiHelper.GetLanguageType()
				};
				var cbBeforeSend = function () {
					confirmBtn.val("请稍等...");
				};
				var cbComplete = function () {
					confirmBtn.val("确定");
				};
				var cbSuccess = function (resultData) {
					if ((resultData || "") != "") {
						if ((typeof resultData).toLowerCase() == "string") {
							resultData = eval('(' + resultData + ')');
						}
						if (resultData.flag && (resultData.errorCode || "") == "0") {
							//更新用户json变量中的数据
							that.loginUserInfo.PetName = tempNickName;
							MintUI.Toast('更新成功！');
							 window.history.go(-1);
							//location.changeSelectedPage("Key_USet");
						}
						else {
							MintUI.Toast(resultData.msg);
						}
					}
				};
				var cbError = function (jqXHR, textStatus, errorThrown) {
					MintUI.Toast(jqXHR.responseText);
				};
				//请求后端
				channelHelper.CommonCallServiceAuto(url, dataParams, cbSuccess, cbError, cbBeforeSend, cbComplete);
			}
		},
		created() {
		var that=this;
			var isDesign = location.pathname.indexOf('design');
			if (!(isDesign>-1)) {
				that.$set(that,'vh',true);
			}else{
				that.$set(that,'vh',false);
			}
		},
		mounted: function () { 
			
		}
	}
</script>
