@extends('layouts.app')
@section('title','Update Employee | ')
@section('content')
<!-- Content Header (Page header) -->
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0">Update Employee</h1>
            </div><!-- /.col -->
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="#">Home</a></li>
                    <li class="breadcrumb-item active">Employee</li>
                </ol>
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<section class="content">
    <div class="container-fluid">

        <div class="card card-default">
            <div class="card-header">
                <h3 class="card-title">Update Employee Details</h3>
                <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-card-widget="collapse">
                        <i class="fas fa-minus"></i>
                    </button>
                </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <strong>{{ session('success') }}</strong>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                @endif
                @if(session('error'))
                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                        <strong>{{ session('error') }}</strong>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                @endif
                @if(Auth::user()->user_role == 1 || Auth::user()->user_role == 2 || Auth::user()->user_role == 3)
                <form action="{{ route('update.employee') }}" method="post" autocomplete="off">
                    @csrf
                    <input type="hidden" name="id" value="{{ $userDetails->id }}">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="EmpFullname">Full Name</label>
                                <input type="text" name="full_name"
                                    class="form-control   @if( $errors->has('full_name')) is-invalid border-danger  @endif"
                                    value="{{ $userDetails->full_name }}" id="EmpFullname"
                                    placeholder="eg. Mohammad Shaan">
                                @if($errors->has('full_name'))
                                    <span id="EmpFullname-error"
                                        class="error text-danger invalid-feedback">{{ $errors->first('full_name') }}</span>
                                    <small></small>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="EmpEmail">Email</label>
                                <input type="email" name="email"
                                    class="form-control @if( $errors->has('email')) is-invalid border-danger  @endif"
                                    id="EmpEmail" value="{{ $userDetails->email }}"
                                    placeholder="eg. shaan@example.com">
                                @if($errors->has('email'))
                                    <span id="EmpFullname-error"
                                        class="error text-danger invalid-feedback">{{ $errors->first('email') }}</span>
                                    <small></small>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="EmpMobile">Mobile</label>
                                <input type="number" name="mobile_number"
                                    class="form-control @if( $errors->has('mobile_number')) is-invalid border-danger  @endif"
                                    id="EmpMobile" value="{{ $userDetails->mobile_number }}"
                                    placeholder="Enter Mobile"  min="6000000001" max="9999999999" readonly>
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" name="update_mobile" value="1" type="checkbox" id="update_mobile" {{ (old('update_mobile') == '1') ? 'checked' : '' }}>
                                    <label for="update_mobile" class="custom-control-label font-weight-bold text-secondary">Do you want to update mobile number?</label>
                                </div>
                                @if($errors->has('mobile_number'))
                                    <span id="mobile_number-error"
                                        class="error text-danger invalid-feedback">{{ $errors->first('mobile_number') }}</span>
                                    <small></small>
                                @endif
                            </div>
                        </div>
                        @if(Auth::user()->user_role == 1 || Auth::user()->user_role == 2)
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="user_role">User Role</label>
                                <select name="user_role"
                                    class="custom-select form-control @if( $errors->has('user_role')) is-invalid border-danger  @endif"
                                    id="user_role">
                                    <option value="">Select Employee Role</option>
                                    <option value="2" @if($userDetails->user_role == 2) selected @endif>Admin/HR</option>
                                    <!-- <option value="3" @if($userDetails->user_role == 3) selected @endif>Team Lead</option> -->
                                    <option value="4" @if($userDetails->user_role == 4) selected @endif>Employee</option>
                                </select>
                                @if($errors->has('user_role'))
                                    <span id="user_role-error"
                                        class="error text-danger invalid-feedback">{{ $errors->first('user_role') }}</span>
                                    <small></small>
                                @endif
                            </div>
                        </div>
                        @endif
                        @if(Auth::user()->user_role == 1 || Auth::user()->user_role == 2)
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="district">District</label>
                                <select name="district" class="custom-select form-control @if( $errors->has('district')) is-invalid border-danger  @endif" id="district">
                                @if($districts->count() > 0)
                                <option value="">Select District</option>
                                @foreach($districts as $distict)
                                    <option value="{{ $distict->district_id }}"  @if($userDetails->district_id == $distict->district_id) selected @endif>{{ $distict->district_name }}</option>
                                @endforeach  
                                @endif
                                </select>
                                @if ($errors->has('district'))
                                <span id="district-error" class="error text-danger invalid-feedback">{{ $errors->first('district') }}</span>
                                <small></small>
                                @endif
                            </div>
                        </div>
                        @endif
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="employeeStatus">Status</label>
                                <select name="employee_status"
                                    class="custom-select form-control @if( $errors->has('employee_status')) is-invalid border-danger  @endif"
                                    id="employeeStatus">
                                    <option value="0" @if($userDetails->emp_status =='0')
                                        {{ __('selected') }} @endif>Inactive</option>
                                    <option value="1" @if($userDetails->emp_status =='1')
                                        {{ __('selected') }} @endif>Active</option>
                                </select>
                                @if($errors->has('employee_status'))
                                    <span id="employeeStatus-error"
                                        class="error text-danger invalid-feedback">{{ $errors->first('employee_status') }}</span>
                                    <small></small>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <a href="{{ route('list.all.employees') }}" class="btn btn-secondary">Back</a>
                                <button type="submit" class="btn btn-primary">Update Employee</button>
                            </div>
                        </div>
                    </div>
                </form>
                @else 
                    <p class="text-info">Permission denied: You don't have access permission to access this page</p>
                @endif
            </div>
        </div>
        <!-- /.card -->
    </div>
    <!--/. container-fluid -->
</section>
<!-- /.content -->

@stop
@push('js')
<script>

$('#update_mobile').click(function(){
    let tag = $('#EmpMobile');
    let mobile_tag = document.getElementById('EmpMobile');
    let hasAttr = mobile_tag.hasAttribute('readonly');
    if(hasAttr) {
        mobile_tag.removeAttribute('readonly');
    } else {
        mobile_tag.setAttribute("readonly","readonly");
    }
});
@if(old('update_mobile') == '1')
$('#update_mobile').ready(function(){
   
    let mobile_tag = document.getElementById('EmpMobile');
    let hasAttr = mobile_tag.hasAttribute('readonly');
    if(hasAttr) {
        mobile_tag.removeAttribute('readonly');
    } else {
        mobile_tag.setAttribute("readonly","readonly");
    }
});
@endif
</script>

@endpush
