
<?php $__env->startSection('content'); ?>
    <div class="az-content-breadcrumb">
        <span>Purchase Order</span>
        <span>Purchase Order </span>
        
        
    </div>
    <h2 class="az-content-title" style="display: inline">Create PO <span><a href="<?php echo e(route('po.list')); ?>"
                class="btn btn-az-primary" style="float: right">Purchase Order List</a></span></h2>
    


    
    <div class="row">
        <div class="col-md-12 col-lg-12 col-xl-12">
            <div class="card card-body pd-40" style="height: 60%">
                <h5 class="card-title mg-b-20">Add Purchase Order</h5>
                <form method="post" action="<?php echo e(route('invoice.store')); ?>">
                    <?php echo csrf_field(); ?>
                    <?php if(count($errors) > 0): ?>
                        <div class="p-1">
                            <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="alert alert-warning alert-danger fade show" role="alert"><?php echo e($error); ?>

                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>
                    <?php endif; ?>



                    <div class="row row-sm mg-b-20">
                        <div class="row" style="height: 9rem" id="customer_div">
                            <div class="col-md-4 form-group mb-3">
                                <label for="serial_no">Search Supplier</label>
                                <select class="form-control select2" name="supplier_name" id="supplier_select">
                                    <option value="">Select</option>
                                    <?php $__currentLoopData = $supplier; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sup): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($sup->id_supplier); ?>"><?php echo e($sup->supplier_name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <div class="col-md-8 form-group">
                                <div class="card border-0 p-4 text-white" style="background-color: #00beda">
                                    <div class="row"><span><b>SUPPLIER INFO</b></span></div>
                                </div>
                                <br>
                                <div class="row" id="supplier_details">
                                    <div class="col-md-12">
                                        <p>Supplier Name: <span></span></p>
                                    </div>
                                    <div class="col-md-12">
                                        <p>Supplier Address: <span></span></p>
                                    </div>
                                    <div class="col-md-12">
                                        <p>Supplier Phone: <span></span></p>
                                    </div>
                                    <div class="col-md-12">
                                        <p>Supplier Email#: <span></span></p>
                                    </div>
                                </div>
                            </div>


                        </div>



                    </div>

            </div>


            <div class="card card-body pd-40 mt-5">
                <div class="row">
                    <div class="col-md-2 mt-3">
                        <label for="supplier_brands">Brands</label>
                        <select class="form-control select2" id="supplier_brands" name="supplier_brands[]">
                            <option value="">select</option>
                            <!-- Supplier brands will be dynamically added here -->
                        </select>
                    </div>
                    <div class="col-md-4 mt-3">
                        <label for="supplier_brands_products">Products</label>
                        <select class="form-control select2" id="supplier_brands_products"
                            name="supplier_brands_products[]">
                            <option value=""></option>
                            <!-- Supplier brands' products will be dynamically added here -->
                        </select>
                    </div>
                    <div class="col-md-2 mt-3">
                        <label for="supplier_brands">To Currency</label>
                        <select class="form-control select2" id="supplier_brasnds" name="supplier_brands[]">
                            <!-- Supplier brands will be dynamically added here -->
                        </select>
                    </div>
                    <div class="col-md-2 mt-3">
                        <label for="supplier_brands">Currency Rate </label>
                        <select class="form-control select2" id="supplier_bransds" name="supplier_brands[]">
                            <!-- Supplier brands will be dynamically added here -->
                        </select>
                    </div>
                    <div class="col-md-2 mt-4">
                        <button class="btn btn-az-primary btn-block mt-2">Add products <i
                                class="fas fa-arrow-down"></i></button>
                    </div>
                </div>
            </div>
            <a type="button" href="<?php echo e(route('po.list')); ?>" class="btn btn-danger btn-block mt-2">
                Cancel
            </a>
            <button type="submit" class="btn btn-az-primary btn-block mt-2" style="float: right">
                Submit
            </button>
            </form>
            <!-- card -->
        </div>
        <!-- col -->
    </div>


