<!--sidebar end-->
<!--main content start-->
<section id="main-content">
    <section class="wrapper site-min-height">
        <div class="col-md-12">
            <section class="panel">
                <header class="panel-heading">
                    <i class="fa fa-user"></i> <?= lang('production_wasted_details'); ?>
                </header>
                <div class="panel-body">
                    <div class="adv-table editable-table ">
                        <div class="clearfix">
                            <a href="<?php echo base_url('product/listProduct'); ?>">
                                <div class="btn-group">
                                    <button class="button button-primary">
                                        <i class="fa-solid fa-circle-arrow-left"></i>
                                    </button>
                                </div>
                            </a>
                            <button class="export" onclick="javascript:window.print();"><i class="fa-solid fa-print"></i> <?php echo lang('print'); ?></button>
                        </div>
                        <div class="space15">
                            <div class="row">
                                <div class="col-lg-12 col-md-12 col-sm-12">
                                    <div class="new__cards__auto card__box alert-success">
                                        <h4 class="text-center"><strong><?= lang('production_details'); ?> </strong></h4>
                                        <div class="col-xs-4">
                                            <table>
                                                <tr>
                                                    <td class="text-center" colspan="2">
                                                        <h5><strong><?= lang('product_information'); ?> </strong></h5>
                                                    </td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('product_name'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?= $productById->pr_name ?></p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('product_category'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $productCategory = $this->product_model->getProductCategoryById($productById->pr_prc_id)->prc_name; ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('shed'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?= $this->shed_model->getShedById($productAssignedById->pra_shed_id)->sh_no ?>: <?= $this->shed_model->getShedById($productAssignedById->pra_shed_id)->sh_title ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('batch'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($productAssignedById->pra_shed_id, $productAssignedById->pra_batch_id)->lshs_batch_id ?>: <?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($productAssignedById->pra_shed_id, $productAssignedById->pra_batch_id)->lshs_batch_title ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('production_unit'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php $unit = $this->settings_model->getUnitById($productById->pr_unit_id);
                                                                if ($unit) {
                                                                    echo  $unit->un_name;
                                                                }
                                                                ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('approx_selling_price_per_quantity'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?= $settings->currency; ?> <?= $productById->pr_selling_price; ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('note'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?= $productById->pr_description ?></p>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                        <!-- Product Wise Details -->
                                        <div class="col-xs-4">
                                            <table>
                                                <tr>
                                                    <td class="text-center" colspan="2">
                                                        <h5><strong><?= lang('product_wise_stock_information'); ?> </strong></h5>
                                                    </td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('total_production'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $totalProduction = $this->product_model->getTotalProductionQuantityByProductId($productById->pr_id);
                                                                $unit = $this->settings_model->getUnitById($productById->pr_unit_id);
                                                                if ($totalProduction) {
                                                                    if ($unit) {
                                                                        echo  ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>

                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('total_sold'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $totalSoldProduction = $this->sale_model->getProductSoldQuantityByProductId($productById->pr_id, 'prsv_quantity');
                                                                if ($totalSoldProduction) {
                                                                    if ($unit) {
                                                                        echo  ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('total_wasted'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $totalWastedProduction = $this->product_model->getTotalProductWastedQuantityByProductId($productById->pr_id);
                                                                if ($totalWastedProduction) {
                                                                    if ($unit) {
                                                                        echo  ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('production_in_stock'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php
                                                                echo $totalStockProduction = $totalProduction - $totalSoldProduction - $totalWastedProduction;
                                                                if ($totalStockProduction) {
                                                                    if ($unit) {
                                                                        echo  ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                        <!-- Shed and Batch Wise Production -->
                                        <div class="col-xs-4">
                                            <table>
                                                <tr>
                                                    <td class="text-center" colspan="2">
                                                        <h5><strong><?= lang('shed_and_batch_wise_stock_information'); ?> </strong></h5>
                                                    </td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('total_production'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $totalProductionSB = $this->product_model->getTotalProductionQuantityByShedAndBatchId($productAssignedById->pra_shed_id, $productAssignedById->pra_batch_id);
                                                                if ($totalProductionSB) {
                                                                    if ($unit) {
                                                                        echo ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('total_sold'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $totalSoldSB = $this->sale_model->getProductAssignedWiseProductSaleValue($productAssignedById->pra_id, 'prsv_quantity');
                                                                if ($totalSoldSB) {
                                                                    if ($unit) {
                                                                        echo ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>
                                                            <?php
                                                            ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('total_wasted'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php echo $totalWasteSB = $this->product_model->getTotalProductWastedQuantityByProductAssignId($productAssignedById->pra_id);
                                                                if ($totalWasteSB) {
                                                                    if ($unit) {
                                                                        echo  ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>

                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="table__padding">
                                                        <p><strong><?= lang('production_in_stock'); ?> </strong></p>
                                                    </td>
                                                    <td class="table__padding">
                                                        <p>: <?php
                                                                echo $SbWiseStockProduction = $totalProductionSB - $totalSoldSB - $totalWasteSB;
                                                                if ($SbWiseStockProduction) {
                                                                    if ($unit) {
                                                                        echo  ' ' . $unit->un_name;
                                                                    }
                                                                }
                                                                ?>
                                                        </p>
                                                    </td>
                                                </tr>
                                                <tr>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="report__card__color">

                                        <div class="row">

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <table class="table table-striped table-hover table-bordered" id="editable-sample">
                            <thead>
                                <tr>
                                    <th><?= lang('serialNo'); ?>.</th>
                                    <th><?= lang('production_date'); ?> </th>
                                    <th><?= lang('shed'); ?> </th>
                                    <th><?= lang('batch'); ?> </th>
                                    <th><?= lang('production_quantity'); ?> </th>
                                    <th><?= lang('note'); ?> </th>
                                    <th><?php echo lang('options'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $serial = 0;
                                foreach ($wastedProductions as $list) {
                                    $serial++;
                                ?>
                                    <tr class="">
                                        <td><?= $serial ?></td>
                                        <td><?= date("$settings->date_format", strtotime($list->prw_date)); ?></td>
                                        <td><?= $this->shed_model->getShedById($list->prw_shed_id)->sh_no ?>: <?= $this->shed_model->getShedById($list->prw_shed_id)->sh_title ?> </td>
                                        <td><?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($list->prw_shed_id, $list->prw_batch_id)->lshs_batch_id ?>: <?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($list->prw_shed_id, $list->prw_batch_id)->lshs_batch_title ?> </td>
                                        <td> <?= $list->prw_wasted_quantity; ?></td>
                                        <td> <?= $list->prw_description; ?></td>
                                        <td>
                                            <button type="button" class="button button-warning editButton" data-toggle="modal" data-date="<?= date("$settings->date_format", strtotime($list->prw_date)); ?>" data-unit="<?php if ($unit) {
                                                                                                                                                                                                                                echo $unit->un_name;
                                                                                                                                                                                                                            } ?>" data-stock-quantity="<?= $SbWiseStockProduction; ?>" data-id="<?= $list->prw_id; ?>" data-product-assign-id="<?= $list->prw_pra_id ?>" data-product-name="<?= $productById->pr_name ?>" data-category-name="<?= $productCategory; ?>" data-date="<?= date("$settings->date_format", strtotime($list->prw_date)); ?>" data-shed-id="<?= $list->prw_shed_id; ?>" data-batch-id="<?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($list->prw_shed_id, $list->prw_batch_id)->lshs_batch_id ?>" data-quantity="<?= $list->prw_wasted_quantity; ?>" data-description="<?= $list->prw_description; ?>" data-shed-value="<?= $this->shed_model->getShedById($list->prw_shed_id)->sh_no ?>: <?= $this->shed_model->getShedById($list->prw_shed_id)->sh_title ?>" data-batch-value="<?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($list->prw_shed_id, $list->prw_batch_id)->lshs_batch_id ?>: <?= $this->purchase_model->getAssignedSummaryDataByShedAndBatchId($list->prw_shed_id, $list->prw_batch_id)->lshs_batch_title ?>"><i class="fas fa-edit"></i> <?php echo lang('edit'); ?></button>
                                            <a class="" href="<?php echo base_url('') ?>product/deleteProductWasted?prw_id=<?= $list->prw_id; ?>&&prw_pra_id=<?= $list->prw_pra_id; ?>" onclick="return confirm('<?= lang('are_you_sure_want_to_delete_this_item'); ?>');"><button type="button" class="button button-danger"><i class="fas fa-trash"></i> <?php echo lang('delete'); ?></button></i></a>
                                        </td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </section>
        </div>
        <!-- page end-->
    </section>
</section>
<!--main content end-->

<!-- Edit production -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-info">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title"><strong><i class="fa fa-edit"></i> <?= lang('edit_wasted_production'); ?></strong></h4>
            </div>
            <div class="modal-body" style="height:100%;">
                <form role="form" id="productionEditForm" action="<?php echo base_url('product/updateProductionWasted'); ?>" method="post" enctype="multipart/form-data">
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <label for="exampleInputEmail1"><?= lang('product_name'); ?></label>
                            <input type="text" class="form-control" name="" id="productName" value='' placeholder="" readonly>
                        </div>
                        <div class="form-group col-sm-6">
                            <label for="exampleInputEmail1"><?= lang('category'); ?></label>
                            <input type="text" class="form-control" name="" id="categoryName" value='' placeholder="" readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <label for="exampleInputEmail1"><?= lang('shed'); ?></label>
                            <input type="text" class="form-control" name="" id="shedValue" value='' placeholder="" readonly>
                        </div>
                        <div class="form-group col-sm-6">
                            <label for="exampleInputEmail1"><?= lang('batch'); ?></label>
                            <input type="text" class="form-control" name="" id="batchValue" value='' placeholder="" readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <label for="exampleInputEmail1"><?= lang('product_in_stock'); ?></label>
                            <input type="text" class="form-control" name="" id="productInStockW" value='' placeholder="" readonly>
                        </div>
                        <div class="form-group col-sm-6">
                            <label for="exampleInputEmail1"><?= lang('wasted_quantity'); ?><span class="text-danger">*</span></label>
                            <input type="text" class="form-control input__number" name="prw_wasted_quantity" id="wastedQuantity" value='' placeholder="" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1"><?= lang('production_date'); ?></label>
                        <input type="text" class="form-control datepicker" name="prw_date" id="wastedDate" value='' placeholder="" required>
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1"><?php echo lang('description'); ?></label>
                        <textarea name="prw_description" class="form-control" id="description" rows="5" placeholder="Enter description" style="height: auto !important;"></textarea>
                    </div>
                    <input type="hidden" name="prw_pra_id" value='' id="productAssignId">
                    <input type="hidden" name="prw_id" value='' id="productionId">
                    <section class="">
                        <button type="submit" name="submit" class="button button-info submit_button"><?php echo lang('edit_button'); ?></button>
                    </section>
                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>



<!-- Javascript For Edit product -->
<script src="<?php echo base_url('common/js/jquery-1.11.1.min.js'); ?>"></script>
<script type="text/javascript">
    $(document).ready(function() {
        // Edit Product
        $(".editButton").click(function(e) {
            e.preventDefault(e);
            // Get the record's ID via attribute  
            var iid = $(this).attr('data-id');
            var product_assign_id = $(this).attr('data-product-assign-id');
            var product_name = $(this).attr('data-product-name');
            var category_name = $(this).attr('data-category-name');
            var shed_id = $(this).attr('data-shed-id');
            var batch_id = $(this).attr('data-batch-id');
            var shed_value = $(this).attr('data-shed-value');
            var batch_value = $(this).attr('data-batch-value');
            var quantity = $(this).attr('data-quantity');
            var date = $(this).attr('data-date');
            var description = $(this).attr('data-description');
            var stock_quantity = $(this).attr('data-stock-quantity');
            var unit = $(this).attr('data-unit');
            $('#myModal').modal('show');
            $("#productionId").val(iid);
            $("#productAssignId").val(product_assign_id);
            $("#productName").val(product_name);
            $("#categoryName").val(category_name);
            $("#shedId").val(shed_id);
            $("#batchId").val(batch_id);
            $("#shedValue").val(shed_value);
            $("#batchValue").val(batch_value);
            $('#wastedQuantity').val(quantity);
            $('#wastedDate').val(date);
            $('#description').val(description);
            $('#productInStockW').val(stock_quantity + ' ' + unit);

            $("#wastedQuantity").keyup(function() {
                var wastedQuantity = $(this).val();
                if (parseFloat(wastedQuantity) > parseFloat(stock_quantity)) {
                    Swal.fire({
                        icon: "warning",
                        title: "<?= lang('warning'); ?>...",
                        text: "<?= lang('please_enter_a_valid_value'); ?>"
                    });
                    $("#wastedQuantity").val(quantity);
                }
            });
        });

    });
</script>