var milestoneModal = $('#modal-add-milestone > .modal-dialog > .modal-content');
function addMilestone(id){
    console.log("cek cek id milestone", id);
    if(id && id > 0){
        $("#milestone_parent").val(id);
    }
    $("#modal-add-milestone").modal("show");
}

function resetFormMilestone(){
    $('#select-milestone').val(null).trigger("change");
    $('#select-milestone').find('option').remove();
    $('#select-milestone').val("");
    $("#milestone_parent").val("");
    $("#name_milestone").val("");
    $('#form-milestone').trigger("reset");
    $('#submit-milestone').html('Add');
    $('#submit-milestone').prop("disabled",false);
    $("#modal-add-milestone").modal("hide");
    
}

$(document).ready(function () {
    $('#select-milestone').select2({
        dropdownParent: milestoneModal,
        selectOnBlur: true,
        placeholder: 'Pilih / masukan milestone',
        allowClear:true,
        ajax: {
            url: `${base_url}project-milestone/select`,
            dataType: 'json',
            data: function (params) {
                var query = {
                    search: params.term,
                    type: 'public',
                    idProject:proyekId,
                    idGantt:ganttId
                }
                return query;
            },
            processResults: function (result) {
                return {
                    results:  $.map(result, function (item) {
                        // console.log("cek item", item)
                        return {
                            due_date:item.due_date,
                            deadline:item.deadline,
                            text: item.status,
                            id: item.id
                        }
                    })
                };
            },
            cache: false
        },
        tags: true,
        createTag: function (params) {
            return {
                id: params.term,
                text: params.term,
                newOption: true
            }
        }
    });

    $('#select-milestone').on('select2:select', function (e) {
        var data = e.params.data;
        $("#due_milestone").val(data.due_date);
        $("#deadline_milestone").val(data.deadline);
        $("#name_milestone").val(data.text);
    });
    

    $('#modal-material').on('show.bs.modal', function (event) {

    });
    
    $('#modal-material').on('hide.bs.modal', function (event) {
        resetFormMilestone();
    });

    // form-milestone
    $("#form-milestone").on('submit', function (e) {
        e.preventDefault();
        $('#submit-milestone').html('Add..');
        $('#submit-milestone').prop("disabled",true);
        var data = Object.fromEntries(new FormData(this));
        console.log("cek data form", data);
        let parent = data.milestone_parent;
        var taskId = 0;
        var start = moment(data.due_date, "YYYY-MM-DD");
        var end = moment(data.deadline, "YYYY-MM-DD");
        let duration = moment.duration(start.diff(end)).asDays();
        let nameMilestone = data.status!="" ? data.status : data.id_milestone
        // console.log("cek duration", Math.abs(duration));
        if(parent && parseInt(parent) > 0){
            taskId = gantt.addTask({
                id: Math.floor(Math.random() * 1000) + 5000,
                text: nameMilestone,
                start_date: data.due_date,
                end_date: data.deadline,
                duration: Math.abs(duration),
                type: "milestone",
                type_activity:"milestone"
            }, parent);
        }else{
            taskId = gantt.addTask({
                id: Math.floor(Math.random() * 1000) + 5000,
                text: nameMilestone,
                start_date: data.due_date,
                end_date: data.deadline,
                duration: Math.abs(duration),
                type: "milestone",
                type_activity:"milestone"
            });
        }
        
        if(taskId && taskId > 0){
            resetFormMilestone();
            gantt.alert("Add Milestone Success!");
        }else{
            $('#submit-milestone').html('Add');
            $('#submit-milestone').prop("disabled",false);
            gantt.alert("Add Milestone Failed!");
        }
    });
});