<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script>
        // $(document).ready(function() {
        //     $('#supplier_select').on('change', function() {
        //         var id = $(this).val();
        //         if (id) {
        //             $.ajax({
        //                 url: '<?php echo e(url('po/get_supplier_details')); ?>',
        //                 type: 'GET',
        //                 data: {
        //                     id: id
        //                 },
        //                 success: function(response) {
        //                     $('#supplier_details').html(response.html);
        //                 },
        //                 error: function(xhr, status, error) {
        //                     console.log(xhr.responseText);
        //                 }
        //             });
        //         } else {
        //             $('#supplier_details').html(`
    //         <div class="row">
    //             <div class="col-md-12">
    //                 <p>Supplier Name: <span></span></p>
    //             </div>
    //             <div class="col-md-12">
    //                 <p>Supplier Address: <span></span></p>
    //             </div>
    //             <div class="col-md-12">
    //                 <p>Supplier Phone: <span></span></p>
    //             </div>
    //             <div class="col-md-12">
    //                 <p>Supplier Email#: <span></span></p>
    //             </div>
    //         </div>
    //     `);
        //         }
        //     });
        // });
        $(document).ready(function() {
            // Initialize Select2 for supplier brands and products
            $('.select2').select2();

            $('#supplier_select').on('change', function() {
                var id = $(this).val();
                if (id) {
                    // Fetch supplier details
                    $.ajax({
                        url: '<?php echo e(url('po/get_supplier_details')); ?>',
                        type: 'GET',
                        data: {
                            id: id
                        },
                        success: function(response) {
                            $('#supplier_details').html(response.html);

                            // Fetch supplier brands
                            $.ajax({
                                url: '<?php echo e(url('po/get_supplier_brands')); ?>',
                                type: 'GET',
                                data: {
                                    id: id
                                },
                                success: function(brandsResponse) {
                                    $('#supplier_brands').html(brandsResponse.html);
                                    $('#supplier_brands')
                                .select2(); // Reinitialize Select2

                                    // Clear products dropdown initially
                                    $('#supplier_brands_products').empty();
                                },
                                error: function(xhr, status, error) {
                                    console.log('Error fetching brands:', xhr
                                        .responseText);
                                }
                            });
                        },
                        error: function(xhr, status, error) {
                            console.log('Error fetching supplier details:', xhr.responseText);
                        }
                    });
                } else {
                    $('#supplier_details').html(`
                <div class="row">
                    <div class="col-md-12">
                        <p>Supplier Name: <span></span></p>
                    </div>
                    <div class="col-md-12">
                        <p>Supplier Address: <span></span></p>
                    </div>
                    <div class="col-md-12">
                        <p>Supplier Phone: <span></span></p>
                    </div>
                    <div class="col-md-12">
                        <p>Supplier Email#: <span></span></p>
                    </div>
                </div>
            `);
                    $('#supplier_brands').empty();
                    $('#supplier_brands_products').empty();
                    $('.select2').select2(); // Clear and reinitialize Select2
                }
            });

            $('#supplier_brands').on('change', function() {
                var brandId = $(this).val();
                var supplierId = $('#supplier_select').val(); // Get the selected supplier ID
                if (brandId && supplierId) {
                    // Fetch products for the selected brand
                    $.ajax({
                        url: '<?php echo e(url('po/get_supplier_brands_products')); ?>',
                        type: 'GET',
                        data: {
                            brand_id: brandId,
                            supplier_id: supplierId
                        },
                        success: function(productsResponse) {
                            $('#supplier_brands_products').html(productsResponse.html);
                            $('#supplier_brands_products').select2(); // Reinitialize Select2
                        },
                        error: function(xhr, status, error) {
                            console.log('Error fetching products:', xhr.responseText);
                        }
                    });
                } else {
                    $('#supplier_brands_products').empty();
                }
            });
        });
    </script>
    <script>
        $(document).ready(function() {
            $('#supplier_select').select2({
                theme: 'default'
            });
        });
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\wamp64\www\mds-scm\resources\views/purchase_order/create.blade.php ENDPATH**/ ?>