@extends('layouts.app')
@section('title','Test Item Option Import')
@section('pages')
<style type="text/css">
    .breadcrumb-title {
      border-right: none;
    }
    .card {
        margin-bottom: 0rem;
    }
    .form-control {
        height: 33px !important;
    }
</style>

    <!--breadcrumb-->
    <div class="page-breadcrumb row align-items-center justify-content-between mb-3 px-3 px-md-0">
      <div class="breadcrumb-title col-md-4"> Test Test Item Options Import </div>
        <div class="col-md-8">
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb justify-content-md-end mb-0">
                <li class="breadcrumb-item"><i class="bi bi-info-circle-fill text-info show-help-support" data-id="75" data-bs-toggle="modal" data-bs-target="#commonModal"></i></li>
                <li class="breadcrumb-item" aria-current="page">Test Manager</li>
                <li class="breadcrumb-item active" aria-current="page"><i class="bi bi-dot"></i> Test Package</li>
                <li class="breadcrumb-item active" aria-current="page"><i class="bi bi-dot"></i> Test Package Group</li>
                <li class="breadcrumb-item active" aria-current="page"><i class="bi bi-dot"></i> Test Item</li>
                <li class="breadcrumb-item active" aria-current="page"><i class="bi bi-dot"></i> Test Item Option</li>
                <li class="breadcrumb-item active" aria-current="page"><i class="bi bi-dot"></i> Upload Excel</li>
              </ol>
            </nav>
        </div>
    </div>
    <!--end breadcrumb-->

    <!-- main -->
    <div class="card style_table shadow-none radius-5 min-height">

        <div class="card-header py-3">

            @include('layouts.partials.nav-menu')

            <div class="row gx-2">

                <div class="col-lg-12 col-md-12">
                    <div class="ms-auto float-md-end">

                        <div class="btn-group">
                            <a href="javascript:history.back()" class="btn btn-outline-secondary me-1"><i class="bx bx-arrow-back ms-0 me-1"></i>Back</a>
                        </div>

                        @if(auth()->user()->can('test-item-option-export'))
                            <div class="btn-group">
                                <a href="{{ route('test-item-options.export-format') }}" class="btn btn-success"><i class="bi bi-download"></i> Download Excel Format</a>
                            </div>
                        @endif
                    </div>
              </div>
            </div>

        </div>

        <input type="hidden" name="hidden_page" id="hidden_page" value="1"/>
        <input type="hidden" name="hidden_column_name" id="hidden_column_name" value="name"/>
        <input type="hidden" name="hidden_sort_type" id="hidden_sort_type" value="asc"/>
    </div>
    <!-- end main -->

    <div class="row auto">
        <div class="col-xl-12 mx-auto">
            <div class=" shadow-none radius-5 min-height">
                <div class="">
                    <div class="">
                        <form class="row g-3 auto-width form" action="{{ route('test-item-options.import-store', $item_id) }}" enctype="multipart/form-data" method="POST">
                            @csrf
                            @method('post')
                            <div class="col-md-12">
                                <div class="card">
                                    <div class="card-body g-3 row">
                                        <h5 class="mb-0">Upload Options in {{ $item_name->name }} Item</h5>
                                        <hr class="mb-0">

                                        <div class="col-md-12">
										    <label for="users">Upload Excel File
										    	<span class="text-danger"><b>*</b></span>
										    </label>

										    <input type="file" class="form-control import_excel" name="test_item_options"/>
										    </div>
										    <span class="text-danger error_import" style="display:none;">This field is required.</span>
										</div>

                                        <div class="col-12 d-flex justify-content-center mb-3">
                                            <a style="width: 50%;" class="btn btn-primary import_button">
                                                <i class="bi bi-upload "></i> Upload
                                            </a>
                                        </div>

                                        <div class="col-12 justify-content-center mb-3">
                                            <div style="" class="accordion-item">
                                                <h2 class="accordion-header" id="panelsStayOpen-headingTwo">
                                                  <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseTwo" aria-expanded="false" aria-controls="panelsStayOpen-collapseTwo">
                                                    Guidelines For Options Upload:
                                                  </button>
                                                </h2>
                                                <div style="" id="panelsStayOpen-collapseTwo" class="accordion-collapse collapse" aria-labelledby="panelsStayOpen-headingTwo">
                                                  <div class="accordion-body">
                                                    <strong>Note:</strong><br>
                                                    <p>Duplicate record(s) will be skipped. <br>
                                                    If a record(s) already exists, only that record(s) will be skipped.<br>All fields are required.</p>

                                                    <strong>1. Option Name (required):</strong><br>
                                                    <p>Enter the Option Name.</p>
                                                      
                                                    <strong>2. Style Type (required):</strong><br>
                                                    <p>Enter the Style Type as either "Normal," "Low," or "High." Only these types are allowed.</p>
                                                      
                                                    <strong>3. Status (optional):</strong><br>
                                                    <p>Specify the status as either "Active" or "Inactive," if applicable.</p>
                                                </div>
                                            </div>
                                        </div>
                                        
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
<script>

$(document).on('change', '.import_excel', function(){

	var ext = this.value.match(/\.(.+)$/)[1];

    if(ext != 'xls' && ext != 'xlsx'){
        toastr.error('File must be type of xls & xlsx.');
        this.value = '';
    }
    
	const size = (this.files[0]).size;
         
    if (size > 5e+6) {
        toastr.error('File must be less than 5 MB');
        this.value = '';
    }

	var formData = new FormData($('.form')[0]);

    $('#commonModalLabel').text('Excel File Preview');
    $('.save_button_span').text('Close');
    $('.bx-save').switchClass('bx bx-save', 'bi bi-x-lg');
    $('.save_button').attr('data-bs-dismiss','modal');
    $('.modal-dialog').css('max-width', '1000px')
    $.ajax({
        url:"{!! route('test-item-options.import-preview') !!}",
        type:'POST',
       	data: formData,
       	contentType: false,
      	processData: false,
        success:function(data){
            if (data.success === false){
                $('.import_excel').val('');
                toastr.error(data.message);
            } else {
                $('#commonModal').modal('show');
                $('#commonModal').find('.modal-body').html('');
                $('#commonModal').find('.modal-body').html(data);
            }
        }
    });
});

$(document).on('click', '.import_button', function(event){
	$('.error_import').hide();
	var error = 0;

	if ($('input[name="test_item_options"]').val() == '') {
		$('.error_import').show();
		error = 1;
	}

	if (error == 0) {
		$('.form').submit();
	}

	error = 1;
});

</script>
@endsection