@extends('layouts.company')
@section('content')
<!-- start page title -->
<div class="row">
    <div class="col-12">
        <div class="page-title-box d-sm-flex align-items-center justify-content-between bg-galaxy-transparent">
            <h4 class="mb-sm-0">Create Target Type</h4>

            <div class="page-title-right">
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Home</a></li>
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Target Type</a></li>
                    <li class="breadcrumb-item active">Create Target Type</li>
                </ol>
            </div>
        </div>
    </div>
</div>
<!-- end page title -->

<div class="row">
    <div class="col-lg-12">
        <div class="card" id="orderList">
            <div class="card-body">
                <form class="tablelist-form" autocomplete="off" name="targetTypeForm" id="targetTypeForm" method="post" novalidate enctype="multipart/form-data" action="{{ route('company.target_types.store') }}">
                <input type="hidden" name="id" id="id" value="{{ (!empty($targettype->id)) ? $targettype->id : '' }}" />
                    <div class="row">
                        <div class="col-lg-4 mb-3">
                            <label for="customername-field" class="form-label">Target Type</label>
                            <input type="text" id="name" name="name" class="form-control" placeholder="Enter Target Type" required value="{{ (!empty($targettype->name)) ? $targettype->name : '' }}" />
                        </div>
                        

                        <div class="col-lg-4 mb-3">
                            <label for="customername-field" class="form-label">Status</label>
                            <select class="form-select js-example-basic-single" data-placeholder="Select Status" id="status" name="status">
                                <option value=""></option>
                                <option value="1" {{ (!empty($targettype->status) && $targettype->status == 1) ? 'selected' : '' }}>Active</option>
                                <option value="0" {{ (!empty($targettype->status) && $targettype->status == 0) ? 'selected' : '' }}>Inactive</option>
                            </select>
                        </div>
                    </div>

                    <div class="hstack gap-2 justify-content-end">
                        <button type="submit" class="btn btn-success">{{ (!empty($targettype->id)) ? 'Update Target Type' : 'Create Target Type' }}</button>
                        <!-- <button type="button" class="btn btn-success" id="edit-btn">Update</button> -->
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@endsection
@section('script')
    <script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/additional-methods.min.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $("#targetTypeForm").validate({
                rules: {
                    name: {
                        required: true,
                        minlength: 3,
                    },
                    status: {
                        required: true,
                    },
                },
                messages: {
                    name: {
                        required: "Please enter the target type name.",
                        minlength: "target type name must be at least 3 characters.",
                    },
                    status: {
                        required: "Please select the status.",
                    },
                },
                errorPlacement: function (error, element) {
                    error.insertAfter(element); // Place error message after the input field
                },
                submitHandler: function (form) {
                    let formData = new FormData(form);

                    // AJAX submission
                    $.ajax({
                        url: form.action,
                        type: "POST",
                        data: formData,
                        processData: false,
                        contentType: false,
                        headers: {
                            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
                        },
                        success: function (response) {
                            // Show success message with Swal
                            Swal.fire({
                                title: "Success!",
                                text: response.message,
                                icon: "success",
                                confirmButtonText: "OK"
                            }).then(() => {
                                window.location.href = "{{ route('company.target_types') }}"; // Redirect to company list
                            });
                        },
                        error: function (xhr) {
                            if (xhr.status === 422) {
                                let errors = xhr.responseJSON.errors;

                                // Display validation errors from the server
                                $.each(errors, function (key, value) {
                                    let field = $(`[name="${key}"]`);
                                    field.addClass("error"); // Add error class to the field
                                    field.after(`<label class="error">${value[0]}</label>`); // Add error message
                                });
                            } else {
                                Swal.fire({
                                    title: "Error!",
                                    text: "Something went wrong. Please try again later.",
                                    icon: "error",
                                    confirmButtonText: "OK"
                                });
                            }
                        }
                    });

                    return false; // Prevent default form submission
                }
            });
        });
    </script>
    
@endsection
