@extends('app.app')

@section('title')
    Users
@endsection

@section('customcss')
    <style>
        /* .select2-container {
                    z-index: 99999;
                } */
    </style>
@endsection

@section('content')
    <div class="container-fluid">
        <div class="content">
            <div class="row">
                <div class="col-sm-12">
                    <div class="card">
                        <div class="card-header">
                            <div class="row d-flex align-items-center">
                                <div class="col">
                                    <p class="card-title text-bold mb-0">Profile</p>
                                </div>
                                <div class="col d-flex justify-content-end">
                                    <label class="form-check-label" for="updt-profile"><span class="text-dark" id="updt-txtprofile">Change Profile ?</span></label>
                                    &nbsp;&nbsp;&nbsp;
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="updt-profile">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-sm-6">
                                    <h6>User Infomation</h6>
                                    <div class="mb-3">
                                        <label for="updt-first_name" class="form-label">Name</label>
                                        <input type="text" class="form-control form-control-sm" name="updt-first_name" id="updt-first_name">
                                    </div>
                                    <div class="mb-3">
                                        <label for="updt-phone" class="form-label">Phone</label>
                                        <input type="number" class="form-control form-control-sm" name="updt-phone" id="updt-phone" placeholder="without zero">
                                    </div>
                                    <div class="mb-3">
                                        <label for="updt-email" class="form-label">Email</label>
                                        <input type="mail" class="form-control form-control-sm" name="updt-email" id="updt-email">
                                    </div>
                                    <div class="mb-3">
                                        <label for="updt-fulladdress" class="form-label">Address</label>
                                        <textarea name="updt-fulladdress" id="updt-fulladdress" class="form-control form-control-sm" rows="3"></textarea>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <h6>Change Password</h6>
                                    <div class="mb-3">
                                        <label for="updt-is_change_pasw" class="form-label">Update Password ?</label>
                                        <select id="updt-is_change_pasw" name="updt-is_change_pasw" class="form-control" style="width:100%;">
                                            <option value="no">No</option>
                                            <option value="yes">Yes</option>
                                        </select>
                                    </div>
                                    <div class="form-row groupPassword d-none">
                                        <div class="col">
                                            <div class="mb-3">
                                                <label for="updt-password" class="form-label">Password</label>
                                                <input type="password" class="form-control form-control-sm" name="updt-password" id="updt-password">
                                            </div>
                                        </div>
                                        <div class="col">
                                            <div class="mb-3">
                                                <label for="updt-conf_password" class="form-label">Password Confirmation</label>
                                                <input type="password" class="form-control form-control-sm" name="updt-conf_password" id="updt-conf_password">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-12 text-end">
                                    <button type="button" id="btnSubmitUpdtUser" class="btn btn-sm btn-danger">Update data</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-8"></div>
            </div>
        </div>
    </div>
@endsection

