$('#products_add_save').click(function () {

    let sendData = {
        title                       : $.trim($('#title').val()),
        cat_id                      : $.trim($('#cat_id').val()),
        icon                        : "",
        image                       : "",
        summary                     : "",
        type                        : "normal",
        cat2_title                  : "",
        cat3_title                  : "",
        background                  : "",
        background_color            : "",
        data                        : "",
        data_preview                : "",
        subscribe_ids               : "",
        sku                         : "",
        password_protected          : "",
        intent_type                 : "",
        intent_data                 : "",
        pp_variables_list           : "",
        pp_variables_map            : "",
        pp_gallery                  : "",
        pp_spec_table               : "",
        pp_crosssell_ids            : "",
        pp_guarantee                : "",
        pp_virtual_intent_type      : "",
        pp_virtual_intent_data      : "",
        pp_price_index              : "",
        pp_amazing_discount_deadline: "",
        pp_virtual_intent_btn_text  : "",
        pp_virtual_intent_btn_icon  : "",
        date_updated                : "",
        single_buy                  : 1
    }

    $.ajax({
            type: "POST",
            url: mpPanelBaseUrl + "/add_products",
            data: sendData,
            timeout: 25000,
            cache: false,
            success: function (data) {
                if (data == "ok" || isInt(data)) {
                    notyf.success("با موفقیت انجام شد");
                    $('#global_item_adder_end').trigger('click');
                } else {
                    notyf.error(data);
                }
            },
            error: function (xhr, status, error) {
                loadingOverlay.hide();
                notyf.error('Error: ' + xhr.status + ' ' + xhr.statusText);
            }
        });

});

$('#products_copy').click(function () {

    let sendData = {
        copy_title  : $.trim($('#copy_title').val()),
        origin_id   : $.trim($('#product_origin_id').val()),
        sku         : "",
        date_updated: ""
    }

    RequestSuperHandler(
        $(this), //Current Obj
        'از کپی کردن این محصول مطمئن هستید؟', //Alert Title
        "", //Alert Desc
        1, // alertType -> 1:confirm - 2:delete
        '/duplicate_products', //Request URL
        "/products", //Return URL
        sendData
    );

});

$('.product-duplicate-item').click(function () {

    let idVal = $(this).attr('data-product-id');
    let sendData = {
        origin_id   : idVal,
        sku         : "",
        date_updated: ""
    }

    RequestSuperHandler(
        $(this), //Current Obj
        'از کپی کردن این محصول مطمئن هستید؟', //Alert Title
        "", //Alert Desc
        1, // alertType -> 1:confirm - 2:delete
        '/duplicate_products', //Request URL
        "/products", //Return URL
        sendData
    );

});

$('.product-delete-item').click(function () {

    let idVal = $(this).attr('data-product-id');
    let sendData = {
        id: idVal
    }

    RequestSuperHandler(
        $(this), //Current Obj
        'آیا از حذف کامل این محصول مطمئن هستید؟', //Alert Title
        "با حذف محصول تمام محتوی آن به طور کامل حذف خواهد شد", //Alert Desc
        2, // alertType -> 1:confirm - 2:delete
        '/delete_products', //Request URL
        "/products", //Return URL
        sendData
    );

});

$('#delete_product_btn').click(function () {

    let sendData = {
        id: $.trim($('#id').val())
    }

    RequestSuperHandler(
        $(this), //Current Obj
        'آیا از حذف کامل این محصول مطمئن هستید؟', //Alert Title
        "با حذف محصول تمام محتوی آن به طور کامل حذف خواهد شد", //Alert Desc
        2, // alertType -> 1:confirm - 2:delete
        '/delete_products', //Request URL
        "/products", //Return URL
        sendData
    );

});


$('#product_edit_save').click(function () {

    let _published = ($('.js-publish-switch').is(":checked")) ? 1 : 0;

    let sendData = {
        id                          : $.trim($('#id').val()),
        title                       : $.trim($('#title').val()),
        image                       : $.trim($('#image').val()),
        published                   : _published,
        summary                     : $.trim($('#summary').val()),
        primary_link                : $.trim($('#primary_link').val()),
        seo_title                   : $.trim($('#seo_title').val()),
        seo_description             : $.trim($('#seo_description').val()),
        cat_id                      : $.trim($('#cat_id').val()),
        cat_id2                     : $.trim($('#cat_id2').val()),
        cat_id3                     : $.trim($('#cat_id3').val()),
        cat2_title                  : $.trim($('#cat2_title').val()),
        cat3_title                  : $.trim($('#cat3_title').val()),
        price                       : $.trim($('#price').val()),
        price_off                   : $.trim($('#price_off').val()),
        sku                         : $.trim($('#sku').val()),
        comments                    : $.trim($('#comments').val()),
        background                  : $.trim($('#background').val()),
        background_color            : $.trim($('#background_color').val()),
        hide_like_view_dates        : $.trim($('#hide_like_view_dates').val()),
        openwith_rewarded_ad        : $.trim($('#openwith_rewarded_ad').val()),
        open_with_interstitial_ad   : $.trim($('#open_with_interstitial_ad').val()),
        only_for_logedin_users      : $.trim($('#only_for_logedin_users').val()),
        hide_option_menu            : $.trim($('#hide_option_menu').val()),
        show_find_in_page_by_default: $.trim($('#show_find_in_page_by_default').val()),
        type                        : $.trim($('#type').val()),
        is_under_construction       : $.trim($('#is_under_construction').val()),
        intent_type                 : $.trim($('#intent_type').val()),
        intent_data                 : $.trim($('#intent_data').val()),
        date                        : $.trim($('#date').val())
    }

    RequestSuperHandler(
        $(this), //Current Obj
        'از ویرایش کانفیگ این محصول مطمئن هستید؟', //Alert Title
        "", //Alert Desc
        1, // alertType -> 1:confirm - 2:delete
        '/update_products', //Request URL
        "/products/" + sendData.id, //Return URL
        sendData
    );

});