
$(function () {
    let href = this.location.pathname;
    console.log('href=' + href)

    $('#clearBtn').click(function () {
        $('input').val("");
    });



    /*let pay_type = $(".pay-type-create option:selected").text();
    if (pay_type == "支票") {
        $('.check-create-hide').show();
    }
    else {
        $('.check-create-hide').hide();
    }*/


    //for home/index todolist
    //============首頁todo list功能================

    if (href === "/") {

        //編輯
        $('.todo-list').on('change', 'input[type="checkbox"]', function () {
            let checkbox = $(this);
            $(this).parents('li').toggleClass('completed');
            $(this).attr('disabled', true);
            let checked = $(this).prop('checked');
            let thisId = $(this).data('id');
            $.post('/home/ChangeToDoListItem', { id: thisId, IsDone: checked }).done(function (data) {
                console.log($(this))
                checkbox.attr('disabled', false);
                showAlert(data.isSuccess, data.message);
            });
        });


        //新增
        $('#add-task').click(function () {
            var post_data = { Subject: $('#new_task').val() };
            $.post('/home/CreateItem', post_data).done(function (data) {
                showAlert(data.isSuccess, data.message);
                if (data.isSuccess) {
                    $('#new_task').val('');
                    var item = JSON.parse(data.data);
                    console.log(item)
                    $('.todo-list-custom').append("<li><div class='form-check'><label class='form-check-label'><input class='checkbox' type='checkbox' data-id='" + item.id + "'/>" + item.subject + "<i class='input-helper'></i></label></div><i class='remove mdi mdi-close-circle-outline' data-id='" + item.id + "'></i></li>");
                }

            });
        });

        //=============刪除todolist=============
        $('.todo-list').on('click', '.remove', function () {
            let item = $(this).parent('li');
            //console.log($(this).siblings('.form-check').find('.checkbox').prop('disabled'));
            let checkbox = $(this).siblings('.form-check').find('.checkbox');
            let checkboxStatus = checkbox.prop('disabled');
            let checked = checkbox.prop('checked');
            console.log('checked=' + checked)
            if (!checkboxStatus) {

                if (checked == true) {
                    console.log($(this).data("id"));
                    var post_data = { id: $(this).data("id") };
                    $.post('/home/DeleteToDoListItem', post_data).done(function (data) {

                        showAlert(data.isSuccess, data.message);
                        if (data.isSuccess)
                            item.remove();
                    });
                }

            }
        });

    }
    //============END首頁todo list功能================
    $('#openUploadModal').click(function () {
        $('#fileList').html("");
    })

    //uploadModal
    $('#inputGroupFile01').on('change', function () {
        var files = $('input[name="files"]').prop('files');//获取到文件列表
        var names = [];
        for (var i = 0; i != files.length; i++) {
            console.log(files[i].name)
            names.push(files[i].name);
        }
        if (files.length > 1) {
            $('#fileList').html(names.join("</br>"));
            $(this).next('label').html("multiple files");
        }
        else {
            $('#fileList').html("");
            $(this).next('label').html(names[0]);
        }

    })

    $('#uploadBtn').click(function () {
        var files = $('input[name="files"]').prop('files');//获取到文件列表
        if (files.length == 0) {
            alert('請選擇圖片');
            return;
        } else {
            var formData = new FormData();
            for (var i = 0; i != files.length; i++) {

                formData.append("files", files[i]);
            }
            formData.append('Route', "Home");
            $.ajax({
                url: "/Upload/UploadImage",
                data: formData,
                processData: false,
                contentType: false,
                type: HttpMethod.Post,
                success: function (data) {

                    $('input[name="files"]').val('');
                    console.log("test");
                    console.log(data);
                    alert(data.message);
                    if (data.isSuccess) {
                        let inputFile = $('#inputGroupFile01')
                        const input = document.querySelector('#inputGroupFile01')


                        input.value = '';

                        $('#uploadModal').modal('hide');
                    }

                },
                error: function (data) {
                    console.log(data);
                }
            });
        }
    });
    //============首頁通知功能================
    function GetNotifyForAjax() {
        $.post('/home/GetNotifyCountForAjax').done(function (data) {
            //console.log(data.data)
            $('.count-symbol').prop('hidden', false);
            if (data.data != 0) { $('.count-symbol').show() }
            else {
                $('.count-symbol').hide();
                $('#notifyData>a:not(:nth-last-child(1))').remove();
                $('#setReadBtn').addClass('disabled')
            }
        });
    }
    if (!href.includes("Login") && !href.includes("login")) {
        //notify navbar
        //setInterval(function () { GetNotifyForAjax() }, 3000);

    }


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

        $.post('/home/GetNotifyDataForAjax').done(function (data) {
            var mydata = JSON.parse(data.data);
            //console.log(mydata)
            if (mydata != null) {
                $('#setReadBtn').removeClass('disabled')
                $('#notifyList>a:not(:nth-last-child(1))').remove();

                for (var item in mydata) {
                    var obj = mydata[item];
                    //console.log(obj)
                    /*let icon_tea = '<div class="preview-thumbnail"><div class="preview-icon bg-success"><i class="mdi mdi-leaf"></i></div></div>';
                    let icon_fortuneLight = '<div class="preview-thumbnail"><div class="preview-icon bg-warning"><i class="mdi mdi-coin"></i></div></div>';
                    let icon_brightLight = '<div class="preview-thumbnail"><div class="preview-icon bg-info"><i class="mdi mdi-white-balance-incandescent"></i></div></div>';
                    let icon_donate = '<div class="preview-thumbnail"><div class="preview-icon bg-danger"><i class="mdi mdi-cash-usd"></i></div></div>';
                    let icon_default = '<div class="preview-thumbnail"><div class="preview-icon"></div></div>'
                    var icon;
                    switch (obj.itemName) {
                        case '青草茶': icon = icon_tea;
                            break;
                        case '文昌': icon = icon_brightLight;
                            break;
                        case '財利燈': icon = icon_fortuneLight;
                            break;
                        case '捐贈': icon = icon_donate;
                            break;
                        default: icon = icon_default;
                            break;
                        
                    }*/


                    if (obj.itemName == null || obj.itemName == "")
                        obj.itemName = "Null";

                    let icon = "";
                    let fontColor = "";

                    //回傳字串象徵的顏色
                    let colorHex = TextColor(obj.itemName);

                    //判斷背景顏色深淺來決定字的顏色
                    let ContrastYIQ = getContrastYIQ(colorHex);
                    if (ContrastYIQ == "light")
                        fontColor = "#000000";
                    else
                        fontColor = "#ffffff";

                    icon = "<input type='button' value='" + obj.itemName.split("")[0] + "' style='width: 50px; height: 50px; border-radius: 50%; font-size: 15pt; background-color: " + colorHex + "; border: none; color: " + fontColor + "'>"

                    let row = "<a href='/FinancialRecordIncome/GetIncome?Type=收入' class='dropdown-item preview-item'>" + icon + "<div class='preview-item-content d-flex align-items-start flex-column justify-content-center'>"
                        + "<h6 class='preview-subject font-weight-normal mb-1'>" + obj.memberName + "</h6><p class='text-gray ellipsis mb-0'>" + obj.itemName + "</p></div></a >"


                    $('#notifyList').append(row);
                }
            }
        });
    });


    //設為已讀
    $('#setReadBtn').click(function () {
        $.post('/home/ReadNotifyDataForAjax').done(function (data) {
            if (data.isSuccess) {
                showAlert(data.isSuccess, data.message);
            }

        });

    });
    //============END首頁通知功能================

})