<?php include 'header.php'; ?>

    <style>.ts-wrapper{width:100%;position:relative;}.ts-control{min-height:56px!important;height:56px!important;border:1px solid #d1d5db!important;border-radius:16px!important;padding:0 16px!important;background:#fff!important;box-shadow:none!important;display:flex!important;align-items:center!important;overflow:hidden!important;}.ts-control input{font-size:14px!important;}.ts-wrapper.focus .ts-control{border-color:#8b5cf6!important;box-shadow:0 0 0 4px rgba(139,92,246,0.10)!important;}.ts-dropdown{margin-top:8px!important;border-radius:16px!important;border:1px solid #e5e7eb!important;overflow:hidden!important;background:#fff!important;box-shadow:0 10px 30px rgba(0,0,0,0.10)!important;z-index:9999!important;}.ts-dropdown-content{max-height:220px!important;overflow-y:auto!important;overflow-x:hidden!important;scroll-behavior:smooth;}.ts-dropdown-content::-webkit-scrollbar{width:6px;}.ts-dropdown-content::-webkit-scrollbar-thumb{background:#c4b5fd;border-radius:20px;}.ts-dropdown .option{padding:14px 16px!important;font-size:14px!important;cursor:pointer;}.ts-dropdown .active{background:#f3e8ff!important;color:#7c3aed!important;}.fi{position:relative;}.fi input{width:100%;height:56px;border:1px solid #d1d5db;border-radius:16px;padding:0 16px;outline:none;font-size:15px;background:#fff;transition:0.25s;}.fi input:focus{border-color:#8b5cf6;box-shadow:0 0 0 4px rgba(139,92,246,0.10);}.fi label{position:absolute;left:14px;top:50%;transform:translateY(-50%);background:#fff;padding:0 6px;font-size:14px;color:#6b7280;pointer-events:none;transition:0.2s;}.fi input:focus + label,.fi input:not(:placeholder-shown) + label{top:0;transform:translateY(-50%);font-size:11px;color:#8b5cf6;}.si{position:relative;}.si select{width:100%;height:56px;border:1px solid #d1d5db;border-radius:16px;padding:0 42px 0 16px;outline:none;font-size:15px;background:#fff;appearance:none;transition:0.25s;}.si select:focus{border-color:#8b5cf6;box-shadow:0 0 0 4px rgba(139,92,246,0.10);}.si::after{content:'\ea4e';font-family:'remixicon';position:absolute;right:16px;top:50%;transform:translateY(-50%);color:#6b7280;pointer-events:none;}@media(max-width:640px){.ts-control{height:52px!important;min-height:52px!important;}.fi input,.si select{height:52px;}}</style>


    <!-- HEADER -->
    <div class="max-w-7xl mx-auto px-3 sm:px-4 lg:px-8 pt-6 sm:pt-8 pb-5 sm:pb-6">

        <div class="flex items-start justify-between">

            <div>
                <h1 class="text-3xl font-bold text-gray-900">
                    Add User
                </h1>
            </div>

<a href="user.php"
    class="inline-flex items-center gap-2 h-11 px-5 rounded-xl border border-gray-200 bg-white hover:bg-gray-50 text-gray-700 text-sm font-semibold transition-all shadow-sm">

    <i class="ri-arrow-left-line"></i>
    Back

</a>

        </div>

    </div>

    <!-- FORM -->
    <div class="max-w-7xl mx-auto px-3 sm:px-4 lg:px-8 pb-6 sm:pb-10">

        <div class="w-full bg-white rounded-3xl shadow-xl border border-gray-100 p-4 sm:p-6 md:p-8 lg:p-12">

            <!-- FORM CONTENT -->
            <form id="formContent" autocomplete="off" novalidate>

                <div class="mb-8">

                    <h2 class="text-2xl sm:text-3xl font-bold text-gray-900">
                        User Details
                    </h2>
                </div>

                <!-- TWO INPUTS PER ROW -->
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">

<!-- NAME -->
<div class="fi">
    <input type="text" id="name" placeholder=" " />
    <label>Name <span class="text-red-500">*</span></label>
</div>

<!-- EMAIL -->
<div class="fi">
    <input type="email" id="email" spellcheck="false" autocomplete="off" placeholder="" />
    <label>Email Address <span class="text-red-500">*</span></label>

    <p id="emailError" class="hidden text-red-500 text-xs mt-1 pl-1">
        Enter valid email address
    </p>
</div>

<!-- MOBILE -->
<div class="fi">
    <input
        type="text"
        id="mobile"
        maxlength="10"
        placeholder=" "
        oninput="validateMobile(this)"
    />

    <label>Mobile No. <span class="text-red-500">*</span></label>

    <p id="mobileError" class="hidden text-red-500 text-xs mt-1 pl-1">
        Mobile number must be exactly 10 digits
    </p>
</div>

<!-- PASSWORD -->
<div class="fi relative">
    <input type="password" id="password" placeholder=" " />
    <label>Password <span class="text-red-500">*</span></label>
    
       <button
        type="button"
        onclick="togglePassword()"
        class="absolute right-4 top-1/2 -translate-y-1/2 text-gray-500 hover:text-violet-600 z-10">
        <i id="passwordIcon" class="ri-eye-line text-lg"></i>
    </button>
</div>

<!-- ROLE -->
<div class="si">
    <select id="role" placeholder="Role">
        <option value="">Role</option>
        <option>Admin</option>
        <option>Super Admin</option>
    </select>
</div>

<!-- STATUS -->
<div class="si">
    <select id="status" placeholder="Status">
        <option value="">Status</option>
        <option>Active</option>
        <option>Inactive</option>
    </select>
</div>

                </div>

                <!-- BUTTON -->
                <div class="flex justify-end mt-8 pt-6 border-t border-gray-100">

                    <button
                    type="button"
                        onclick="submitForm()"
                        class="w-full sm:w-auto inline-flex justify-center items-center gap-2 h-12 px-9 rounded-xl bg-gradient-to-r from-blue-500 to-indigo-600 hover:from-blue-600 hover:to-indigo-700 text-white text-sm font-semibold transition-all shadow-lg shadow-violet-200">
                        Save
                        <i class="ri-check-line"></i>

                    </button>

                </div>

            </form>

            <!-- SUCCESS -->
            <div id="successMsg" class="hidden text-center py-10">

                <div class="w-16 h-16 rounded-full bg-violet-100 flex items-center justify-center mx-auto mb-4">

                    <i class="ri-check-line text-3xl text-violet-600"></i>

                </div>

                <h2 class="text-2xl font-bold text-gray-900 mb-2">
                    User Created!
                </h2>

                <p class="text-sm text-gray-500 mb-5">
                     User has been created successfully.
                </p>

                <button
                    onclick="resetForm()"
                    class="inline-flex items-center gap-2 px-6 py-3 rounded-xl bg-violet-500 hover:bg-violet-600 text-white text-sm font-semibold transition-all shadow-lg shadow-violet-200">

                    <i class="ri-add-line"></i>
                    Create Another User

                </button>

            </div>

        </div>

    </div>

    <script>

const searchableSelects = [
    '#role',
    '#status'
];

        searchableSelects.forEach(id => {

            if(document.querySelector(id)){

                new TomSelect(id, {
                    create:false,
                    allowEmptyOption:true,
                    placeholder:'Search...',
                    closeAfterSelect:true,

                    render:{
                        no_results:function(){
                            return '<div class="p-3 text-sm text-gray-400">No results found</div>';
                        }
                    }
                });

            }

        });

        /* SUBMIT */

/* SUBMIT */

function submitForm(){

    const name = document.getElementById('name').value.trim();

    const email = document.getElementById('email').value.trim();

    const mobile = document.getElementById('mobile').value.trim();

    const password = document.getElementById('password').value.trim();

    const role = document.getElementById('role').value;

    const status = document.getElementById('status').value;

    /* REGEX */
    const emailRegex =
        /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[A-Za-z]{2,}$/;

    const mobileRegex = /^[0-9]{10}$/;

    /* NAME */
    if(name === ''){

        Swal.fire({
            icon:'warning',
            title:'Name Required',
            text:'Please enter user name',
            width: '350px',
        });

        return;
    }

    /* EMAIL */
    if(!emailRegex.test(email)){

        Swal.fire({
            icon:'warning',
            title:'Invalid Email',
            text:'Please enter valid email address',
            width: '350px',
        });

        return;
    }

    /* MOBILE */
    if(!mobileRegex.test(mobile)){

        Swal.fire({
            icon:'warning',
            title:'Invalid Mobile Number',
            text:'Mobile number must be exactly 10 digits',
            width: '350px',
        });

        return;
    }

    /* PASSWORD */
    if(password === ''){

        Swal.fire({
            icon:'warning',
            title:'Password Required',
            text:'Please enter password',
            width: "350px",
        });

        return;
    }

    /* ROLE */
    if(role === ''){

        Swal.fire({
            icon:'warning',
            title:'Role Required',
            text:'Please select role',
            width: '350px',
        });

        return;
    }

    /* STATUS */
    if(status === ''){

        Swal.fire({
            icon:'warning',
            title:'Status Required',
            text:'Please select status',
            width:'350px',
        });

        return;
    }

    /* SUCCESS */
    Swal.fire({
        icon:'success',
        title:'User Created!',
        text:'User has been created successfully',
        confirmButtonColor:'#8b5cf6',
        width:'350px',
    }).then(() => {

        document.getElementById('formContent')
            .classList.add('hidden');

        document.getElementById('successMsg')
            .classList.remove('hidden');

    });

}


/* MOBILE VALIDATION */

function validateMobile(input){

    /* REMOVE EVERYTHING EXCEPT NUMBERS */
    input.value = input.value.replace(/[^0-9]/g, '');

    /* LIMIT TO 10 DIGITS */
    if(input.value.length > 10){

        input.value = input.value.slice(0,10);
    }
}
        /* RESET */

function resetForm(){

            document.querySelectorAll('input')
                .forEach(el => el.value = '');

            document.querySelectorAll('select')
                .forEach(el => {

                    el.selectedIndex = 0;

                    if(el.tomselect){

                        el.tomselect.clear();

                        el.tomselect.refreshOptions(false);
                    }

                });

            document.getElementById('formContent')
                .classList.remove('hidden');

            document.getElementById('successMsg')
                .classList.add('hidden');
        }
        
        function togglePassword() {

    const password = document.getElementById('password');
    const icon = document.getElementById('passwordIcon');

    if (password.type === 'password') {

        password.type = 'text';
        icon.classList.remove('ri-eye-line');
        icon.classList.add('ri-eye-off-line');

    } else {

        password.type = 'password';
        icon.classList.remove('ri-eye-off-line');
        icon.classList.add('ri-eye-line');
    }
}

    </script>



<?php include 'footer.php'; ?>