@extends('template.app')

@section('meta')
@endsection

@section('title')
Offers & Discounts 
@endsection

@section('styles')
<link href="{{ asset('assets/css/dropify.min.css') }}" rel="stylesheet">
<link href="{{ asset('assets/css/sweetalert2.bundle.css') }}" rel="stylesheet">
@endsection

@section('content')
<div class="page-breadcrumb">
    <div class="row">
        <div class="col-7 align-self-center">
            <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Offers & Discounts Master</h4>
            <div class="d-flex align-items-center">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb m-0 p-0">
                        <li class="breadcrumb-item"><a href="{{ route('offer') }}" class="text-muted">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('offer') }}" class="text-muted">Offers & Discounts </a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">View</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('offer.view') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('GET')

                        
                        <div class="row">
                            <div class="form-group col-sm-4">
                                <label for="tittle"> Tittle</label>
                                <input type="text" name="title" id="tittle" class="form-control" placeholder="Enter Tittle" value="{{ $data->title ??'' }}" disabled>
                                <span class="kt-form__help error tittle"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="start_date">Start date</label>
                                <input type="date" name="start_date" id="start_date" class="form-control" placeholder="Enter Start date" value="{{ !empty($data->start_date) ? \Carbon\Carbon::parse($data->start_date)->format('Y-m-d') : '' }}" disabled>
                                <span class="kt-form__help error start_date"></span>
                            </div>
                            <div class="form-group col-sm-4">
                                <label for="end_date">End date</label>
                                <input type="date" name="end_date" id="end_date" class="form-control" placeholder="Enter End date" value="{{ !empty($data->end_date) ? \Carbon\Carbon::parse($data->end_date)->format('Y-m-d') : '' }}" disabled>
                                <span class="kt-form__help error end_date"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="discount_year">Discount Year</label>
                                <select name="discount_year" id="discount_year" class="form-control" disabled>
                                    <option value="">Select Year</option>
                            
                                    @php
                                        $currentYear = date('Y');
                                    @endphp
                            
                                    @for ($i = $currentYear; $i >= $currentYear - 50; $i--)
                                        <option value="{{ $i }}" 
                                            {{ (old('discount_year', $data->discount_year ?? '') == $i) ? 'selected' : '' }}>
                                            {{ $i }}
                                        </option>
                                    @endfor
                                </select>
                            
                                <span class="kt-form__help error discount_year"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="car_name">Branch</label>
                                <select class="form-control" name="branch_id[]" id="branch_id" multiple disabled>
                                    <option value="">Select Branch</option>
                                    @if(isset($branch) && isset($data->branch_ids))
                                        @php
                                            $branchData = array_map('intval', explode(',', $data->branch_ids));
                                        @endphp
                                    
                                        @foreach($branch as $que)
                                            <option value="{{ $que->id }}"
                                                {{ in_array((int)$que->id, $branchData, true) ? 'selected' : '' }}>
                                                {{ $que->name }}
                                            </option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error car_name"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="car_name">Car Model</label>
                                <select class="form-control" name="car_name" id="car_name" disabled>
                                    @if(isset($Carmodel_arr)) 
                                        @foreach($Carmodel_arr as $car_model)
                                           <option value="{{ $car_model->id }}" @if($data->car_model_id == $car_model->id) selected @endif > {{ $car_model->name }}</option>
                                        @endforeach
                                    @endif
                                </select>
                                        
                                 <span class="kt-form__help error car_name"></span>
                            </div>

                            <div class="form-group col-sm-4" id="fule_type">
                                <label for="fule_type">Fuel Type</label>  
                                <select class="form-control" name="fuel_type" id="fuel_type" disabled>
                                    <option value="{{ $data->fid }}"> {{ $data->ftitle }}</option>
                                    @if(isset($fuel_type)) 
                                        @foreach($fuel_type as $fuel)
                                            <option value="{{ $fuel->id }}" @if($data->fuel_id == $fuel->id) selected @endif > {{ $fuel->title }}</option>
                                        @endforeach
                                    @endif
                                 </select>
                                <span class="kt-form__help error fule_type"></span>
                            </div>
                             
                            <div class="form-group col-sm-4">
                                <label for="car_name">Car Variant</label>  
                                <select class="form-control" name="veriant[]" id="veriant" multiple disabled>
                                    <option value="">Select Car Variant</option>
                                    @if(isset($veriant) && isset($data->car_varient_id)) 
                                        @php
                                            $selectedVarients = explode(',', $data->car_varient_id); // Convert stored variant IDs into an array
                                        @endphp
                                        @foreach($veriant as $que)
                                            @if(in_array($que->id, $selectedVarients)) <!-- Only show selected variants -->
                                                <option value="{{ $que->id }}" selected>
                                                    {{ $que->name }}
                                                </option>
                                            @endif
                                        @endforeach
                                    @endif
                                </select>                                        
                                <span class="kt-form__help error car_name"></span>
                            </div>

                            <div class="form-group col-sm-4">
                                <label for="vin">Car VIN Number</label>  
                                <select class="form-control" name="vin[]" id="vin" multiple disabled>   
                                    @if(!empty($data->car_vin_number))
                                        @foreach(explode(',', $data->car_vin_number) as $vin)
                                            <option value="{{ trim($vin) }}" selected>
                                                {{ trim($vin) }}
                                            </option>
                                        @endforeach
                                    @endif
                                </select>                                          
                                 <span class="kt-form__help error vin"></span>
                            </div>

                            <div class="form-group col-sm-4">
                                <label for="description">Remarks</label>
                                <textarea name="description" row="3" id="description"  class="form-control" disabled> {{ $data->description ??'' }} </textarea>
                                <span class="kt-form__help error description"></span>
                            </div>
                        </div>
                        
                        <div class="row mt-5">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="kec_desgn">KEC</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="kec_discount">Amount</label>
                                <input type="text" name="kec_discount" id="kec_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->kec_discount ??'' }}" disabled>
                                <span class="kt-form__help error kec_discount"></span>
                            </div>
                        </div>
                        
                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="tl_desgn">Team Leader</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="tl_discount">Amount</label>
                                <input type="text" name="tl_discount" id="tl_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->tl_discount ??'' }}" disabled>
                                <span class="kt-form__help error tl_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="sm_desgn">Sales Manager</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="sm_discount">Amount</label>
                                <input type="text" name="sm_discount" id="sm_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->sm_discount ??'' }}" disabled>
                                <span class="kt-form__help error sm_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="gm_desgn">General Manager</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="gm_discount">Amount</label>
                                <input type="text" name="gm_discount" id="gm_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->gm_discount ??'' }}" disabled>
                                <span class="kt-form__help error gm_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="bh_desgn">Business Head</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="bh_discount">Amount</label>
                                <input type="text" name="bh_discount" id="bh_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->bh_discount ??'' }}" disabled>
                                <span class="kt-form__help error bh_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="ceo_desgn">CEO</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="ceo_discount">Amount</label>
                                <input type="text" name="ceo_discount" id="ceo_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->ceo_discount ??'' }}" disabled>
                                <span class="kt-form__help error ceo_discount"></span>
                            </div>
                        </div>
                        
                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="md_desgn">Managing Director</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="md_discount">Amount</label>
                                <input type="text" name="md_discount" id="md_discount" class="form-control" placeholder="Enter Amount" value="{{ $data->md_discount ??'' }}" disabled>
                                <span class="kt-form__help error md_discount"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <a href="{{ route('discount') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script src="{{ asset('assets/js/promise.min.js') }}"></script>
<script src="{{ asset('assets/js/sweetalert2.bundle.js') }}"></script>

<script>
    $(document).ready(function() {
        var form = $('#form');
        $('.kt-form__help').html('');
        form.submit(function(e) {
            $('.help-block').html('');
            $('.m-form__help').html('');
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: new FormData($(this)[0]),
                dataType: 'json',
                async: false,
                processData: false,
                contentType: false,
                success: function(json) {
                    return true;
                },
                error: function(json) {
                    if (json.code === 422) {
                        e.preventDefault();
                        var errors_ = json.responseJSON;
                        $('.kt-form__help').html('');
                        $.each(errors_.errors, function(key, value) {
                            $('.' + key).html(value);
                        });
                    }
                }
            });
        });
    });
</script>
@endsection