@section('customjs')
    <script>
        'use strict';
        const State = {
            uid: "{{ Auth::user()->id }}",
            file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
            storage_lara: "{{ asset('storage') }}/",
            user_status: {
                active: "{{ App\Models\Users::STATUS_ACTIVE }}",
                inactive: "{{ App\Models\Users::STATUS_NOT_ACTIVE }}",
            },
            user_roles: {
                checker: "{{ App\Models\Users::ROLE_CHECKER }}",
                vendor: "{{ App\Models\Users::ROLE_VENDOR }}",
            },
            is_tracking: {
                no: "{{ App\Models\Users::IS_TRACK_VHC_NO }}",
                yes: "{{ App\Models\Users::IS_TRACK_VHC_YES }}",
                default: "{{ App\Models\Users::IS_TRACK_VHC_DEFAULT }}",
            },
        };

        const Wrapper = {
            activate: function() {
                Wrapper.event();
                UUpdate.activate();
            },
            event: function() {
                $('#updt-is_change_pasw').select2({
                    readonly: true,
                });

                $('#updt-profile').on('change', function() {
                    $('#updt-is_change_pasw').trigger('no').change();

                    if ($('#updt-profile').prop('checked')) {
                        UUpdate.removeReadOnly();
                    } else {
                        UUpdate.addReadOnly();
                    }
                })

                $('#updt-profile').prop('checked', false).trigger('change');
            },
        };

        const UUpdate = {
            activate: function() {
                UUpdate.event();
            },
            event: function() {
                let uid = State.uid;
                // update profile
                $('#btnSubmitUpdtUser').on('click', function() {
                    let data = UUpdate.getData();
                    if (typeof data.password != 'undefined') {
                        if (data.password !== data.conf_password) {
                            Helper.toast('Warning', 'just now', 'password not same');
                            return false;
                        }
                    }
                    UUpdate.submitData(data);
                });
                // change pw
                $('#updt-is_change_pasw').on('change', function() {
                    let val = $('#updt-is_change_pasw').val();
                    $('#updt-password').val('');
                    $('#updt-conf_password').val('');
                    if (val === 'yes') {
                        $('.groupPassword').removeClass('d-none');
                    } else {
                        $('.groupPassword').addClass('d-none');
                    }
                });
                // get data
                (async () => {
                    let resp = await UUpdate.reqData({
                        uid
                    });
                    if (resp.type != 'success') {
                        Helper.toast('User Not Found', 'just now', 'please try again');
                        return false;
                    }
                    UUpdate.passDataToView(resp.data);
                })();
            },
            reqData: function(params) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_show_user', '') }}/" + params.uid,
                        method: 'GET',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                        },
                        data: params,
                        success: (data, textStatus, jqXHR) => {
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                data: data.data
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                });
            },
            passDataToView: function(data) {
                $('#updt-first_name').val(data.first_name);
                $('#updt-fulladdress').val(data.fulladdress);
                $('#updt-phone').val(data.phone);
                $('#updt-email').val(data.email);
                $('#updt-is_change_pasw').val('no').trigger('change');
            },
            getData: function() {
                let data = {};
                data.uid = State.uid;
                data.first_name = $('#updt-first_name').val();
                data.fulladdress = $('#updt-fulladdress').val();
                data.phone = $('#updt-phone').val();
                data.email = $('#updt-email').val();

                data.password = $('#updt-password').val();
                data.conf_password = $('#updt-conf_password').val();

                return data;
            },
            submitData: async function(data) {
                return new Promise((resolve, reject) => {
                    if (typeof $('#btnSubmitUpdtUser').attr('disabed') != 'undefined') {
                        resolve({
                            type: 'fail'
                        });
                        return false;
                    }
                    $('#btnSubmitUpdtUser').attr('disabed', true);
                    $.ajax({
                        url: "{{ route('api_edit_user_profile', '') }}/" + data.uid,
                        method: 'PUT',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                            'x-api-key': Helper.getCookie('_trtk'),
                        },
                        data: data,
                        success: (data, textStatus, jqXHR) => {
                            $('#btnSubmitUpdtUser').removeAttr('disabed');
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            Helper.toast('Success', 'just now', 'success update profile');
                            $('#updt-profile').prop('checked', false).trigger('change');
                            resolve({
                                type: 'success'
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            $('#btnSubmitUpdtUser').removeAttr('disabed');
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                });
            },
            removeReadOnly: function() {
                $('#updt-first_name').removeAttr('readonly');
                $('#updt-fulladdress').removeAttr('readonly');
                $('#updt-phone').removeAttr('readonly');
                $('#updt-email').removeAttr('readonly');
                $('#updt-is_change_pasw').val('no').trigger('change');
                $('#updt-is_change_pasw').select2({
                    disabled: false,
                });
                $('#btnSubmitUpdtUser').removeClass('d-none');
            },
            addReadOnly: function() {
                $('#updt-first_name').attr('readonly', true);
                $('#updt-fulladdress').attr('readonly', true);
                $('#updt-phone').attr('readonly', true);
                $('#updt-email').attr('readonly', true);
                $('#updt-is_change_pasw').val('no').trigger('change');
                $('#updt-is_change_pasw').select2({
                    disabled: true,
                });
                $('#btnSubmitUpdtUser').addClass('d-none');
            },
        }

        Wrapper.activate();
    </script>
@endsection
