<?php echo $this->profileheader($this->customer);?>



<div class="resp-tabs-container tab_identifier_parent no-padding">
    <div>
        <div class="client-contact-wrapper" id="profile-fields">
            <div class="form-wrapper">
                <form name="customerdata" id="customerdata" action="index.php?fuse=clients&controller=userprofile&action=updateprofilecontact" autocomplete="off" method="POST">
                    <input type="hidden" name="id" value="<?php echo $this->customerId;?>" />
                    <input type="hidden" name="userid" value="<?php echo $this->customerId;?>" />
                    <input type="hidden" name="packageaction" value="0" />
                    <input type="hidden" name="packageplugin" value="0" />
                    <div class="row">
                        <?php if ($this->showCustomerGroup) { ?>
                        <div class="col-sm-6">
                            <div class="form-elem">
                                <label><?php echo $this->user->lang("Client Group");?></label>
                                <select class="normalSelect2" id="dropdown_customerGroup" name="dropdown_customerGroup" <?php if (!$this->user->hasPermission('clients_edit_customers')) { ?>  disabled="disabled"
                                <?php } ?> >
                               <?php foreach ($this->groups as $group) { ?>
                                    <option value="<?php echo $group['groupValue'];?>" <?php echo $group['groupSelected'];?> >
                                        <?php echo $group['groupLabel'];?>
                                    </option>
                                <?php } ?>
                                </select>
                            </div>
                        </div>
                        <?php } ?>
                        <?php foreach ($this->customFields as $cf) { CE_Lib::log(4, $cf); ?>
                        <div class="col-sm-6">
                            <div class="form-elem" <?php if ($cf['fieldType'] == typeVATNUMBER) { ?> id="vatBlock" <?php } ?>>
                                <label>
                                    <?php echo $cf['rowLabel'];?>
                                </label>

                                 <?php if ($cf['typeSimple']) { ?>

                                <?php  if ($cf['fieldType'] == typeVATNUMBER) { ?>
                                <div class="input-group">
                                    <span class="input-group-text" id="vat_country"></span>
                                <?php } ?>

                                <input class="form-control" type="text" name="CT_<?php echo $cf['fieldId'];?>" id="CT_<?php echo $cf['fieldId'];?>" value="<?php echo $cf['value'];?>" <?php echo $cf['additionalSettings'];?> <?php echo $cf['disabled'];?> />
                                <?php } ?>
                                <?php  if ($cf['fieldType'] == typeVATNUMBER) { ?>
                                </div>
                                <?php } ?>


                                <?php if (count($cf['typeLanguages']) > 0) { ?>
                                <select class="form-control normalSelect2" name='CT_<?php echo $cf['fieldId'];?>' <?php echo $cf['disabled'];?> >
                                <?php foreach($cf['typeLanguages'] as $language) { ?>
                                    <option size="30" value="<?php echo $language['typeLanguageOptionValue'];?>" <?php echo $language['typeLanguageOptionSelected'];?> >
                                        <?php echo $language['typeLanguageOptionLabel'];?>
                                    </option>
                                <?php } ?>
                                </select>
                                <?php } ?>

                                <?php if ($cf['typeTextarea']) { ?>
                                <textarea class="form-control" name='CT_<?php echo $cf['fieldId'];?>' cols='<?php echo $cf['fieldWidth'];?>' <?php echo $cf['disabled'];?> >
                                    <?php echo $cf['value'];?>
                                </textarea>
                                <?php } ?>

                                <?php if (count($cf['dropDowns']) > 0) { ?>
                                <select class="normalSelect2 form-control" name="CT_<?php echo $cf['fieldId'];?>" <?php echo $cf['disabled'];?> >
                                <?php foreach ($cf['dropDowns'] as $dropdown) { ?>
                                    <option value="<?php echo $dropdown['typeDropdownOptionValue'];?>" <?php echo $dropdown['typeDropdownOptionSelected'];?> >
                                        <?php echo $dropdown['typeDropdownOptionLabel'];?>
                                    </option>
                                <?php } ?>
                                </select>
                                <?php } ?>

                                <?php if ($cf['typeCountry']) { ?>
                                <script data-cfasync="false" type="text/javascript">
                                    countryVAR = "CT_<?php echo $cf['fieldId'];?>";
                                </script>
                                <select class="searchSelect2 form-control" name="CT_<?php echo $cf['fieldId'];?>" id="CT_<?php echo $cf['fieldId'];?>" onChange="clientexec.checkListsStates();" <?php echo $cf['disabled'];?> >
                                    <?php echo $cf['countryOptions'];?>
                                </select>
                                <?php } ?>

                                <?php if ($cf['typeYesNo']) {?>
                                <select class="normalSelect2 form-control" name="CT_<?php echo $cf['fieldId'];?>" <?php echo $cf['disabled'];?> >
                                    <option value="1" <?php echo $cf['yesSelected'];?> ><?php echo $this->user->lang("Yes");?></option>
                                    <option value="0" <?php echo $cf['noSelected'];?> ><?php echo $this->user->lang("No");?></option>
                                </select>
                                <?php } ?>

                                 <?php if ($cf['typeDate']) { ?>
                                <input type="hidden" name="CTT_<?php echo $cf['fieldId'];?>" id="CTT_<?php echo $cf['fieldId'];?>" value="<?php echo $cf['typeDateConstant'];?>" />
                                <input class="datepicker form-control" type="text" name="CT_<?php echo $cf['fieldId'];?>" id="CT_<?php echo $cf['fieldId'];?>" value="<?php echo $cf['value'];?>" <?php echo $cf['readOnly'];?> />
                                <?php } ?>

                                <?php if ($cf['VATmessages']) { ?>
                                <script data-cfasync="false" type="text/javascript">
                                    var vatVAR = "CT_<?php echo $cf['fieldId'];?>";
                                </script>
                                <div id="vat_validating" style="display:none"><?php echo $this->user->lang("Validating...");?></div>
                                <div id="vat_valid" style="display:none"><?php echo $this->user->lang("Valid VAT Number");?></div>
                                <div id="vat_invalid" style="display:none"><?php echo $this->user->lang("Invalid VAT Number");?>. <a href="javascript:clientexec.getTax();"><?php echo $this->user->lang("Retry");?></a></div>
                                <div id="vat_error" style="display:none"><?php echo $this->user->lang("Unable to validate at the moment.");?> <a href="javascript:clientexec.getTax();"><?php echo $this->user->lang("Retry");?></a></div>

                                <?php } ?>

                                <?php if ($cf['typeState']) { ?>
                                <script data-cfasync="false" type="text/javascript">
                                    var stateVAR = "CT_<?php echo $cf['fieldId'];?>";
                                </script>

                                <select class="form-control searchSelect2" name="CT_<?php echo $cf['fieldId'];?>" id="CT_<?php echo $cf['fieldId'];?>" onChange="clientexec.getTax(mainForm);" <?php echo $cf['disabled'];?> >
                                    <?php echo $cf['stateOptions'];?>
                                </select>
                                <?php } ?>

                            </div>
                        </div>
                        <?php } ?>
                    </div>
                    <?php if ($this->user->hasPermission('clients_edit_customers')) { ?>
                    <h4>Supply only if you want password changed</h4>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-elem">
                                <label>New Password</label>
                                <input class="form-control" type="password" id="new_password" name="new_password" autocomplete="off">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-elem">
                                <label>Confirm password</label>
                                <input class="form-control" type="password" id="confirm_password" name="confirm_password" autocomplete="off">
                            </div>
                        </div>
                    </div>
                    <?php if ($this->showSaveButton) {?>
                        <input id="updatecontact" type="submit" class="cm-btns lg orange" value="<?php echo $this->user->lang("Update");?>">
                        <hr>
                    <?php } ?>
                    <?php } ?>
                </form>
                    <?php
                    if ($this->showMergeButton) {
                        if (!$this->customer->isGuest()) {
                    ?>
                    <button onclick="window.location='index.php?fuse=clients&view=viewmergeclient&controller=user'" class="cm-btns orange">Merge</button>
                <?php } else { ?>
                    <button onclick="moveGuestToCustomer();" class="cm-btns orange">Merge</button>
                <?php } ?>
                <?php } ?>

                <?php if ($this->showResetPasswordButton) { ?>
                <input type="hidden" name="resetpass-customer-id" id="resetpass-customer-id" value="<?php echo $this->customerId;?>" />
                <button id="reset-password" class="cm-btns orange"><?php echo $this->user->lang('Reset Password'); ?></button>
                <?php } ?>

                <?php if ($this->showExportDataButton) { ?>
                <input type="hidden" name="exportdata-customer-id" id="exportdata-customer-id" value="<?php echo $this->customerId;?>" />
                <button id="export-data" class="cm-btns orange"><?php echo $this->user->lang('Export Data'); ?></button>
                <?php } ?>

                <?php if ($this->showDeleteButton) { ?>
                    <input type="hidden" id="deleteid" name="deleteid" value="<?php echo $this->customerId;?>" />
                    <input type="hidden" id="deletewithplugin" name="deletewithplugin" value="0" />
                    <button id="deleteclient" class="cm-btns red"><?php echo $this->user->lang("Delete");?></button>
                <?php } ?>
            </div>
        </div>
    </div>

