/**
* 2007-2024 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2024 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/
$(document).ready(function () {
    $('.datepicker').datepicker({
        prevText: '',
        nextText: '',
        dateFormat: 'yy-mm-dd'
    });

    $('#product_autocopy_input').autocomplete(ajax_products_list + "?includepublicproduct=" + allowCopyMainStoreProduct + '&ajax=1', {
        minChars: 1,
        autoFill: true,
        max: 20,
        matchContains: true,
        mustMatch: true,
        scroll: false,
        cacheLength: 0,
        formatItem: function (item) {
            return item[1] + ' - ' + item[0];
        }
    }).result(copyProduct);

    $("#chkAll").click(function () {
        var checked = $("#chkAll").prop('checked');
        $("[id^=chkProd_]").prop('checked', checked);
    });

    $("#bulkAction").change(function () {
        var act = $("#bulkAction").val();
        var data = [];
        $.each($("[id^=chkProd_]"), function (idx, item) {
            if ($(item).prop('checked')) data.push($(item).val());
        });
        $("#bulkActionData").val(data);
        if (act == "delete") alert(bulkmsg_delete);
        else if (act == "enable") alert(bulkmsg_enable);
        else if (act == "disable") alert(bulkmsg_disable);
        $("#frmBulkAction").submit();
    });

    $('.pagination a.js-search-link:not(.disabled)').on('click', function(){
        window.location.href = $(this).attr('href');
    })
});


function onClickConfirm(act, id_product) {
    var msg = '';
    if (act == "delete") msg = msgDelete;
    else msg = msgDuplicate;

    if (confirm(msg)) {
        actionOnProduct(act, id_product);
    }
    else {
        event.stopPropagation();
        event.preventDefault();
    }
}

function ResetOnClick() {
    $("[id^='filter_']").val("");
}

function goOnClick() {
    var url = listurl + "?page=" + (p <= 0 ? 1 : p) + "&n=" + n + "&orderBy=" + orderBy + "&orderWay=" + orderWay + getExtra();
    window.location.href = url;
}

function CSVOnClick() {
    var url = listurl + "?export=CSV" + "&page=" + (p <= 0 ? 1 : p) + "&n=" + n + "&orderBy=" + orderBy + "&orderWay=" + orderWay + getExtra();
    window.location.href = url;
}

function orderByWay(by, way) {
    var url = listurl + "?page=" + (p <= 0 ? 1 : p) + "&n=" + n + "&orderBy=" + by + "&orderWay=" + way + getExtra();
    window.location.href = url;
    return false;
}

function actionOnProduct(action, id_product) {
    var url = listurl + "?process=" + action + "&id_product=" + id_product + "&page=" + (p <= 0 ? 1 : p) + "&n=" + n + "&orderBy=" + orderBy + "&orderWay=" + orderWay + getExtra();
    window.location.href = url;
}

function getExtra() {
    var ex = "";
    var filter_active = $("#filter_active").val();

    var filter_approved = $("#filter_approved").val();
    var filter_name = $("#filter_name").val();
    var filter_category = $("#filter_category").val();
    var filter_id_product = $("#filter_id_product").val();
    var filter_quantity = $("#filter_quantity").val();
    var filter_price = $("#filter_price").val();
    var filter_date_add_from = $("#filter_date_add_from").val();
    var filter_date_add_to = $("#filter_date_add_to").val();

    if (filter_active.length > 0) ex = ex + "&filter_active=" + filter_active;
    if (filter_approved && filter_approved.length > 0) ex = ex + "&filter_approved=" + filter_approved;
    if (filter_name.length > 0) ex = ex + "&filter_name=" + filter_name;
    if (filter_category.length > 0) ex = ex + "&filter_category=" + filter_category;
    if (filter_id_product.length > 0) ex = ex + "&filter_id_product=" + filter_id_product;
    if (filter_quantity.length > 0) ex = ex + "&filter_quantity=" + filter_quantity;
    if (filter_price.length > 0) ex = ex + "&filter_price=" + filter_price;
    if (filter_date_add_from.length > 0) ex = ex + "&filter_date_add_from=" + filter_date_add_from;
    if (filter_date_add_to.length > 0) ex = ex + "&filter_date_add_to=" + filter_date_add_to;

    return ex;
}

function copyProduct(event, data, formatted) {
    if (data == null) return false;
    var productId = data[1];
    var productName = data[0];
    if (confirm(msgDuplicate + productName + "(id=" + productId + ")"))
        window.location.href = duplicateURL + "&id_product=" + productId;
}
