crmApp.controller("SearchUserController", SearchUserController);

function SearchUserController($scope, $window, $q, DTOptionsBuilder, DTColumnBuilder, DTColumnDefBuilder, $filter, $compile, UserSI, AntiForgeryService) {
    var vm = this;
    vm.dtInstanceUser = {};
    AntiForgeryService.cookieToken = $window.cookietoken;
    AntiForgeryService.formToken = $window.formtoken;
    vm.SeksyenUserEnabled = true;
    vm.JabatanUserEnabled = true;
    vm.SeksyenEnabledDocUnit = true;
    vm.JabatanEnabledDocUnit = true;
    vm.show = true;

    //vm.init() = function () {
    //    UserSI.getAdminUserProfileList().then(function (r) {
    //        if (r.status == 200) {
    //            vm.userlist = r.data;
    //        }
    //    });
    //}

    getDDLOptions().then(function (data) {

        for (var key in data) {
            vm[key] = data[key];
        }

        vm.WorkflowAssignToSeksyenOptionDocUnit = data["WorkflowAssignToSeksyenOption"];
        vm.WorkflowAssignToJabatanOptionDocUnit = data["WorkflowAssignToJabatanOption"];
        vm.WorkflowAssignToCawanganOptionDocUnit = data["WorkflowAssignToCawanganOption"];
        vm.WorkflowAssignToUnitOptionDocUnit = data["WorkflowAssignToUnitOption"];

        vm.WorkflowAssignToSeksyenUserOption = data["WorkflowAssignToSeksyenOption"];
        vm.WorkflowAssignToJabatanUserOption = data["WorkflowAssignToJabatanOption"];
        vm.WorkflowAssignToCawanganUserOption = data["WorkflowAssignToCawanganOption"];
        vm.WorkflowAssignToUnitUserOption = data["WorkflowAssignToUnitOption"];
    });

    vm.dtColumnsUser = [

            DTColumnBuilder.newColumn('LoginID').withTitle('Login ID'),
            DTColumnBuilder.newColumn('DisplayName').withTitle('Nama').renderWith(function (data, type, full) {
                if (data == "") {
                    return '-';
                }
                else {
                    return '<a target="_blank" href="/User/profile/detail/' + full.UserProfileID + '">' + data + '</a>';
                }
            }),
            DTColumnBuilder.newColumn('UnitName').withTitle('Jabatan/Cawangan/Unit').renderWith(function (data, type, full) {
                if (data == "") {
                    return '-';
                }
                else {
                    return '<b><div>'+ data + '</div></b><span><small>' + full.AdditionalInfo + ' </small></span>';
                }
            }),
            DTColumnBuilder.newColumn('ManagerDisplayName').withTitle('Pengurus Unit').renderWith(function (data, type, full) {
                if (data == "") {
                    return '-';
                }
                else {
                    return '<a target="_blank" href="/User/profile/detail/' + full.ManagerUserProfileID + '">' + data + '</a>';;
                }
            }),
            DTColumnBuilder.newColumn('IsActive').withTitle('Aktif?').renderWith(function (data, type, full) {
                if (data) {
                    return '<i class="fa fa-check" ></i>';
                }
                else {
                    return '<i class="fa fa-close" ></i>';;
                }
            }),
    ];

    var ajaxCallbackUser = function (data, callback, settings) {

        data.UserDislayName = vm.Name == undefined ? "" : vm.Name; 
        data.ManagerUserProfileID = (vm.ManagerProfileID == undefined) ? 0 : vm.ManagerProfileID.UserProfileID == 0 ? 0 : vm.ManagerProfileID.UserProfileID,
        data.UnitID = (vm.DefaultUnitID == undefined) ? 0 : vm.DefaultUnitID.ID == "0" ? 0 : parseInt(vm.DefaultUnitID.ID)

        UserSI.getAdminUserProfileList(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    vm.dtOptionsUser = DTOptionsBuilder.newOptions()
        .withOption('ajax', ajaxCallbackUser)
        .withDataProp('data')
        .withOption('processing', true)
         .withOption('searching', true)
        .withOption('serverSide', true)
        .withPaginationType('full_numbers')
        .withDisplayLength(10);

    vm.redirectTo = function (url) {

        $window.beforeUnloadCanceled = true;
        $window.location.href = url;
    }

    vm.addNew = function ()
    {
        window.open('/user/Profile/New');
       // vm.redirectTo('/user/Profile/New');
    }

    vm.searchCL = function()
    {
            vm.blnValid = true;

            if (vm.blnValid) {
                vm.dtInstanceUser.reloadData();
            }
    }

    // Filter Default Unit
    vm.ShowFilterOptionDocUnit = function () {

        vm.showdetailDocUnit = !vm.showdetailDocUnit;
    }

    vm.ResetHierarchy = function () {
        vm.SeksyenEnabled = true;
        vm.JabatanEnabled = true;
        vm.NoUserMessage = false;

        vm.SelectedTimbalan = "";
        vm.SelectedJabatan = "";
        vm.SelectedCawangan = "";
        vm.SelectedUnits = "";
        vm.AssignTo = "";

        vm.actWorkflowAssignToJabatanOption = [];
        vm.actWorkflowAssignToCawanganOption = [];
        vm.actWorkflowAssignToUnitOption = [];
        vm.UserResultOption = [];

        vm.JabatanOption = [];
        vm.CawanganOption = [];
        vm.UnitOption = [];


    }

    vm.ResetHierarchyDocUnit = function () {
        vm.SeksyenEnabledDocUnit = true;
        vm.JabatanEnabledDocUnit = true;

        vm.SelectedTimbalanDocUnit = "";
        vm.SelectedJabatanDocUnit = "";
        vm.SelectedCawanganDocUnit = "";
        vm.SelectedUnitsDocUnit = "";
        // vm.AssignTo = "";

        vm.actWorkflowAssignToJabatanOptionDocUnit = [];
        vm.actWorkflowAssignToCawanganOptionDocUnit = [];
        vm.actWorkflowAssignToUnitOptionDocUnit = [];
        //vm.UserResultOption = [];

        vm.JabatanOptionDocUnit = [];
        vm.CawanganOptionDocUnit = [];
        vm.UnitOptionDocUnit = [];
    }

    vm.seksyenChange = function (selectedTimbalan) {

        vm.actWorkflowAssignToJabatanOption = [];

        if (selectedTimbalan != null) {
            var m = $filter('filter')(vm.WorkflowAssignToJabatanOption, { ParentUnitID: parseInt(selectedTimbalan) }, true);

            if (m != undefined && m.length != 0) {

                vm.actWorkflowAssignToJabatanOption = m;
            }


            if (vm.Other == constAssignToTypeUser) {
                SearchUserByUnitID(selectedTimbalan);
            }
        }

    };

    vm.seksyenChangeDocUnit = function (selectedTimbalanDocUnit) {

        vm.actWorkflowAssignToJabatanOptionDocUnit = [];

        if (selectedTimbalanDocUnit != null) {
            var m = $filter('filter')(vm.WorkflowAssignToJabatanOptionDocUnit, { ParentUnitID: parseInt(selectedTimbalanDocUnit) }, true);
            if (m != undefined && m.length != 0) {
                vm.actWorkflowAssignToJabatanOptionDocUnit = m;
            }
        }

    };

    vm.jabatanChange = function (SelectedJabatan) {
        //enable upper hierarchy
        vm.SeksyenEnabled = true;

        vm.actWorkflowAssignToCawanganOption = [];

        if (SelectedJabatan != null) {
            //disable upper hierarchy
            vm.SeksyenEnabled = false;
            var m = $filter('filter')(vm.WorkflowAssignToCawanganOption, { ParentUnitID: parseInt(SelectedJabatan) }, true);

            if (m != undefined && m.length != 0) {

                vm.actWorkflowAssignToCawanganOption = m;
            }

            if (vm.Other == constAssignToTypeUser) {
                SearchUserByUnitID(SelectedJabatan);
            }
        }

    };

    vm.jabatanChangeDocUnit = function (SelectedJabatanDocUnit) {
        //enable upper hierarchy
        vm.SeksyenEnabledDocUnit = true;
        vm.actWorkflowAssignToCawanganOptionDocUnit = [];

        if (SelectedJabatanDocUnit != null) {
            //disable upper hierarchy
            vm.SeksyenEnabledDocUnit = false;
            var m = $filter('filter')(vm.WorkflowAssignToCawanganOptionDocUnit, { ParentUnitID: parseInt(SelectedJabatanDocUnit) }, true);

            if (m != undefined && m.length != 0) {
                vm.actWorkflowAssignToCawanganOptionDocUnit = m;
            }
        }

    };

    vm.cawanganChange = function (SelectedCawangan) {

        //enable upper hierarchy
        vm.SeksyenEnabled = true;
        vm.JabatanEnabled = true;

        vm.actWorkflowAssignToUnitOption = [];

        if (SelectedCawangan != null) {
            //disable upper hierarchy
            vm.SeksyenEnabled = false;
            vm.JabatanEnabled = false;

            var m = $filter('filter')(vm.WorkflowAssignToUnitOption, { ParentUnitID: parseInt(SelectedCawangan) }, true);

            if (m != undefined && m.length != 0) {

                vm.actWorkflowAssignToUnitOption = m;
            }

            if (vm.Other == constAssignToTypeUser) {
                SearchUserByUnitID(SelectedCawangan);
            }

        }

    };
    vm.cawanganChangeDocUnit = function (SelectedCawanganDocUnit) {

        //enable upper hierarchy
        vm.SeksyenEnabledDocUnit = true;
        vm.JabatanEnabledDocUnit = true;

        vm.actWorkflowAssignToUnitOptionDocUnit = [];

        if (SelectedCawanganDocUnit != null) {
            //disable upper hierarchy
            vm.SeksyenEnabledDocUnit = false;
            vm.JabatanEnabledDocUnit = false;

            var m = $filter('filter')(vm.WorkflowAssignToUnitOptionDocUnit, { ParentUnitID: parseInt(SelectedCawanganDocUnit) }, true);

            if (m != undefined && m.length != 0) {
                vm.actWorkflowAssignToUnitOptionDocUnit = m;
            }
        }

    };

    vm.unitChange = function (SelectedUnits) {

        if (vm.Other == constAssignToTypeUser) {
            SearchUserByUnitID(SelectedUnits);
        }


    };

    vm.SelectHierarchy = function () {
        vm.UserResultOption = [];
        m = [];

        if (vm.SelectedUnits != "" && vm.SelectedUnits != null) {
            m = $filter('filter')(vm.WorkflowAssignToUnitOption, { ID: vm.SelectedUnits }, true);
        }
        else if (vm.SelectedCawangan != "" && vm.SelectedCawangan != null) {
            m = $filter('filter')(vm.WorkflowAssignToCawanganOption, { ID: vm.SelectedCawangan }, true);
        }
        else if (vm.SelectedJabatan != "" && vm.SelectedJabatan != null) {
            m = $filter('filter')(vm.WorkflowAssignToJabatanOption, { ID: vm.SelectedJabatan }, true);
        }

        else if (vm.SelectedTimbalan != "" && vm.SelectedTimbalan != null) {
            m = $filter('filter')(vm.WorkflowAssignToSeksyenOption, { ID: vm.SelectedTimbalan }, true);
        }

        if (m && (m.length != 0)) {

            vm.UserResultOption = m;
            vm.AssignTo = m[0];
        }

    }

    vm.SelectHierarchyDocUnit = function () {
        vm.UnitPenerimaanDocOption = [];
        m = [];

        if (vm.SelectedUnitsDocUnit != "" && vm.SelectedUnitsDocUnit != null) {
            m = $filter('filter')(vm.WorkflowAssignToUnitOptionDocUnit, {
                ID: vm.SelectedUnitsDocUnit
            }, true);
        }
        else if (vm.SelectedCawanganDocUnit != "" && vm.SelectedCawanganDocUnit != null) {
            m = $filter('filter')(vm.WorkflowAssignToCawanganOptionDocUnit, {
                ID: vm.SelectedCawanganDocUnit
            }, true);
        }
        else if (vm.SelectedJabatanDocUnit != "" && vm.SelectedJabatanDocUnit != null) {
            m = $filter('filter')(vm.WorkflowAssignToJabatanOptionDocUnit, {
                ID: vm.SelectedJabatanDocUnit
            }, true);
        }

        else if (vm.SelectedTimbalanDocUnit != "" && vm.SelectedTimbalanDocUnit != null) {
            m = $filter('filter')(vm.WorkflowAssignToSeksyenOptionDocUnit, {
                ID: vm.SelectedTimbalanDocUnit
            }, true);
        }
        if (m && (m.length != 0)) {
            vm.UnitPenerimaanDocOption = m;
            vm.DefaultUnitID = m[0];
        }
    }

    vm.SearchUnitByNameDocUnit = function (query) {
        vm.UnitPenerimaanDocOption = [];
        vm.UnitDocloading = true;

        if (query.length > 2) {
            UserSI.searchUnitByName(query).then(function (r) {
                if (r.data != null) {
                    var m = r.data;
                    if (m.length != 0) {
                        vm.UnitPenerimaanDocOption = m;
                        vm.userList.DefaultUnitID = m[0];
                    }
                } vm.UnitDocloading = false;
            });
        }
    }

    vm.SearchJabatanUnit = function (query) {
        vm.JabatanUnitOption = [];
        if (query.length > 1) {
            vm.JabatanUnitOption = vm.WorkflowAssignToJabatanOptionDocUnit;
        }
    }

    vm.SearchCawanganUnit = function (query) {
        vm.CawanganUnitOption = [];
        if (query.length > 1) {
            vm.CawanganUnitOption = vm.WorkflowAssignToCawanganOptionDocUnit;
        }
    }
    // Filter Default Unit

    //Filter user group
    vm.seksyenUserChange = function (selectedTimbalanUser) {
        vm.filteredWorkflowAssignToJabatanUserOption = [];
        if (selectedTimbalanUser != null) {
            var m = $filter('filter')(vm.WorkflowAssignToJabatanUserOption, { ParentUnitID: parseInt(selectedTimbalanUser) }, true);
            vm.filteredWorkflowAssignToJabatanUserOption = [];
            if (m != undefined && m.length != 0) {
                vm.filteredWorkflowAssignToJabatanUserOption = m;
            }

            SearchUserByUnitID(selectedTimbalanUser);
        }
        else { vm.AssignToUser = ""; }
    };

    vm.jabatanUserChange = function (SelectedJabatanUser) {
        //enable upper hierarchy
        vm.SeksyenUserEnabled = true;

        vm.filteredWorkflowAssignToCawanganUserOption = [];

        if (SelectedJabatanUser != null) {
            //disable upper hierarchy
            vm.SeksyenEnabled = false;
            var m = $filter('filter')(vm.WorkflowAssignToCawanganUserOption, { ParentUnitID: parseInt(SelectedJabatanUser) }, true);

            if (m != undefined && m.length != 0) {

                vm.filteredWorkflowAssignToCawanganUserOption = m;
            }
            SearchUserByUnitID(SelectedJabatanUser);

        } else { vm.AssignToUser = ""; }
    };

    vm.cawanganUserChange = function (SelectedCawanganUser) {
        //enable upper hierarchy
        vm.SeksyenUserEnabled = true;
        vm.JabatanUserEnabled = true;

        vm.filteredWorkflowAssignToUnitUserOption = [];

        if (SelectedCawanganUser != null) {
            //disable upper hierarchy
            vm.SeksyenUserEnabled = false;
            vm.JabatanUserEnabled = false;

            var m = $filter('filter')(vm.WorkflowAssignToUnitUserOption, { ParentUnitID: parseInt(SelectedCawanganUser) }, true);

            if (m != undefined && m.length != 0) {
                vm.filteredWorkflowAssignToUnitUserOption = m;
            }
            SearchUserByUnitID(SelectedCawanganUser);
        } else { vm.AssignToUser = ""; }
    };

    vm.unitUserChange = function (SelectedUnitsUser) {
        SearchUserByUnitID(SelectedUnitsUser);
    };

    function SearchUserByUnitID(selectedUnitID) {
        if (selectedUnitID) {
            vm.NoUserMessage = false;
            vm.UserResultOption = [];
            vm.AssignToUser = "";
            UserSI.searchUserByUnitID(selectedUnitID).then(function (r) {
                if (r.status === 200) {
                    if (r.data) {
                        vm.UserResultOption = r.data;

                        if (vm.UserResultOption.length == 0) {
                            vm.NoUserMessage = true;
                        }
                    }
                }
            });
        }
    }

    vm.SearchUserByName = function (query) {
        vm.UserResultOption = [];
        vm.loading = true;

        if (query.length > 2) {
            UserSI.searchUserByName(query).then(function (r) {
                if (r.data != null) {
                    var m = r.data;
                    if (m.length != 0) {
                        vm.UserResultOption = m;
                    }
                } vm.loading = false;
            });
        }

    }

    vm.ShowFilterUserOption = function () {
        if (!vm.showUserdetail) {
            vm.showUserdetail = true;
        }
        else {
            //vm.ResetUserHierarchy();
            vm.showUserdetail = false;
        }
    }

    vm.SearchJabatanUser = function (query) {
        vm.JabatanUserOption = [];
        if (query.length > 1) {
            vm.JabatanUserOption = vm.WorkflowAssignToJabatanUserOption;
        }
    }

    vm.SearchCawanganUser = function (query) {
        vm.CawanganUserOption = [];
        if (query.length > 1) {
            vm.CawanganUserOption = vm.WorkflowAssignToCawanganUserOption;
        }
    }

    vm.ResetUserHierarchy = function () {
        vm.SeksyenUserEnabled = true;
        vm.JabatanUserEnabled = true;
        vm.NoUserMessage = false;

        vm.SelectedTimbalanUser = "";
        vm.SelectedJabatanUser = "";
        vm.SelectedCawanganUser = "";
        vm.SelectedUnitsUser = "";
        vm.AssignToUser = "";

        vm.filteredWorkflowAssignToJabatanUserOption = [];
        vm.filteredWorkflowAssignToCawanganUserOption = [];
        vm.filteredWorkflowAssignToUnitUserOption = [];
        vm.UserResultOption = [];

        vm.JabatanUserOption = [];
        vm.CawanganUserOption = [];
        vm.UnitOption = [];

        vm.showdetailDocUnit = false;
        vm.showUserdetail = false;
    }

    vm.SearchUserJabatanUnit = function (query) {
        vm.UserJabatanUnitOption = [];
        if (query.length > 1) {
            vm.UserJabatanUnitOption = vm.WorkflowAssignToJabatanUserOption;
        }
    }

    vm.SearchUserCawanganUnit = function (query) {
        vm.UserCawanganUnitOption = [];
        if (query.length > 1) {
            vm.UserCawanganUnitOption = vm.WorkflowAssignToCawanganUserOption;
        }
    }
    //Filter user group


    function getDDLOptions() {
        var deferred = $q.defer();
        UserSI.getUserGroupHierarcyOptions().then(function (r) {
            if (r.status === 200) {
                deferred.resolve(r.data);
            }
        });
        return deferred.promise;
    }

    vm.clearUserSearch = function()
    {
        vm.DefaultUnitID = "";
        vm.ManagerProfileID = "";
        vm.Name = "";

        vm.ResetUserHierarchy();
        vm.ResetHierarchy();

        vm.blnValid = false;
        //vm.dtInstanceUser.reloadData();

        //vm.SeksyenUserEnabled = true;
        //vm.JabatanUserEnabled = true;
        //vm.SelectedTimbalanUser = "";
        //vm.SelectedJabatanUser = "";
        //vm.SelectedCawanganUser = "";
        //vm.SelectedUnitsUser = "";

        //vm.SeksyenEnabledDocUnit = true;
        //vm.JabatanEnabledDocUnit = true;
        //vm.SelectedTimbalan = "";
        //vm.SelectedJabatan = "";
        //vm.SelectedCawangan = "";
        //vm.SelectedUnits = "";

        //vm.showUserdetail = false;
        //vm.showdetailDocUnit = false;
    }

}