<link href="<?php echo base_url('application/modules/dashboard/assets/css/user-form.css'); ?>" rel="stylesheet" type="text/css"/>

<div class="row">
        <div class="col-sm-12 col-md-12">
            <div class="panel panel-bd">
                <div class="panel-heading">
                    <div class="panel-title">
                       <h4><?php echo display('add_user')?></h4>
                    </div>
                </div>
                <div class="panel-body">
                <?php echo form_open_multipart("dashboard/user/form/$user->id") ?>
                    
                    <?php echo form_hidden('id',$user->id) ?>
                    
                    <div class="form-group row">
                        <label for="firstname" class="col-sm-3 col-form-label"><?php echo display('employee') ?> *</label>
                        <div class="col-sm-9">
                          <?php echo form_dropdown('employee_id',$empl,(!empty($empinfo->employee_id)?$empinfo->employee_id:null),'class="form-control employee-in-user" onchange="EmployeeinUser(this.value)"') ?>
                          
                           <input name="firstname" class="form-control" type="" placeholder="<?php echo display('firstname') ?>" id="firstname"  value="<?php echo $user->firstname ?>">
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="lastname" class="col-sm-3 col-form-label"><?php echo display('lastname') ?> *</label>
                        <div class="col-sm-9">
                            <input name="lastname" class="form-control" type="text" placeholder="<?php echo display('lastname') ?>" id="lastname" value="<?php echo $user->lastname ?>">
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="email" class="col-sm-3 col-form-label"><?php echo display('email') ?> *</label>
                        <div class="col-sm-9">
                            <input name="email" class="form-control" type="text" placeholder="<?php echo display('email') ?>" id="email_id" value="<?php echo $user->email ?>">
                        </div>
                    </div> 

                    <div class="form-group row">
                        <label for="password" class="col-sm-3 col-form-label"><?php echo display('password') ?> *</label>
                        <div class="col-sm-9">
                            <input name="password" class="form-control" type="password" placeholder="<?php echo display('password') ?>" id="password">
                            <input name="oldpassword" class="form-control" type="" value="<?php echo $user->password ?>">
                        </div>
                    </div>

                

                    <div class="form-group row">
                    
                        <div class="col-sm-9">
                            <input type="hidden" name="image" id="image" aria-describedby="fileHelp">
                            <small id="fileHelp" class="text-muted"></small>
                             <input type="hidden" name="old_image" value="<?php echo $user->image ?>">
                        </div>
                    </div> 

         
                    <div class="form-group row">
                        <label for="status" class="col-sm-3 col-form-label">Status *</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                                <?php echo form_radio('status', '1', (($user->status==1 || $user->status==null)?true:false), 'id="status"'); ?>Active
                            </label>
                            <label class="radio-inline">
                                <?php echo form_radio('status', '0', (($user->status=="0")?true:false) , 'id="status"'); ?>Inactive
                            </label> 
                        </div>
                    </div>
         
                    <div class="form-group form-group-margin text-right">
                        <button type="reset" class="btn btn-primary w-md m-b-5"><?php echo display('reset') ?></button>
                        <button type="submit"  class="btn btn-success w-md m-b-5"><?php echo display('save') ?></button>
                        <button type="button" class="btn btn-info w-md m-b-5" onclick="saveToAnotherDatabase()">Save As Employee</button>
                    </div>
                <?php echo form_close() ?>

            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
function saveToAnotherDatabase() {
    var formData = {
        id: $('input[name=id]').val(),
        firstname: $('input[name=firstname]').val(),
        lastname: $('input[name=lastname]').val(),
        email: $('input[name=email]').val(),
        password: $('input[name=password]').val(),
        oldpassword: $('input[name=oldpassword]').val(),
        status: $('input[name=status]:checked').val(),
    };

    console.log(formData); // Debugging line

    $.ajax({
        url: '<?php echo site_url('dashboard/user/save_to_another_database'); ?>',
        type: 'POST',
        data: formData,
        dataType: 'json', // Expecting JSON response
        success: function(response) {
            console.log(response);

            if (response.status === 'success') {
                // Display the success message
                $('body').prepend('<div class="alert alert-success">' + response.message + '</div>');

                // Optionally, redirect the user
                setTimeout(function() {
                    window.location.href = '<?php echo site_url('dashboard/user/form'); ?>/' + formData.id;
                }, 2000); // Redirect after 2 seconds
            } else {
                // Display the error message
                $('body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.error('AJAX Error: ', textStatus, errorThrown);
            $('body').prepend('<div class="alert alert-danger">An error occurred while processing the request.</div>');
        }
    });
}

</script>


 