$(function () {

    $(".table #all").on('change', function () {
        var checked = $(this).prop('checked');
        $(".table tbody .item-checkbox").prop('checked', checked);
    })

    $(".table tbody .item-checkbox").on('change', function () {
        var max = $(".table .item-checkbox").length;
        var current = $(".table .item-checkbox:checked").length;
        var checked = max == current;
        if ($(".table #all:checked") != checked) {
            $(".table #all").prop('checked', checked);
        }
    });

    $(".table .delete").on('click', function () {
        var ids = $(this).data('id');
        tableItemDelete(ids);
    })

    $(".deletes").on('click', function () {
        /*var current = $(".table .item-checkbox:checked").length;
        if (current == 0) {
            $alert.info('请先勾选一条信息');
            return;
        }
        var ids = [];
        var deleteIds = '';
        $(".table .item-checkbox:checked").each(function () {
            ids.push($(this).val())
        });
        deleteIds = ids.join(',');*/
        var deleteIds = tableItemId() ;
        tableItemDelete(deleteIds);

    });

    $(".update").on('click',function(){
        var ids = $(this).data('id');
        $alert.confirm('确定更改状态吗?').then(function (result) {
            if (result.value) {
                $api.post(rootUrl + '/' + ids + '/status', {}, function (data) {
                    if (data.code == 0) {
                        $toast.success('操作成功');
                        window.location.reload();
                    }
                })
            }
        });
    })
})

function tableItemId(){
    var current = $(".table .item-checkbox:checked").length;
    if (current == 0) {
        $alert.info('请先勾选一条信息');
        return;
    }
    var ids = [];
    var opIds = '';
    $(".table .item-checkbox:checked").each(function () {
        ids.push($(this).val())
    });
    opIds = ids.join(',');
    return opIds;
}

function tableItemDelete(ids) {
    $alert.confirm('确定要删除吗?').then(function (result) {
        if (result.value) {
            $api.post(rootUrl + '/' + ids + '/delete', {}, function (data) {
                if (data.code == 0) {
                    $toast.success('删除成功');
                    window.location.reload();
                }
            })
        }
    });
}
