<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/jquery.dataTables-cust.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/ColReorder.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/FixedColumns.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/ColVis.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/ZeroClipboard.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/media/js/TableTools.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/DT_bootstrap.js"></script>

<script src="https://cdn.lordicon.com/libs/mssddfmo/lord-icon-2.1.0.js"></script>
<div id="main p-0" role="main">
    <div id="content" style=" padding-top: 0px; ">
        <div class="container">
            <div class="row">
                <div class="col-xs-12 col-sm-7 col-md-7 col-lg-4">
                    <h1 class="page-title txt-color-blueDark">
                        <i class="fa fa-star fa-fw "></i>
                        <?php echo $this->lang->line('new_product'); ?>
                    </h1>
                </div>
            </div>
            <?php if ($this->session->flashdata('flashMsgType') == 'Success') : ?>
                <div class="alert alert-success fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-check"></i>
                    <strong><?php print $this->session->flashdata('flashMsgType') ?></strong> <?php print $this->session->flashdata('flashMsg') ?>
                </div>
            <?php elseif ($this->session->flashdata('flashMsgType') == 'Error') : ?>
                <div class="alert alert-danger fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-times"></i>
                    <strong><?php print $this->session->flashdata('flashMsgType') ?></strong> <?php print $this->session->flashdata('flashMsg') ?>
                </div>
            <?php endif; ?>
            <form action="<?php echo site_url(); ?>admin/products/productAction/<?php if (!empty($product)) echo $product->id; ?>" method="POST" autocomplete="OFF" class="container-fluid" enctype="multipart/form-data">
                <div class="col-lg-6 mx-auto">
                    <div class="d-flex flex-column justify-content-center customerFormBg text-dark fw-bold">
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="product_type" class="form-label"><?php echo $this->lang->line('product_type'); ?></label>
                            <select id="product_type" name="product_type" class="form-select">
                                <option value="1" <?php if (!empty($product) && $product->type == 1) echo 'selected="selected"'; ?>><?php echo $this->lang->line('item'); ?></option>
                                <option value="2" <?php if (!empty($product) && $product->type == 2) echo 'selected="selected"'; ?>><?php echo $this->lang->line('service'); ?></option>
                            </select>
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="phone"><?php echo $this->lang->line('name'); ?></label>
                            <input type="text" class="form-control" name="name" id="name" value="<?php if (!empty($product)) echo $product->name; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="description"><?php echo $this->lang->line('text'); ?></label>
                            <textarea class="form-control" id="description" name="description" rows="3"><?php if (!empty($product)) echo $product->description; ?></textarea>
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="price"><?php echo $this->lang->line('price'); ?></label>
                            <input type="text" class="form-control" required="required" name="price" id="price" value="<?php if (!empty($product)) echo $product->price; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="discount"><?php echo $this->lang->line('discount'); ?></label>
                            <input type="text" class="form-control" name="discount" id="discount" value="<?php if (!empty($product)) echo $product->discount; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="code"><?php echo $this->lang->line('sku'); ?></label>
                            <input type="text" class="form-control" name="code" id="code" value="<?php if (!empty($product)) echo $product->code; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="category_id"><?php echo $this->lang->line('category'); ?></label>
                            <input type="text" class="form-control" name="category_id" id="category_id" value="<?php if (!empty($product)) echo $product->category_id; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="quantity"><?php echo $this->lang->line('unit'); ?></label>
                            <input type="text" class="form-control" name="quantity" id="quantity" value="<?php if (!empty($product)) echo $product->quantity; ?>" autocomplete="OFF">
                        </div>
                        <?php if (!empty($product)) $readonly = 'readonly="readonly"'; else $readonly = ''; ?>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="cartoon_quantity"><?php echo $this->lang->line('cartoon_quantity'); ?></label>
                            <input <?php echo $readonly; ?> type="text" class="form-control" name="cartoon_quantity" id="cartoon_quantity" value="<?php if (!empty($product)) echo $product->cartoon_quantity; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="cartoon_price"><?php echo $this->lang->line('cartoon_price'); ?></label>
                            <input type="text" class="form-control" name="cartoon_price" id="cartoon_price" value="<?php if (!empty($product)) echo $product->cartoon_price; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="manufacturer_id"><?php echo $this->lang->line('producer'); ?></label>
                            <input type="text" class="form-control" name="manufacturer_id" id="manufacturer_id" value="<?php if (!empty($product)) echo $product->manufacturer_id; ?>" autocomplete="OFF">
                        </div>
                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12">
                            <label for="colors"><?php echo $this->lang->line('colors'); ?></label>
                            <select name="colors[]" id="colors" class="select2" multiple>
                                <?php $colors = explode(",", $product->colors); ?>
                                <option <?php if (!empty($product) && in_array("White", $colors))   echo 'selected="selected"'; ?> value="White"><?php echo $this->lang->line('white'); ?></option>
                                <option <?php if (!empty($product) && in_array("Black", $colors))   echo 'selected="selected"'; ?> value="Black"><?php echo $this->lang->line('black'); ?></option>
                                <option <?php if (!empty($product) && in_array("Red", $colors))     echo 'selected="selected"'; ?> value="Red"><?php echo $this->lang->line('red'); ?></option>
                                <option <?php if (!empty($product) && in_array("Green", $colors))   echo 'selected="selected"'; ?> value="Green"><?php echo $this->lang->line('green'); ?></option>
                                <option <?php if (!empty($product) && in_array("Blue", $colors))    echo 'selected="selected"'; ?> value="Blue"><?php echo $this->lang->line('blue'); ?></option>
                                <option <?php if (!empty($product) && in_array("Gold", $colors))    echo 'selected="selected"'; ?> value="Gold"><?php echo $this->lang->line('gold'); ?></option>
                                <option <?php if (!empty($product) && in_array("Silver", $colors))  echo 'selected="selected"'; ?> value="Silver"><?php echo $this->lang->line('silver'); ?></option>
                                <option <?php if (!empty($product) && in_array("Yellow", $colors))  echo 'selected="selected"'; ?> value="Yellow"><?php echo $this->lang->line('yellow'); ?></option>
                                <option <?php if (!empty($product) && in_array("Grey", $colors))    echo 'selected="selected"'; ?> value="Grey"><?php echo $this->lang->line('grey'); ?></option>
                            </select>
                        </div>

                        <div class="form-group col-lg-12 col-sm-12 col-xs-12 col-md-12 d-flex justify-content-between mt-3">
                            <div>
                                <label class="toggle">
                                    <input type="checkbox" name="status" <?php if (!empty($product) && $product->status == 1) echo 'checked="checked"'; ?>>
                                    <i data-swchon-text="ON" data-swchoff-text="OFF"></i>
                                    <?php echo $this->lang->line('status'); ?>
                                </label>
                                <label class="toggle">
                                    <input type="checkbox" name="special" <?php if (!empty($product) && $product->special == 1) echo 'checked="checked"'; ?>>
                                    <i data-swchon-text="ON" data-swchoff-text="OFF"></i>
                                    <?php echo $this->lang->line('special_product'); ?>
                                </label>
                            </div>
                            <button type="submit" class="btn btn-success">
                                <?php echo $this->lang->line('save'); ?>
                            </button>
                        </div>
                    </div>
                    <?php if (!empty($product) && !empty($product->main_image)) { ?>
                        <div class="col-lg-6">
                            <div class="well well-light">
                                <h2><?php echo $this->lang->line('beginning_picture'); ?>: </h2>
                                <img src="<?php echo site_url(); ?>assets/products/<?php echo $product->main_image; ?>" width="100%" />
                            </div>
                        </div>
                    <?php }; ?>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $(function() {
            $('input[type=file]').change(function() {
                var t = $(this).val();
                var labelText = t.substr(12, t.length);
                $(this).parent().parent().parent().find('input[name="filename"]').attr('placeholder', labelText);
            })
        });
    });
