<ul class="breadcrumb">
    <li><a href="<?= site_url('admin/dashboard') ?>">Home</a></li>
    <li class="active"><?php echo $breadcum; ?></li> 
</ul>
<div class="page-content-wrap">                
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <?php if ($this->session->userdata('unit_success_msg')) { ?>  
                    <div class="alert alert-info"> <?= $this->session->userdata('unit_success_msg') ?></div>
                    <?php
                    $this->session->unset_userdata('unit_success_msg');
                }
                ?>
                <form action="" method="post" id="specification_form">
                    <div class="panel-heading">
                        <input type="submit" name="submit" value="Update" class="btn btn-default" />
                        <a href="javascript:" class="btn btn-default add_more"><span class="fa fa-plus-square"></span>Add New Row</a>
                    </div>
                    <div class="panel-body panel-body-table">
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped table-actions">
                                <thead>
                                    <tr>  
                                        <th>Unit Name</th>  
                                        <th>Unit Price</th>  
                                        <th>Is Active</th>  
                                    </tr>
                                </thead>
                                <tbody id="item_tbody"> 
                                    <?php
                                    $i = 0;
                                    foreach ($specificationList as $value) {
                                        ?>
                                        <tr class="item_tr">                                                                                      
                                            <td>
                                                <input type="text" name="unit_name[]" value="<?= $value->unit_name ?>" class="form-control requiredField" />
                                            </td>  
                                            <td>
                                                <input type="text" name="unit_prices[]" value="<?= $value->unit_prices ?>" class="form-control requiredField" />
                                            </td>   
<!--                                            <td>
                                                <input type="text" name="no_of_people[]" value="<?= $value->no_of_people ?>" class="form-control requiredField" />
                                            </td>  -->
                                            <td>
                                                <select class="form-control requiredField" name="is_active[]">
                                                    <option value="yes" <?php if ($value->is_active == 'yes') { ?> selected="selected" <?php } ?>>Yes</option>
                                                    <option value="no" <?php if ($value->is_active != 'yes') { ?> selected="selected" <?php } ?>>No</option>
                                                </select>
                                            </td> 
                                        </tr>
                                        <?php
                                        $i++;
                                    }
                                    ?>

                                    <?php if (sizeof($specificationList) == 0) { ?>
                                        <tr class="item_tr">                                      

                                            <td>
                                                <input type="text" class="form-control requiredField" name="unit_name[]" value="<?= $unit_name ?>">                                          
                                            </td>  
                                            <td>
                                                <input type="text" class="form-control requiredField" name="unit_prices[]" value="<?= $unit_prices ?>">   
                                            </td>  
<!--                                            <td>
                                                <input type="text" class="form-control requiredField" name="no_of_people[]" value="<?= $no_of_people ?>">   
                                            </td>  -->
                                            <td>
                                                <select class="form-control requiredField requiredField" name="is_active[]">
                                                    <option value="yes">Yes</option>
                                                    <option value="No">No</option>
                                                </select>
                                            </td>  
                                        </tr>   
                                    <?php } ?> 
                                </tbody>
                            </table>
                        </div>                                
                    </div>
                </form>
            </div>                                                
        </div>
    </div>
</div>
<style>
    .error { color:red!important;}
</style>
<script>
    $('body').delegate('.check_all', 'click', function () {
        if ($(this).is(':checked')) {
            $('.chk_all').addClass('selectCheck');
            $('.chk_all').prop('checked', 'checked');
            $('.GreyBtn').attr('disabled', false);
        } else {
            $('.chk_all').removeClass('selectCheck');
            $('.chk_all').removeAttr('checked');
            $('.GreyBtn').attr('disabled', 'disabled');
        }
    });
    $('body').delegate('.add_more', 'click', function () {
        $("#item_tbody tr:first").clone().find('input[type=text]').val('').end().find('input[type=text]:first').val('').end().find('textarea').val('').end().appendTo("#item_tbody");
        

    });
    $('body').delegate('.remove', 'click', function () {
        if ($('.item_tr').size() > 1) {
            $(this).parent().parent().remove();
        }
    });

    $('body').delegate('.type', 'change', function () {
        var typeval = $(this).val();
        if (typeval == 'list')
        {
            $(this).parent().parent().find('.value').removeAttr('readonly');
        } else {
            $(this).parent().parent().find('.value').attr('readonly', 'readonly');
        }
    });

    $(document).ready(function () {
        $('form#specification_form').submit(function () {
            $('form#specification_form .error').remove();
            var hasError = false;
            $('.requiredField').each(function () {

                if ($.trim($(this).val()) == '') {
                    if ($(this).parent().parent().find('input[type=checkbox]').prop("checked") == true) {
                        var labelText = $(this).prev('label').text();
                        $(this).parent().append('<span class="error">Required!</span>');
                        hasError = true;
                    }
                } else if ($(this).hasClass('unit_price') || $(this).hasClass('quantity')) {

                    if (!$.isNumeric(($.trim($(this).val())))) {
                        var labelText = $(this).prev('label').text();
                        $(this).parent().append('<span class="error">Invalid</span>');
                        hasError = true;
                    }
                }
            });
            if (!hasError) {
                var formInput = $(this).serialize();
                $('#specification_form').submit();
            }
            return false;
        });
    });



    $('body').delegate('.category_id', 'change', function () {
        var category_id = $(this).val();
        if (category_id != '') {
            $.ajax({
                type: "POST",
                async: true,
                url: baseurl + "admin/ajax-package-item-list",
                data: {
                    "category_id": category_id
                },
                dataType: "json",
                success: function (data) {
                    $('.item').html('');
                    if (data.success) {
                        $('.item').html('<option value="">Select Package Item</option>');

                        $.each(data.package_item, function (key, value) {
                            $('.item').append('<option value="' + value.pkg_item_id + '">' + value.item_name + '</option>');
                        });
                    } else {
                        $('.item').html('<option value="">No Record Found</option>');
                    }
                    // $(".select").selectpicker('refresh');

                    //$('#region_id').append('<option value="">No Region Found</option>');

                }
            });
        }
    });

</script>