<style type="text/css">
    @media print
    {
        .noprint {display:none;}
    }
</style>
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    
                </div>
                <h4 class="page-title" >Package Types</h4>
            </div>


        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box table-responsive">

                    <h4 class="header-title m-t-0 m-b-30">&nbsp;</h4>

                    <table id="datatable-buttons" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th class="hidden">ID</th>
                                <th>Image</th>
                                <th>Type</th>
                                <th>Active</th>
                              
                                <th class='noprint'>Action</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php
                            if (isset($packages_type)) {
                                foreach ($packages_type as $type) {
                                    ?>
                                    <tr>
                                        <td class="hidden"><?php echo $type['id_package_type']; ?></td>
                                        <td>
                                            <a title="<?php echo $type['service_type']; ?>" onclick="upload_image('<?php echo $type['id_package_type']; ?>', '<?php echo $type['service_type_image'] ? $type['service_type_image'] : NULL; ?>')" href="javascript:void(0)">
                                                <img width="50px;" src="<?php echo base_url() . 'assets/images/servicetype/'; ?><?php echo $type['service_type_image'] ? $type['service_type_image'] : "nu.jpg"; ?>" alt="<?php echo $type['service_type'] ?>"/>
                                            </a>
                                        </td>
                                        <td><?php echo $type['service_type']; ?></td>
                                        
                                        <td><span class="label <?php
                                            if ($type['service_type_active'] === "Yes") {
                                                echo 'label-pink';
                                            } else {
                                                echo 'label-inverse';
                                            }
                                            ?> "><?php echo $type['service_type_active']; ?></span>
                                        </td>

                                        <td class='noprint' > 
                                            <button id='btnduplicate' onclick="openduplicate(<?php echo $type['id_package_type']; ?>,'<?php echo $type['service_type']; ?>');" class="btn btn-icon waves-effect waves-light btn-pink m-b-5"> <i class="fa fa-copy"></i> Copy To Other Branch</button> 
                                        </td>
                                        
                                    </tr>
                                    <?php
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div><!-- end col -->
        </div>
        <!-- end row -->

        <!--Modals-->


         <!--Duplicate Type Modal-->
        <div id="duplicatetypemodal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="duplicatecatmodal" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Copy Package To Other Branch</h4>
                    </div>
                    <div class="modal-body">

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" readonly="readonly" class="form-control"  id="idduplicatetype" name="idduplicatetype">                                                                        
                                    <input type="text" readonly="readonly" class="form-control"  id="duplicatetype" name="duplicatetype">                                                                        
                                </div> 
                            </div> 
                            <div class="col-md-6">
                                <div class="form-group">
                                    <?php foreach($common_branches as $branch){?>
                                        <div>
                                    <button id="btn_branch_<?php echo $branch['id_business'];?>" onclick ="duplicate_package(<?php echo $branch['id_business'];?>);"class="btn btn-custom waves-effect waves-light m-b-5">Copy To <?php echo $branch['business_name'];?></button> 
                                    </div>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        
                    </div>
                </div>
            </div>
        </div>
        
        <script>
            $(document).ready(function() {
                $('#datatable-buttons').DataTable({
                    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    stateSave: true,
                    dom: "Bfrtlip",
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"}, {
                            extend: "excel",
                            className: "btn-sm btn-warning btn-trans"
                        }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                    responsive: !0,
                    "aoColumns": [
                        null,
                        null,
                        null,
                        null,
                        null
                    ]
                });

                toastr.options = {
                    "closeButton": true,
                    "debug": true,
                    "newestOnTop": true,
                    "progressBar": true,
                    "positionClass": "toast-bottom-center",
                    "preventDuplicates": true,
                    "onclick": null,
                    "showDuration": "300",
                    "hideDuration": "1000",
                    "timeOut": "5000",
                    "extendedTimeOut": "1000",
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut"
                }

            });


            function openduplicate(id_package_type, package){
                if (id_package_type && id_package_type !== null) {
                    $("#idduplicatetype").val(id_package_type);
                    $("#duplicatetype").val(package);

                    $("#duplicatetypemodal").modal("show");
                }
            }
            
            function duplicate_package(businessid){

                 if ($("#idduplicatetype").val() !== "" && businessid !== "") {
                    $.ajax({
                        type: 'POST',
                        url: '<?php echo base_url(); ?>tools/duplicate_packages',
                        data: {
                            id_package_type: $("#idduplicatetype").val(),
                            package_type: $("#duplicatetype").val(),
                            copy_to_business: businessid
                           
                        },
                        success: function(data) {
                            var result = data.split("|");
                            if (result[0] === "success") {
                                toastr.success('Package Copied Successfully');
                                location.reload();
                            }
                        }
                    });
                }
            }
        </script>