@include("messages")
<?php
$required = "";
$types = Config::get('params.packages_types');
$currency = Config::get('params.currency');
$size = Config::get('params.best_boiler_image_size');

$action = "packages";
?>

<div class="row">
    <ul id="tabsJustified" class="nav nav-tabs">
        <li class="nav-item"><a href="#_" data-target="#information" data-toggle="tab" class="nav-link small text-uppercase active">Information</a></li>
        <li class="nav-item"><a href="#_" data-target="#prices" data-toggle="tab" class="nav-link small text-uppercase ">Prices</a></li>
        <li class="nav-item"><a href="#_" data-target="#facilities" data-toggle="tab" class="nav-link small text-uppercase ">What Covered</a></li>
        <li class="nav-item"><a href="#_" data-target="#meta" data-toggle="tab" class="nav-link small text-uppercase ">Meta</a></li>
        <li class="nav-item"><a href="#_" data-target="#not-covered" data-toggle="tab" class="nav-link small text-uppercase ">Not Covered</a></li>
        <li class="nav-item"><a href="#_" data-target="#download" data-toggle="tab" class="nav-link small text-uppercase ">Download</a></li>
        <li class="nav-item"><a href="#_" data-target="#faqs" data-toggle="tab" class="nav-link small text-uppercase ">Faqs</a></li>
        <li class="nav-item"><a href="#_" data-target="#extra" data-toggle="tab" class="nav-link small text-uppercase ">Extra</a></li>
    </ul>
</div>

