@extends('layouts.admin')
@section('css')
    <style>
        a {
            color: #000000;
        }
    </style>
@endsection
@section('content')
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1>Information</h1>
                    </div>
                    <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">Information</li>
                        </ol>
                    </div>
                </div>
            </div><!-- /.container-fluid -->
        </section>

        <!-- Main content -->
        <section class="content">
            <!-- Default box -->
            <div class="card">
                <div class="card-body">
                    <div class="d-grid gap-2 d-md-flex mb-3">
                        <a class="btn btn-success btn-sm" href="javascript:void(0)" id="createData"> <i
                                class="fa fa-plus"></i> Create</a>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-sm table-hover text-nowrap data-table w-100">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Name</th>
                                    <th>Start At</th>
                                    <th>Expired At</th>
                                    <th width="100px">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- /.card-body -->
            </div>
            <!-- /.card -->

        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <div class="modal fade" id="formModal1" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="formModalHeading1"></h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="dataForm1" name="dataForm1" class="form-horizontal">
                        <input type="hidden" name="id" id="id">
                        @csrf
                        <div class="alert alert-danger print-error-msg" style="display:none">
                            <ul></ul>
                        </div>

                        <div class="row">
                            <div class="col-12">
                                <div class="form-group">
                                    <label for="phone" class="col-sm-12 control-label">Category</label>
                                    <div class="col-sm-12">
                                        <select class="form-control" name="category" id="category">
                                            <option value="success">Sucess</option>
                                            <option value="danger">Danger</option>
                                            <option value="warning">Warning</option>
                                            <option value="info">Info</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="form-group">
                                    <label for="name" class="col-sm-12 control-label">Name</label>
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" id="name" name="name"
                                            placeholder="Enter Name">
                                    </div>
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="form-group">
                                    <label for="description" class="col-sm-12 control-label">Description</label>
                                    <div class="col-sm-12">
                                        <textarea type="text" class="form-control" id="description" name="description" placeholder="Enter Description"
                                            rows="5"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="form-group">
                                    <label for="expires_at" class="col-sm-12 control-label">Start At</label>
                                    <div class="col-sm-12">
                                        <div class="row">
                                            <div class="col-6 col-sm-7">
                                                <input type="date" class="form-control" id="start_at" name="start_at"
                                                    placeholder="Enter Expired" value="{{ date('Y-m-d') }}">
                                            </div>
                                            <div class="col-6 col-sm-5">
                                                <input type="time" class="form-control" id="start_at_time"
                                                    name="start_at_time" placeholder="Enter Expired"
                                                    value="{{ date('H:i') }}">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="form-group">
                                    <label for="expires_at" class="col-sm-12 control-label">Expired At</label>
                                    <div class="col-sm-12">
                                        <div class="row">
                                            <div class="col-6 col-sm-7">
                                                <input type="date" class="form-control" id="expires_at"
                                                    name="expires_at" placeholder="Enter Expired"
                                                    value="{{ date('Y-m-d') }}">
                                            </div>
                                            <div class="col-6 col-sm-5">
                                                <input type="time" class="form-control" id="expires_at_time"
                                                    name="expires_at_time" placeholder="Enter Expired"
                                                    value="{{ date('H:i') }}">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="row mx-1">
                                    <div class="col-sm-12">
                                        <div class="form-check">
                                            <input type="checkbox" class="form-check-input" name="is_product"
                                                id="is_product">
                                            <label class="form-check-label control-label" for="is_product">Show in
                                                Product</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12">
                                        <div class="form-check">
                                            <input type="checkbox" class="form-check-input" name="is_pricing"
                                                id="is_pricing">
                                            <label class="form-check-label control-label" for="is_pricing">Show in
                                                Pricing</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12">
                                        <div class="form-check">
                                            <input type="checkbox" class="form-check-input" name="is_blog"
                                                id="is_blog">
                                            <label class="form-check-label control-label" for="is_blog">Show in
                                                Blog</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12">
                                        <div class="form-check">
                                            <input type="checkbox" class="form-check-input" name="is_billing"
                                                id="is_billing">
                                            <label class="form-check-label control-label" for="is_billing">Show in
                                                Billing</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12">
                                        <div class="form-check">
                                            <input type="checkbox" class="form-check-input" name="is_info"
                                                id="is_info">
                                            <label class="form-check-label control-label" for="is_info">Show in
                                                Information</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <button type="submit" class="btn btn-success mt-2"
                                            id="saveBtn">Submit</button>
                                        <button type="button" class="btn btn-danger mt-2" id="cancelBtn"
                                            data-dismiss="modal"><i class="fa fa-cancel"></i>
                                            Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('js')
    <script>
        $(function() {
            var table = $('.data-table').DataTable({
                processing: true,
                serverSide: true,
                ajax: "{{ route('admin.information') }}",
                columns: [{
                        data: 'DT_RowIndex',
                        name: 'DT_RowIndex',
                        orderable: false,
                        searchable: false
                    },
                    {
                        data: 'name',
                        name: 'name'
                    },
                    {
                        data: 'start_at',
                        name: 'start_at'
                    },
                    {
                        data: 'expires_at',
                        name: 'expires_at'
                    },
                    {
                        data: 'action',
                        name: 'action',
                        orderable: false,
                        searchable: false
                    },
                ]
            });

            $('#createData').click(function() {
                $('#id').val('');
                $('#dataForm1').trigger("reset");
                $('#formModalHeading1').html("<i class='fa fa-plus'></i> New Information");
                $('#formModal1').modal('show');
            });

            $('body').on('click', '.editData', function() {
                var id = $(this).data('id');
                $('#dataForm1').find(".print-error-msg").find("ul").html('');
                $('#dataForm1').find(".print-error-msg").css('display', 'none');
                $.get("{{ route('admin.information-show', '') }}/" + id, function(data) {
                    $('#formModalHeading1').html(
                        "<i class='fa-regular fa-pen-to-square'></i> Edit Data");
                    $('#formModal1').modal('show');
                    $('#id').val(data.response.id);
                    $('#name').val(data.response.name);
                    $('#description').val(data.response.description);
                    $('#start_at').val(data.response.start_at.substring(0, 10));
                    $('#start_at_time').val(data.response.start_at.substring(11, 16));
                    $('#expires_at').val(data.response.expires_at.substring(0, 10));
                    $('#expires_at_time').val(data.response.expires_at.substring(11, 16));
                    $('#category').val(data.response.category).change();
                    if (data.response.is_product) {
                        $('#is_product').attr('checked', '');
                    } else {
                        $('#is_product').removeAttr('checked');
                    }
                    if (data.response.is_pricing) {
                        $('#is_pricing').attr('checked', '');
                    } else {
                        $('#is_pricing').removeAttr('checked');
                    }
                    if (data.response.is_blog) {
                        $('#is_blog').attr('checked', '');
                    } else {
                        $('#is_blog').removeAttr('checked');
                    }
                    if (data.response.is_billing) {
                        $('#is_billing').attr('checked', '');
                    } else {
                        $('#is_billing').removeAttr('checked');
                    }
                    if (data.response.is_info) {
                        $('#is_info').attr('checked', '');
                    } else {
                        $('#is_info').removeAttr('checked');
                    }
                })
            });

            $('#dataForm1').submit(function(e) {
                e.preventDefault();
                $('#dataForm1').find(".print-error-msg").find("ul").html('');
                $('#dataForm1').find(".print-error-msg").css('display', 'none');
                let formData = new FormData(this);
                $('#saveBtn').html('Sending...');
                $.ajax({
                    type: 'POST',
                    url: "{{ route('admin.information-store') }}",
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: (response) => {
                        $('#saveBtn').html('Submit');
                        $('#dataForm1').trigger("reset");
                        $('#formModal1').modal('hide');
                        table.draw();
                    },
                    error: function(response) {
                        $('#saveBtn').html('Submit');
                        $('#dataForm1').find(".print-error-msg").find("ul").html('');
                        $('#dataForm1').find(".print-error-msg").css('display', 'block');
                        $.each(response.responseJSON.errors, function(key, value) {
                            $('#dataForm1').find(".print-error-msg").find("ul")
                                .append('<li>' + value + '</li>');
                        });
                    }
                });

            });

            $('body').on('click', '.deleteData', function() {
                var id = $(this).data("id");
                var isConfirmed = confirm("Are You sure want to delete?");
                if (isConfirmed) {
                    $.ajax({
                        type: "DELETE",
                        url: "{{ route('admin.information-delete', '') }}/" + id,
                        success: function(data) {
                            table.draw();
                        },
                        error: function(data) {
                            console.log('Error:', data);
                        }
                    });
                }
            });

        });
    </script>
@endsection