</div>

<script data-cfasync="false" type="text/javascript">
    var mainForm = document.forms['customerdata'];
    $(document).ready(function() {
        currentCountry = '';
        clientexec.getTax(mainForm);
        $('#profile-fields').show();
    });

     $("#updatecontact").on('click', function() {

        var contactForm = $("#customerdata");
        $.ajax( {
            url: contactForm.attr('action'),
            type: contactForm.attr('method'),
            data: contactForm.serialize(),
            success: function (json){
                clientexec.parseResponse(json);
            }
        });

        return false;
    });

    $('#reset-password').on('click', function(e){
        e.preventDefault();
        RichHTML.msgBox(clientexec.lang('Are you sure you want to reset the password of this client?'), {
            type:"yesno"
        }, function(result) {
            if (result.btn === clientexec.lang("Yes")) {
                RichHTML.mask();
                $.ajax({
                    url: 'index.php?fuse=clients&controller=user&action=resetpass',
                    type: 'POST',
                    data: { id: $('#resetpass-customer-id').val() },
                    success: function (json) {
                        clientexec.parseResponse(json);
                        RichHTML.unMask();
                    }
                });
            }
        });
    });

    $('#dropdown_customerGroup').on('select2:select', function (e) {
        $.ajax({
            url: "index.php?fuse=clients&controller=userprofile&action=updateclientgroup",
            data: { groupid: $(this).val() },
            success: function(data) {
                clientexec.parseResponse(data);
            }
        });
    });


    $('#export-data').on('click', function(e){
        e.preventDefault();
        RichHTML.msgBox(clientexec.lang('Are you sure you want to export this clients data?'), {
            type:"yesno"
        }, function(result) {
            if (result.btn === clientexec.lang("Yes")) {
                window.location = 'index.php?fuse=admin&view=viewexportplugins&plugin=fullclientdata&controller=importexport&exportdata='+$('#exportdata-customer-id').val();
            }
        });
    });

    $('#deleteclient').click(function() {

        RichHTML.msgBox(clientexec.lang('Are you sure you want to delete this client?'), {
            type:"confirm"
        }, function(result) {
            if (result.btn === clientexec.lang("Yes")) {
                RichHTML.msgBox(clientexec.lang("Do you want to delete this client's packages using the respective server plugin(s)?"), {
                    type:'confirm'
                }, function (innerResult) {

                    if (innerResult.btn === clientexec.lang('Cancel')) {
                        return;
                    }

                    if (innerResult.btn === clientexec.lang('Yes')) {
                        $('#deletewithplugin').val(1)
                    }

                    $.ajax({
                        url: 'index.php?fuse=clients&controller=user&action=deleteclient',
                        type: 'POST',
                        data: {
                            deleteid: $('#deleteid').val(),
                            deletewithplugin: $('#deletewithplugin').val()
                        },
                        success: function (json){
                            clientexec.parseResponse(json);
                            if (json.success == true) {
                                window.location = "index.php?fuse=clients&controller=user&view=viewusers";
                            }
                        }
                    });
                });
            }
        });
        return false;
    });



</script>