<div id="tabsJustifiedContent" class="tab-content">
    <div id="information" class="tab-pane fade active show">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    {!! Form::label('Title') !!}
                    {!! Form::text('title', null , array('class' => 'form-control',$required) ) !!}
                </div>
            </div>

            <div class="col-sm-6">
                <div class="form-group">
                    {!! Form::label('Categories') !!}
                    {!! Form::select('category_id', $categories,null,['class' => 'form-control','name'=>'category_id',$required]) !!}
                </div>
            </div>

            <div class="col-sm-12">
                <div class="form-group">
                    {!! Form::label('tagline') !!}
                    {!! Form::text('tagline', null , array('class' => 'form-control',$required) ) !!}
                </div>
            </div>
            <div class="col-sm-12">
                <div class="form-group">
                    {!! Form::label('tagline 2') !!}
                    {!! Form::text('tagline2', null , array('class' => 'form-control',$required) ) !!}
                </div>
            </div>
            <div class="col-sm-12">
                <div class="form-group">
                    {!! Form::label('URL') !!}
                    {!! Form::text('url', null , array('class' => 'form-control',$required) ) !!}
                </div>
            </div>
            <div class="col-sm-12">
                <div class="form-group">
                    {!! Form::label('shortDescription') !!}
                    {!! Form::textarea('shortDescription', null, ['class'=>'form-control ckeditor','id'=>'shortDescription', 'rows' => 1, 'cols' => 3, 'style' => '']) !!}
                </div>
            </div>
            <div class="col-sm-12">
                <div class="form-group">
                    {!! Form::label('description') !!}
                    {!! Form::textarea('description', null, ['class'=>'form-control ckeditor','id'=>'description', 'rows' => 3, 'cols' => 3, 'style' => '']) !!}
                </div>
            </div>

            <div class="col-sm-4">
                <div class="form-group">
                    {!! Form::label('position') !!}
                    {!! Form::text('position', null , array('class' => 'form-control',$required) ) !!}
                </div>
            </div>
            <div class="col-sm-4">
                <div class="d-flex justify-content-start flex-wrap">
                    <div class="custom-control custom-switch custom-switch-success mr-2 mb-1">
                        <p class="mb-0">Popular</p>
                        <input type="checkbox" 
                               @if(isset($model->popular) && $model->popular==1)
                               checked 
                               @endif
                               class="custom-control-input" name="popular" id="popular" value="1">
                               <label class="custom-control-label" for="popular"></label>
                    </div>
                </div>
            </div>

            <div class="col-sm-4">
                <div class="d-flex justify-content-start flex-wrap">
                    <div class="custom-control custom-switch custom-switch-success mr-2 mb-1">
                        <p class="mb-0">Status</p>
                        <input type="checkbox" 
                               @if(isset($model->status) && $model->status==1)
                               checked 
                               @endif
                               class="custom-control-input" name="status" id="status" value="1">
                               <label class="custom-control-label" for="status"></label>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">

            <div class="col-sm-12">
                <div class="row form-group">
                    <label class="col-xl-2 col-lg-2 col-form-label"> Image</label>
                    <div class="col-lg-6 col-xl-6">
                        <input   value="<?php echo isset($model->image) ? $model->image : ""; ?>"  placeholder="Upload Image" id="image" name="image" type="text" class="form-control "  readonly="">
                        <br clear="all"/>
                        Best Image Size(<?php echo $size; ?>)
                    </div>
                    <div class="col-lg-2 col-xl-2">
                        <button type="button" class="btn btn-warning btn-label-brand btn-md mediaModel" data-toggle="modal" data-target="#media-model" data-control="image">Browse</button>
                    </div>
                    <div class="col-lg-1 col-xl-1 ">

                        <p id="removeImage" class="btn btn-outline-warning mr-1 mb-1 waves-effect waves-light float-right">Remove</p>

                    </div>
                </div>
            </div>   
        </div>
    </div>

    <div id="prices" class="tab-pane fade">

        <div class="row">
            <div class="col-md-8">


                <?php
                foreach ($types as $key => $type) {
                    ?>

                    <h3><?php echo $type ?></h3>
                    <table cellspacing="0" class="table" id="media_gallery_content_grid" width="80%">
                        <thead>
                            <tr class="headings">
                                <th>Excess</th>
                                <th>Price(<?php echo $currency["symbol"] ?>)</th>
                                <th>VAT(%)</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody id="media_gallery_content_list">
                            <?php foreach ($excesses as $excess) { ?>

                                <tr class="body">
                                    <td><?php echo $excess->title ?></td>
                                    <td><input type="text" class="form-control" name="prices[<?php echo $key ?>][<?php echo $excess->id ?>][price]" value="<?php
                                        echo
                                        isset($prices[$key][$excess->id]["price"]) ? $prices[$key][$excess->id]["price"] : null
                                        ?>"></td>
                                    <td><input type="text" class="form-control" name="prices[<?php echo $key ?>][<?php echo $excess->id ?>][vat]" value="<?php
                                        echo
                                        isset($prices[$key][$excess->id]["price"]) ? $prices[$key][$excess->id]["vat"] : 20
                                        ?>"></td>
                                    <td>
                                        <?php if (isset($prices[$key][$excess->id]["price"]) && isset($prices[$key][$excess->id]["vat"])) { ?>

                                            <?php echo $currency["symbol"] ?> <?php
                                            $add = $prices[$key][$excess->id]["price"] * ($prices[$key][$excess->id]["vat"] / 100);
                                            $total = $prices[$key][$excess->id]["price"] + $add;
                                            echo numberformat($total, 2);
                                            ?>

                                        <?php } ?>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                <?php } ?>


            </div>
        </div>


    </div>

    <div id="facilities" class="tab-pane fade">

        <div class="row">
            <div class="col-md-12">

                <table cellspacing="0" class="table" id="media_gallery_content_grid" width="80%">
                    <thead>
                        <tr class="headings">
                            <th>Facilities</th>
                            <th>Effect on price</th>
                            <th>Price(<?php echo $currency["symbol"] ?>)</th>
                        </tr>
                    </thead>
                    <tbody id="media_gallery_content_list">
                        <?php
                        foreach ($facilities as $facility) {
                            $checked = "";
                            $priceChecked = "";
                            if (key_exists($facility->id, $packageFacilities)) {
                                $hasPrice = $packageFacilities[$facility->id]["has_price"];
                                $checked = "checked=checked";
                                if ($hasPrice == 1) {
                                    $priceChecked = "checked=checked";
                                }
                                $price = $packageFacilities[$facility->id]["price"];
                            } else {
                                $hasPrice = 0;
                                $price = null;
                            }
                            ?>
                            <tr class="body">
                                <td>
                                    <div class="custom-control custom-checkbox">
                                        <input type="checkbox" name="facilities[{{$facility->id}}][available]" class="custom-control-input facilities" id="facilities_{{$facility->id}}" value="{{$facility->id}}" {{$checked}}  >
                                        <label class="custom-control-label" for="facilities_{{$facility->id}}">{{$facility->title}}</label>
                                    </div>
                                </td>
                                <td>
                                    <div class="custom-control custom-checkbox">
                                        <input type="checkbox" name="facilities[{{$facility->id}}][has_price]" class="custom-control-input has-price" id="facilities_has_price_{{$facility->id}}"  value="1" {{$priceChecked}} >
                                        <label class="custom-control-label" for="facilities_has_price_{{$facility->id}}">Effect on price</label>
                                    </div>
                                </td>
                                <td>
                                    <label class="form-control-label" for="facilities_price_{{$facility->id}}">Price</label>
                                    <input type="text" name="facilities[{{$facility->id}}][price]" class="form-control-input price" id="facilities_price_{{$facility->id}}" value="<?php echo $price; ?>" >
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>


    <div id="meta" class="tab-pane fade">

        <div class="col-sm-12">
            <div class="form-group">
                {!! Form::label('metaTitle') !!}
                {!! Form::text('metaTitle', null , array('class' => 'form-control',$required) ) !!}
            </div>
        </div>
        <div class="col-sm-12">
            <div class="form-group">
                {!! Form::label('metaDescription') !!}
                {!! Form::textarea('metaDescription', null , array('class' => 'form-control','rows' => 3, 'cols' => 3, 'style' => '',$required) ) !!}
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                {!! Form::label('keywords') !!}
                {!! Form::text('keywords', null , array('class' => 'form-control',$required) ) !!}
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                {!! Form::label('tags') !!}
                {!! Form::text('tags', null , array('class' => 'form-control',$required) ) !!}
            </div>
        </div>

    </div>

    <div id="not-covered" class="tab-pane fade">
        <div class="col-sm-12">
            <div class="form-group">
                {!! Form::label('not Covered Title') !!}
                {!! Form::text('notCoveredTitle', null , array('class' => 'form-control',$required) ) !!}
            </div>
        </div>
        <div class="col-sm-12">
            <div class="form-group">
                {!! Form::label('not Covered Text') !!}
                {!! Form::textarea('notCoveredText', null , array('id' => 'notCoveredText','class' => 'form-control ckeditor','rows' => 3, 'cols' => 3, 'style' => '',$required) ) !!}
            </div>
        </div>

    </div>
    <div id="download" class="tab-pane fade">
        <div class="col-sm-12">
            <div class="form-group">
                {!! Form::label('File Download Text') !!}
                {!! Form::text('fileDownloadText', null , array('class' => 'form-control',$required) ) !!}
            </div>
        </div>

        @if($model->file)
        <div class="col-sm-6">
            <a class="btn btn-success mr-1 mb-1 waves-effect waves-light" href="{{url("/")}}/<?php echo Config("params.dir_packages_downloads"); ?>/<?php echo $model->file; ?>" target="_blank">Download</a>
        </div>
        @endif
        <div class="col-sm-6">
            <div class="form-group">
                {!! Form::label('file') !!}
                {!! Form::file('file', null,array('class'=>'form-control')) !!}
                <br clear="all"/>
                Only .pdf is allowed
            </div>
        </div>

    </div>
    <div id="extra" class="tab-pane fade">


        <div class="col-sm-12">
            <div class="form-group">
                {!! Form::label('Review text') !!}
                {!! Form::textarea('reviews', null , array('id' => 'reviews','class' => 'form-control ckeditor','rows' => 3, 'cols' => 3, 'style' => '',$required) ) !!}
            </div>
        </div>
    </div>

    <div id="faqs" class="tab-pane fade">
        @include("admin.common.faqs_form")
    </div>

    <br clear="all" />


    <div class="row">
        <div class="col-md-6">
            {!! Form::hidden('_token', csrf_token(),array("id"=>"_token")) !!}
            <button type="button" class="btn btn-primary mr-1 mb-1 waves-effect waves-light save">Save</button>
            <a href="{{url('admin/')}}/{{$action}}" class="btn btn-outline-warning mr-1 mb-1 waves-effect waves-light">Cancel</a>
        </div>
    </div>

</div>


@include('vendor_script')
@section('myscript')
{{-- Page js files --}}
<script>
    $(document).ready(function () {

        CKEDITOR.config.allowedContent = true;
        CKEDITOR.replace("ckeditor", {filebrowserImageBrowseUrl: '/file-manager/ckeditor',
        });

    });
        $( "#removeImage" ).click(function() {
            $("#image").val(" ");
            
          });

    $(".save").click(function () {

        var url = $("#form").attr('action');

        var type = "POST";
        var dataType = "json";
        var data = new FormData($('#form')[0]);
        data.append('description', CKEDITOR.instances['description'].getData());
        data.append('shortDescription', CKEDITOR.instances['shortDescription'].getData());
        data.append('notCoveredText', CKEDITOR.instances['notCoveredText'].getData());
        data.append('reviews', CKEDITOR.instances['reviews'].getData());

        var callBack = function (response) {
            if (response.error === 1) {
                displayErrors(response, "errors");
            } else
            {
                window.top.location.href = "<?php echo url("admin/$action") ?>";
            }
        };

        var errorCallBack = function (response) {
            alertMessage('success');
        };

        ajaxRequest(url, type, dataType, data, callBack, errorCallBack);

    });

</script>

@endsection