
// 업무리스트 분류 선택
$(document).ready(function () {


    /* 게시글 등록/수정 */
    $('#brd-register').on("click",function(){

        var formData = new FormData($('#basicForm')[0]);
        let idx = $('#idx').val();
        let bType = $('input[name=bType]').val();
        let page = $('input[name=page]').val();
        let key = $('input[name=key]').val();
        let keyword = $('input[name=keyword]').val();

        let params="?idx="+idx;
        if(bType) params += "&bType="+bType;
        if(page) params += "&np="+page;
        if(key) params += "&key="+key;
        if(keyword) params += "&keyword="+keyword;

        for (var i = 0; i < filesArr.length; i++) {
            // 삭제되지 않은 파일만 폼데이터에 담기
            if (!filesArr[i].is_delete) {
                formData.append("attach_file[]", filesArr[i]);
            }
        }

        if(!$('#bbs_title').val()){
            alert("제목을 입력해 주세요.");
            return
        }

        if(!$('#bbs_cnts').val()){
            alert("내용을 입력해 주세요.");
            return
        }

        // 로딩바
        $('#status').fadeIn();
        $('#preloader').delay(200).fadeIn();

        $.ajax({
            cache: false,
            url: "/html/process.html", // 요기에
            type: 'POST',
            async: true,
            data: formData,
            contentType: false,
            processData: false,
            headers: {'cache-control': 'no-cache', 'pragma': 'no-cache'},
            success: function (data) {
                if (data == 'Success' || data == 'Auth') {

                    if(idx) location.reload();
                    else location.href = 'board.html' + params;

                } else {
                    alert(data);
                    $('#status').fadeOut();
                    $('#preloader').delay(200).fadeOut();
                }
            }, // success

            error: function (xhr, status) {
                alert(xhr + " : " + status);
            }
        }); // $.ajax */
    })


    /* 게시글 답변 등록 및 수정 */
    $('#brd-answer').on("click",function(){

        var formData = new FormData($('#basicForm')[0]);
        let idx = $('#idx').val();
        let bType = $('input[name=bType]').val();
        let page = $('input[name=page]').val();
        let key = $('input[name=key]').val();
        let keyword = $('input[name=keyword]').val();

        let params="?idx="+idx;
        if(bType) params += "&bType="+bType;
        if(page) params += "&np="+page;
        if(key) params += "&key="+key;
        if(keyword) params += "&keyword="+keyword;

        formData.append("mode", 'answer');

        for (var i = 0; i < filesArr.length; i++) {
            // 삭제되지 않은 파일만 폼데이터에 담기
            if (!filesArr[i].is_delete) {
                formData.append("attach_file[]", filesArr[i]);
            }
        }

        // 로딩바
        $('#status').fadeIn();
        $('#preloader').delay(200).fadeIn();

        $.ajax({
            cache: false,
            url: "/html/process.html", // 요기에
            type: 'POST',
            async: true,
            data: formData,
            contentType: false,
            processData: false,
            headers: {'cache-control': 'no-cache', 'pragma': 'no-cache'},
            success: function (data) {
                if (data == 'Success' || data == 'Auth') {

                    location.href = 'board.html' + params;

                } else {
                    alert(data);
                    $('#status').fadeOut();
                    $('#preloader').delay(200).fadeOut();
                }
            }, // success

            error: function (xhr, status) {
                alert(xhr + " : " + status);
            }
        }); // $.ajax */
    })


    /* 확인/승인 */
    $('#brd-complete').on("click",function(){

        let idx = $('input[name=idx]:eq(0)').val();
        let refpage = $('input[name=refpage]:eq(1)').val();
        let opt = $('#opt').text();
        let optVal = $('#brd-complete').text();
        opt = (opt == 'N') ? 'Y' : 'N';
        optVal = (opt == 'N') ? '승인' : '승인취소';

        $.ajax({
            cache : false,
            url : "/html/process.html",
            type : 'POST',
            data : {idx:idx,mode:'agree',opt:opt,refpage:refpage },
            success: function (data) {
                if (data == 'Success' || data == 'Auth') {

                    //$('#opt').text(opt);
                    //$('#brd-complete').text(optVal);
                    location.reload();

                } else alert(data);
            }, // success

            error: function (xhr, status) {
                alert(xhr + " : " + status);
            }
        }); // $.ajax */
    })


    /* 게시물 삭제 */
    $('#brd-delete').on("click",function(){

        let idx = $('input[name=idx]:eq(0)').val();
        let refpage = $('input[name=refpage]:eq(1)').val();

        if(!confirm('삭제하시겠습니까?')) return;

        $.ajax({
            cache : false,
            url : "/html/process.html",
            type : 'POST',
            data : {idx:idx,mode:'delPost',refpage:refpage },
            success: function (data) {
                if (data == 'Success' || data == 'Auth') {

                    location.href = 'board.html';

                } else alert(data);
            }, // success

            error: function (xhr, status) {
                alert(xhr + " : " + status);
            }
        }); // $.ajax */
    })


    /* 코멘트 등록 */
    $('#cmt-register').on("click",function(){

        let idx = $('input[name=idx]:eq(0)').val();
        let refpage = $('input[name=refpage]:eq(1)').val();
        let comment = $('#comment').val();

        if(!comment) return;

        $.ajax({
            cache : false,
            url : "/html/process.html",
            type : 'POST',
            data : {idx:idx,comment:comment,mode:'commentWrite',refpage:refpage },
            success: function (data) {
                if (data == 'Success' || data == 'Auth') {

                    location.reload();

                } else alert(data);
            }, // success

            error: function (xhr, status) {
                alert(xhr + " : " + status);
            }
        }); // $.ajax */
    })


    /* 코멘트 삭제 */
    $('.cmt-delete').on("click",function(){

        let idx = $(this).data('idx');
        let refpage = $('input[name=refpage]:eq(1)').val();

        if(!confirm('삭제하시겠습니까?')) return;

        $.ajax({
            cache : false,
            url : "/html/process.html",
            type : 'POST',
            data : {idx:idx,mode:'commentDel',refpage:refpage },
            success: function (data) {
                if (data == 'Success' || data == 'Auth') {

                    location.reload();

                } else alert(data);
            }, // success

            error: function (xhr, status) {
                alert(xhr + " : " + status);
            }
        }); // $.ajax */
    })


});