</script>
<script src="<?php echo site_url(); ?>assets/js/customerInvoice.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.5.2/bootbox.min.js" integrity="sha512-RdSPYh1WA6BF0RhpisYJVYkOyTzK4HwofJ3Q7ivt/jkpW6Vc8AurL1R+4AUcvn9IwEKAPm/fk7qFZW3OuiUDeg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css">
<script>
    $(document).ready(function() {
        $('#savePaymentDetails').on('click', function() {

            var data = $('#payment_data').serializeArray();

            $.post(root + 'admin/invoices/addPaymentSupplier', data, function(d) {

                if (d.result == true) {
                    alert("<?php echo $this->lang->line('Done'); ?>!");
                    location.reload();
                } else {
                    alert("<?php echo $this->lang->line('Error'); ?>!");
                }

            }, 'json');
        });

        $('a.convert_box').on('click', function() {

            var payment_id = $(this).attr('rel-id');
            $('#payment_id').val(payment_id);
            $('#convert_box').modal('show');

        });

        $('#convert_box_data').on('click', function() {

            var data = {};
            data.payment_id = $('#payment_id').val();
            data.new_box = $('#new_payment_box').val();
            data.new_payment = $('#new_payment_type').val();

            $.post(root + 'admin/invoices/supplierconvertPayment', data, function(d) {

                if (d.result == true) {
                    alert("<?php echo $this->lang->line('Done'); ?>!");
                    location.reload();
                } else {
                    alert("<?php echo $this->lang->line('Error'); ?>!");
                }

            }, 'json');
        });



    });
</script>

<style>
    .form-select, .dataTable-selector {
        background-position: left 1.125rem center !important;
    }
</style>