@extends('layouts/base_view')
@section('title')
{!! $variables['header_routes'] !!}
@endsection
@section('content')
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card card-outline card-warning">
                    <div class="card-header">
                        <h3 class="card-title">{!! $variables['judul'] !!}</h3>
                    </div>
                    <div class="card-body">
                        <!-- <form method="post" action="{{ route('users.store', $data->id, $variables['type']) }}" enctype="multipart/form-data"> -->
                        <form method="post" action="{{ route('users.store', [$data->id, $variables['type']]) }}" enctype="multipart/form-data">
                            @csrf
                            @if($variables['type'] == 'cp')
                            <div class="row">

                                @php
                                $type = '';
                                if ($f_need_change_password && $f_need_change_password != '') {
                                $type = "style=display:none";
                                }
                                @endphp

                                <div class="form-group col-md-4" {{$type}}>
                                    <label for="current_password" class="font-weight-bold">Password Lama</label>
                                    <input
                                        type="password"
                                        class="form-control form-control-sm"
                                        name="current_password"
                                        value="{{ $f_need_change_password ?? '' }}"
                                        placeholder="">
                                    @if ($errors->has('current_password'))
                                    <span class="text-danger text-left">{{ $errors->first('current_password') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="password" class="font-weight-bold">Password Baru</label>
                                    <input
                                        type="password"
                                        class="form-control form-control-sm"
                                        name="password"
                                        placeholder=""
                                        value="{{ old('password', '') }}">
                                    @if ($errors->has('password'))
                                    <span class="text-danger text-left">{{ $errors->first('password') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="password_confirmation" class="font-weight-bold">Konfirmasi Password</label>
                                    <input
                                        type="password"
                                        class="form-control form-control-sm"
                                        name="password_confirmation"
                                        placeholder=""
                                        value="{{ old('password_confirmation', $data->password_confirmation ?? '') }}">
                                    @if ($errors->has('password_confirmation'))
                                    <span class="text-danger text-left">{{ $errors->first('password_confirmation') }}</span>
                                    @endif
                                </div>
                            </div>
                            @elseif($variables['type'] == 'ema')
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <label for="fullname" class="font-weight-bold">Nama Lengkap</label>
                                    <input value="{{ old('fullname', $data->fullname ?? '') }}" type="text" class="form-control form-control-sm" name="fullname" placeholder="">
                                    @if ($errors->has('fullname'))
                                    <span class="text-danger text-left">{{ $errors->first('fullname') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="email" class="font-weight-bold">Email</label>
                                    <input value="{{ old('email', $data->email ?? '') }}" type="text" class="form-control form-control-sm" name="email" placeholder="">
                                    @if ($errors->has('email'))
                                    <span class="text-danger text-left">{{ $errors->first('email') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="telephone" class="font-weight-bold">Telepon</label>
                                    <input value="{{ old('telephone', $data->telephone ?? '') }}" type="text" class="form-control form-control-sm" name="telephone" placeholder="">
                                    @if ($errors->has('telephone'))
                                    <span class="text-danger text-left">{{ $errors->first('telephone') }}</span>
                                    @endif
                                </div>
                            </div>
                            @else
                            <div class="row">
                                <div class="form-group col-md-3">
                                    <label for="fullname" class="font-weight-bold">Fullname</label>
                                    <input value="{{ old('fullname', $data->fullname ?? '') }}" type="text" class="form-control form-control-sm" name="fullname" placeholder="">
                                    @if ($errors->has('fullname'))
                                    <span class="text-danger text-left">{{ $errors->first('fullname') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="email" class="font-weight-bold">Email</label>
                                    <input value="{{ old('email', $data->email ?? '') }}" type="text" class="form-control form-control-sm" name="email" placeholder="">
                                    @if ($errors->has('email'))
                                    <span class="text-danger text-left">{{ $errors->first('email') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="telephone" class="font-weight-bold">Telephone</label>
                                    <input value="{{ old('telephone', $data->telephone ?? '') }}" type="text" class="form-control form-control-sm" name="telephone" placeholder="">
                                    @if ($errors->has('telephone'))
                                    <span class="text-danger text-left">{{ $errors->first('telephone') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="role_id" class="font-weight-bold">Role</label>
                                    <select name="role_id" class="form-control form-control-sm" required>
                                        <option value="">-- Pilih Role --</option>
                                        @foreach($variables['roles'] as $comb)
                                        <option value="{{ $comb->id }}" {{ old('role_id', $variables['currentRole'] ?? '') == $comb->id ? 'selected' : '' }}>
                                            {{ $comb->name }}
                                        </option>
                                        @endforeach
                                    </select>
                                    @if ($errors->has('role_id'))
                                    <span class="text-danger">{{ $errors->first('role_id') }}</span>
                                    @endif
                                </div>
                                @if(empty($data->username))
                                <div class="form-group col-md-3">
                                    <label for="username" class="font-weight-bold">Username</label>
                                    <input value="{{ old('username', $data->username ?? '') }}" type="text" class="form-control form-control-sm" name="username" placeholder="">
                                    @if ($errors->has('username'))
                                    <span class="text-danger text-left">{{ $errors->first('username') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="password" class="font-weight-bold">Password</label>
                                    <input
                                        type="password"
                                        class="form-control form-control-sm"
                                        name="password"
                                        placeholder=""
                                        value="{{ $data->password ?? '' }}">
                                    @if ($errors->has('password'))
                                    <span class="text-danger text-left">{{ $errors->first('password') }}</span>
                                    @endif
                                </div>
                                @endif
                                <div class="form-group col-md-3">
                                    <label for="status" class="font-weight-bold">Status <span class="text-danger">*</span></label>
                                    <select name="status" class="form-control form-control-sm" required>
                                        <option value="Active" {{ old('status', $data->status ?? 'Active') == 'Active' ? 'selected' : '' }}>Aktif</option>
                                        <option value="Inactive" {{ old('status', $data->status ?? '') == 'Inactive' ? 'selected' : '' }}>Nonaktif</option>
                                    </select>
                                    @if ($errors->has('status'))
                                    <span class="text-danger">{{ $errors->first('status') }}</span>
                                    @endif
                                </div>
                                <div class="col-sm-12">
                                    <div class="card card-outline card-warning">
                                        <div class="card-header">
                                            <h3 class="card-title">Hak Akses Khusus</h3>
                                            <div class="card-tools">
                                                <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                                                    <i class="fas fa-minus"></i>
                                                </button>
                                            </div>
                                        </div>
                                        <div class="card-body">
                                            <div class="form-check">
                                                <input 
                                                    class="form-check-input" 
                                                    type="checkbox" 
                                                    name="flag_backdate_so" 
                                                    id="flag_backdate_so" 
                                                    value="1" 
                                                    {{ old('flag_backdate_so', $data->flag_backdate_so ?? false) ? 'checked' : '' }}>
                                                <label class="form-check-label font-weight-bold" for="flag_backdate_so">
                                                    <i class="fas fa-calendar-alt text-warning"></i> Izinkan Backdate Stock Opname
                                                </label>
                                                <small class="form-text text-muted">
                                                    User dengan akses ini dapat membuat Stock Opname dengan tanggal sebelum hari ini (backdate).
                                                </small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-12">
                                    <div class="card card-outline card-warning">
                                        <div class="card-header">
                                            <h3 class="card-title">Akses Cabang</h3>

                                            <div class="card-tools">
                                                <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                                                    <i class="fas fa-minus"></i>
                                                </button>
                                            </div>
                                        </div>
                                        <div class="card-body">
                                            <div class="form-group">
                                                <div class="mb-2">
                                                    <button type="button" class="btn btn-sm btn-success" id="selectAllStores">
                                                        <i class="fas fa-check-double"></i> Pilih Semua
                                                    </button>
                                                    <button type="button" class="btn btn-sm btn-secondary" id="deselectAllStores">
                                                        <i class="fas fa-times"></i> Hapus Semua
                                                    </button>
                                                    <small class="text-muted ml-2">
                                                        <span id="selectedStoresCount">0</span> dari <span id="totalStoresCount">{{ count($variables['stores']) }}</span> toko dipilih
                                                    </small>
                                                </div>
                                                <small class="text-info d-block mb-2">
                                                    <i class="fas fa-info-circle"></i> 
                                                    <strong>Tips:</strong> Gunakan Ctrl+Click untuk pilih multiple, Shift+Click untuk pilih range
                                                </small>
                                                <select 
                                                    required 
                                                    name="user_stores[]" 
                                                    id="user_stores" 
                                                    class="form-control" 
                                                    multiple 
                                                    size="15"
                                                    style="height: 300px;">
                                                    @foreach($variables['stores'] as $comb)
                                                    <option value="{{ $comb->id }}" {{ (is_array(old('user_stores', $variables['user_stores'] ?? [])) && in_array($comb->id, old('user_stores', $variables['user_stores'] ?? []))) ? 'selected' : '' }}>
                                                        {{ $comb->code }} - {{ $comb->name }}
                                                    </option>
                                                    @endforeach
                                                </select>
                                            </div>
                                        </div>
                                        <!-- /.card-body -->
                                    </div>
                                    <!-- /.card -->
                                </div>
                            </div>
                            @endif
                            <button type="submit" class="btn btn-outline-info btn-sm button-cek-permission" data-permission="users.store"><i class="fas fa-save"></i> Simpan</button>
                            <a href="{{ $data->id ? route('users.show',$data->id) : route('users.index') }}" class="btn btn-outline-warning btn-sm" data='back' onclick="return notificationBeforeBack(event, this);"><i class="fas fa-arrow-left"></i> Kembali</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
/* Custom styling for native multi-select */
#user_stores {
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    padding: 0.375rem;
}

#user_stores option {
    padding: 8px 12px;
    border-radius: 3px;
    margin-bottom: 2px;
    cursor: pointer;
}

#user_stores option:hover {
    background-color: #e9ecef;
}

#user_stores option:checked {
    background: linear-gradient(#ffc107, #ffc107);
    color: #000;
    font-weight: 500;
}

/* Firefox specific */
@-moz-document url-prefix() {
    #user_stores option:checked {
        background-color: #ffc107 !important;
        color: #000 !important;
    }
}
</style>

<script>
$(document).ready(function() {
    // Update selected count on load
    updateSelectedCount();
    
    // Update count when selection changes
    $('#user_stores').on('change', function() {
        updateSelectedCount();
    });
    
    // Select All button
    $('#selectAllStores').on('click', function() {
        $('#user_stores option').prop('selected', true);
        updateSelectedCount();
    });
    
    // Deselect All button
    $('#deselectAllStores').on('click', function() {
        $('#user_stores option').prop('selected', false);
        updateSelectedCount();
    });
    
    // Function to update selected count
    function updateSelectedCount() {
        var selectedCount = $('#user_stores option:selected').length;
        $('#selectedStoresCount').text(selectedCount);
        
        // Update button states
        if (selectedCount === 0) {
            $('#deselectAllStores').prop('disabled', true);
        } else {
            $('#deselectAllStores').prop('disabled', false);
        }
        
        var totalCount = $('#user_stores option').length;
        if (selectedCount === totalCount) {
            $('#selectAllStores').prop('disabled', true);
        } else {
            $('#selectAllStores').prop('disabled', false);
        }
    }
});
</script>
@endsection