<!--suppress JSAnnotator -->
<template>
<!-- Page, data-name contains page name which can be used in callbacks -->
        <div class="page " data-name="users-edit"> <!-- page-with-subnavbar -->
        <!-- Top Navbar -->
            <div class="navbar">
                <div class="navbar-inner">
                    <div class="left">
                        <a href="#" class="link back">
                            <i class="icon icon-back"></i>
                        </a>
                    </div>
                    <div class="title sliding">{{PageTitle}}</div>
                    <div class="right">
                        <!-- <a href="#" class="link">
                                                 <i class="f7-icons icon-notification"></i>
                                             </a>    -->
                    </div>


                </div>
            </div>

            <div class="bottom-section-wrapper bottom-section-button-wrapper bg-color-white">
                <div class="row">
                    <label for="submit-form-user-edit" class="col button button-fill color-green elevation-2" tabindex="0">{{@global.LANGUAGE.COM_MSG06}}</label>
                </div>
            </div>

            <!-- Scrollable page content-->
            <form name="UsersEditForm" class="page-content">
                <input type="hidden" name="Code" value="{{Code}}">
                <input type="submit" id="submit-form-user-edit" class="display-none" />

                <div class="list no-margin no-hairlines ">
                    <ul>
                        <li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"><i class="f7-icons icon-driver"></i></div>
                            <div class="item-inner">
                                <div class="item-title item-label">
                                    {{#if @global.IS_MARINE}}
                                    {{@global.LANGUAGE.CONTACT_LIST_MSG08}}
                                    {{else}}
                                    {{@global.LANGUAGE.CONTACT_LIST_MSG05}}
                                    {{/if}}
                                    ({{@global.LANGUAGE.COM_MSG129}})</div>
                                <div class="item-input-wrap ">
                                    <input type="text" name="Number" value="{{Number}}" placeholder="{{@global.LANGUAGE.CONTACT_LIST_MSG05}}" maxlength="200">
                                </div>
                            </div>
                        </li>
						
						<li>
							<label class="item-checkbox item-content">
								<input type="checkbox" name="isDriverLogin" id="isDriverLogin" />
								<div class="item-media text-color-lightgray">
								</div>
								<div class="item-inner">
									<div class="item-title">Setup Driver Login</div>
									<div class="item-after">
										<i class="icon icon-checkbox"></i>
									</div>
								</div>
							</label>
						</li>
						{{#if isDriverLogin}}
						<li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"><i class="f7-icons icon-profile-name "></i></div>
                            <div class="item-inner">
                                <div class="item-title item-label">{{@global.LANGUAGE.USER_SETTINGS_MSG02}}</div>
                                <div class="item-input-wrap ">
                                    <input type="text" name="FirstName" value="{{FirstName}}" placeholder="{{@global.LANGUAGE.USER_SETTINGS_MSG02}}" maxlength="200"  validate>
                                </div>
                            </div>
                        </li>
                        <li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"><!-- <i class="f7-icons icon-profile-name "></i> --></div>
                            <div class="item-inner">
                                <div class="item-title item-label">{{@global.LANGUAGE.USER_SETTINGS_MSG03}}</div>
                                <div class="item-input-wrap ">
                                    <input type="text" name="SubName" value="{{SubName}}" placeholder="{{@global.LANGUAGE.USER_SETTINGS_MSG03}}" maxlength="200"  validate>
                                </div>
                            </div>
                        </li>
                        <li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"><i class="f7-icons icon-email "></i></div>
                            <div class="item-inner">
                                <div class="item-title item-label">{{@global.LANGUAGE.USER_SETTINGS_MSG04}}</div>
                                <div class="item-input-wrap ">
                                    <input type="email" name="Email" value="{{EMail}}" maxlength="200" placeholder="{{@global.LANGUAGE.USER_SETTINGS_MSG04}}"  validate>
                                </div>
                            </div>
                        </li>
                        
                        <li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"><i class="f7-icons icon-mobile "></i></div>
                            <div class="item-inner">
                                <div class="item-title item-label">{{@global.LANGUAGE.USER_SETTINGS_MSG14}}({{@global.LANGUAGE.COM_MSG129}})</div>
                                <div class="item-input-wrap ">
                                    <input type="tel" name="Mobile" value="{{Mobile}}" maxlength="200" placeholder="Enter mobile number" pattern="[0-9]*" data-error-message="Only numbers please!">
                                </div>
                            </div>
                        </li>
                        <li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"><i class="f7-icons icon-mobile "></i></div>
                            <div class="item-inner">
                                <div class="item-title item-label">{{@global.LANGUAGE.USER_SETTINGS_MSG05}}({{@global.LANGUAGE.COM_MSG129}})</div>
                                <div class="item-input-wrap ">
                                    <input type="tel" name="Phone" value="{{Phone}}" maxlength="200" placeholder="Enter phone number" pattern="[0-9]*" data-error-message="Only numbers please!">
                                </div>
                            </div>
                        </li>

						
						<li class="item-content item-input ">
                            <div class="item-media text-color-lightgray"></div>
                            <div class="item-inner">
                                <div class="item-title item-label">{{@global.LANGUAGE.LOGIN_MSG02}}</div>
                                <div class="item-input-wrap ">
                                    <input type="text" name="Lgn" value="{{LoginName}}" maxlength="200" placeholder="{{@global.LANGUAGE.LOGIN_MSG02}}"  validate>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="item-content item-input">
                                <div class="item-media text-color-lightgray">
                                    <i class="f7-icons icon-password"></i>
                                </div>
                                <div class="item-inner">
                                    <div class="item-title item-label">{{@global.LANGUAGE.USERS_LIST_MSG09}}</div>
                                    <div class="item-input-wrap">
                                        <input type="text" name="Pwd" class="padding-right-36" placeholder="{{@global.LANGUAGE.USERS_LIST_MSG09}}" value="{{Password}}"  validate maxlength="200" minlength="6"  >
                                        <span class="password-toggle text-color-gray"></span>
                                    </div>
                                </div>
                            </div>
                        </li>
						{{/if}}

                    </ul>
                </div>

            </form>
        </div>
</template>


<script>
  // script must return component object
    return {
        data: function () {
            var self = this;

            var ret = {};

            if (self.$route.params) {
                if (self.$route.params.code) {
                    if (self.$route.params.code == '000000') {
                        ret = {
                            PageTitle: LANGUAGE.CONTACT_LIST_MSG04,
                            Code: '000000',
							isDriverLogin: false,
                        }
                    }else{
                        var contactList = self.$app.methods.getFromStorage('contactList');
                        var user = contactList.find(obj => obj.Code == self.$route.params.code);

                        ret = user;
                        ret.PageTitle = Template7.global.IS_MARINE ? LANGUAGE.CONTACT_LIST_MSG07 : LANGUAGE.CONTACT_LIST_MSG03;
						ret.isDriverLogin = user?.Number?.length ? true : false
                    }

                }
            }
            //console.log(ret);
			
            return ret;
        },
        methods: {



            saveUser: function(data){
                var self = this;

                var url = API_URL.CONTACT_USER_ADD;
                if (self.Code != '000000') {
                    url = API_URL.CONTACT_USER_EDIT;
                }

                self.$app.preloader.show();
                self.$app.request.post(url, data, function (result, xhr, status) {

                    if (result.MajorCode == '000') {
                       self.getContactList(data.MajorToken);
                    }else if(result.MajorCode == '100' && result.MinorCode == '1005'){

                        self.$app.preloader.hide();
                        self.$app.methods.customDialog({title: data.EMail, text: LANGUAGE.PROMPT_MSG043});

                    }else{

                        self.$app.preloader.hide();
                        self.$app.dialog.alert('Something wrong, try again later');


                    }

                },
                function (xhr, status) {
                    self.$app.preloader.hide();
                    self.$app.dialog.alert('Error occured at Save User ');
                },
                'json');
            },
            getContactList: function(MajorToken){
                var self = this;

                self.$app.request.post(API_URL.GET_CONTACT_USERS_LIST, {MajorToken}, function (result, xhr, status) {

                    self.$app.preloader.hide();
                    /*if (result.MajorCode == '000') {

                       leftView.router.back();
                    }else{
                        self.$app.dialog.alert('Something wrong, try again later');
                    }*/
                    if (result && result.length) {
                        self.$app.methods.setInStorage({name:'contactList', data:result });
                        leftView.router.back();
                    }

                },
                function (xhr, status) {
                    self.$app.preloader.hide();
                    self.$app.dialog.alert('Error occured at Get Group List ');
                },
                'json');
            },

        },

        on: {
            pageInit: function (e, page) {
                var self = this;

                var form = page.$el.find('[name = "UsersEditForm"]');
                $$(form).on('submit', function(e){
                    e.preventDefault();

                    var data = {
                        MajorToken: self.$app.methods.getFromStorage('userInfo').MajorToken,

                        FirstName: page.$el.find('input[name="FirstName"]').val(),
                        SubName: page.$el.find('input[name="SubName"]').val(),
                        EMail: page.$el.find('input[name="Email"]').val(),
                        Mobile: page.$el.find('input[name="Mobile"]').val(),
                        Phone: page.$el.find('input[name="Phone"]').val(),
                        Number: page.$el.find('input[name="Number"]').val(),
                    };
					
					if(self.isDriverLogin) {
						data.LoginName = page.$el.find('input[name="Lgn"]').val() ? page.$el.find('input[name="Lgn"]').val() : page.$el.find('input[name="Email"]').val()
						data.Password = page.$el.find('input[name="Pwd"]').val()
					}
                    if (self.Code != '000000') {
                        data.MinorToken =  page.$el.find('input[name="Code"]').val();
                    }

                    self.saveUser(data);

                    return false;
                });
				
				$$(page.$el).on('change', 'input[name="isDriverLogin"]', function (el) {
						self.$setState({
							isDriverLogin: document.getElementById("isDriverLogin").checked,
						});
				
					/*if(page.$el.find('input[name="Number"]').val().length) { 
						self.$setState({
							isDriverLogin: true,
						});
					}else{
						self.$setState({
							isDriverLogin: false,
						});
					}*/
				})

            },
            pageAfterOut: function () {

            },

        }
    };
</script>