/**
 * Created by Glee on 03-Nov-2020.
 */

const Checkbox = function (deleteItemCb){

    const INPUT_CHECKBOX_CLASS = '.js-checkbox-id';
    let _deleteItemCb = deleteItemCb;

    return {
        setAllChecked,
        getCheckedIds,
        deleteSelected
    }

    function setAllChecked() {
        var all_checkboxes = $(INPUT_CHECKBOX_CLASS);

        if($(this).is(':checked')) {
            all_checkboxes.each(function (index, item) {
                $(item).prop( "checked", true);
            })
        }else{
            all_checkboxes.each(function (index, item) {
                $(item).prop( "checked", false);
            })
        }
    }

    function deleteSelected() {
        if(!confirm('Bạn chắc chắn muốn xóa ?')) {
            return ;
        }

        $(INPUT_CHECKBOX_CLASS).each(function (index, item) {
            if($(item).is(':checked')) {
                //delete_deal(item.value, false);
                _deleteItemCb(item.value)
            }
        });
    }

    function getCheckedIds() {
        let list_ids = [];
        $(INPUT_CHECKBOX_CLASS).each(function (index, item) {
            if($(item).is(':checked')) list_ids.push(item.value);
        });

        return list_ids;
    }
};