"use strict";
appCore.controller("RequisitionAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", function ($state, $scope, $http, $timeout, vServices) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Template";

    $scope.myself_id = $scope.$root.gUser.id;
    $scope.requisition_id = $scope.params.requisition_id;

    $scope.params = $state.params;
    $scope.statusList=[];
    // $scope.$root.page.title = "Manage " + $state.params.type;

    $scope.page = {
            allow: false,
            name: "Requisition",
            type:  $scope.params.requisition_id?"Edit":"Add",
            group:'Salesforce', 
            icon: "fa-users", 
            color: "purple"
        };

    $scope.reset = () => {
    $scope.data = {name: "", desc: ""};
    $scope.page.type = "Add";
    };
    $scope.designationOptions = [];
    $scope.accountOptions = [];
    $scope.projectOptions = [];
    $scope.managerOptions = [];
    $scope.revenueCenters = [];
    $scope.replacement_to_hero = [];

   

    $scope.req = {
        proj_id : 0,
        acc_id : 0,
        desg_id : 0,
        revenue_center_id : 0,
        on_behalf_of : $scope.myself_id=='1' || $scope.myself_id=='400' || $scope.myself_id=='566' || $scope.myself_id=='22' || $scope.myself_id=='163' || $scope.myself_id=='182' ? 0 : $scope.myself_id,
        allocation_hours : 0,
        min_exp : 0,
        max_exp : 0,
        billing_type : 'hourly',
        billing_hours : 0,
        raised_on : moment().format('DD-MM-YYYY'),
        // position_closed_on :  moment().format('DD-MM-YYYY'),
        needed_by : moment().format('DD-MM-YYYY'),
        eng_duration : 0,
        req_status : 1,
        sourcing_status : 'internal',
        description : '',

        revenue_center_id : '0',
        position_type : '',
        replacement_of : '0',
        attechment : '',
        desg_id:'0',

    };
    $scope.req.acc_id = parseInt($scope.req.acc_id.id);

    $scope.add = () => 
    { 
        // $scope.req.acc_id = parseInt($scope.req.acc_id.id);
        // $scope.req.replacement_of = parseInt($scope.req.replacement_of.employeeid);
        // $scope.req.desg_id = parseInt($scope.req.desg_id.id);
        console.log($scope.req);
        var attechment = {'attechment': $scope.req.attechment};
        if ($scope.params.requisition_id!='') {
            $scope.req.mdate = $scope.$root.convertDate2DateTimeUTC(moment().format('DD-MM-YYYY'));
        }else{
            $scope.req.raised_on = $scope.$root.convertDate2DateTimeUTC($scope.req.raised_on);
            $scope.req.needed_by = $scope.$root.convertDate2DateTimeUTC($scope.req.needed_by);
            $scope.req.cdate = $scope.$root.convertDate2DateTimeUTC(moment().format('DD-MM-YYYY'));
            $scope.req.mdate = $scope.$root.convertDate2DateTimeUTC(moment().format('DD-MM-YYYY'));
        }
        vServices.callApi("requisition/addOredit", "POST", $scope.req,attechment).then(function (res){
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go('requisitionDashboard', {}, {reload: true});
        });
    };


    $scope.getProject = (proj_id) =>
    {
        // console.log(proj_id);
        // $scope.req.acc_id = proj_id.id;
        /*Get All designation List*/ 
        if(!isNaN(proj_id)){
            // console.log(proj_id);
            vServices.callApi("requi/projectByAcc?proj_id="+proj_id, "GET", {}).then((res) => {
                if (res) {
                    console.log(res);
                    $scope.projectOptions = res;
                    $scope.projectOptions.unshift({"id": '0', "proj_name": "Select"});
                }
            });
        }
    } 

    const init = function () {

        //---------------------
        if ($scope.params.requisition_id!='') {
            let req_id = $scope.params.requisition_id;
            // console.log(req_id);
            vServices.callApi("requi/allRequisitionByHero/", "GET", {}).then(res => {
                let reqByID = res.filter(reqRow => reqRow.id == req_id);

                console.log(reqByID[0]);
                $scope.req = {
                    id : reqByID[0].id,
                    proj_id : parseInt(reqByID[0].proj_id),
                    acc_id : reqByID[0].acc_id,
                    desg_id : reqByID[0].desg_id,
                    on_behalf_of : parseInt(reqByID[0].on_behalf_of),
                    allocation_hours : reqByID[0].allocation_hours,
                    min_exp : reqByID[0].min_exp,
                    max_exp : reqByID[0].max_exp,
                    billing_type : reqByID[0].billing_type,
                    billing_hours : reqByID[0].billing_hours,
                    raised_on : reqByID[0].raised_on,
                    // position_closed_on :  moment().format('DD-MM-YYYY'),
                    needed_by : reqByID[0].needed_by,
                    eng_duration : reqByID[0].eng_duration,
                    req_status : reqByID[0].req_status,
                    sourcing_status : reqByID[0].sourcing_status,
                    description : reqByID[0].description,
                    revenue_center_id : reqByID[0].revenue_center_id,
                    position_type : reqByID[0].position_type,
                    replacement_of : parseInt(reqByID[0].replacement_of),
                    attechment : reqByID[0].attechment
                };
                // console.log($scope.req);

            })
        }
        //---------------------


            /* Get project list*/
            vServices.callApi("requi/getAllProjects", "GET", {}).then(res => {
                if (res) {
                    $scope.projectOptions = res;
                    $scope.projectOptions.unshift({"id": 0, "proj_name": "Select"});
                    console.log($scope.projectOptions);

                }
            });
            /*Get All designation List*/ 
            vServices.callApi("designation/all", "GET", {}).then((res) => {
                if (res) {
                    $scope.designationOptions = res;
                    $scope.designationOptions.unshift({"id": 0, "desg_name": "Select"});
                    // console.log($scope.designationOptions);
                }
            });
            /*Get All Account List*/ 
            vServices.callApi("requi/getAllAccounts", "GET", {}).then(function (res) {
                if (res) {
                    $scope.accountOptions = res;
                    $scope.accountOptions.unshift({"id": 0, "acc_company_name": "Select", "acc_last_name": ""});
                    // console.log($scope.accountOptions);
                }
            });
            /*Get All Status*/ 
            vServices.callApi("requi/getReqStatusList/", "GET", {}).then((res) => {
                $scope.statusList = res;
            });

            /*Get All Manager List*/
            vServices.callApi("employee/manager/all", "GET", {}).then((res) => {
                if (res && res.length > 0) {
                  $scope.managerOptions = res;
                  $scope.managerOptions.unshift({"id": 0, "emp_first_name": "Select", "emp_last_name": ""});
                } 
            });
            
            /*Get All Revenue Ceneter List*/
            vServices.callApi("/account/all_revenue_centers", "GET", {}).then(function (res) {
                if (res && res.length > 0) {
                    $scope.revenueCenters = res;
                    $scope.revenueCenters.unshift({"revenuecenterid": '0', "rc_name": "Select"});
                }
            }); 
            /*Get All Heroes List*/
            vServices.callApi("employee/all", "GET", {}).then(function (res) {
                if (res && res.length > 0) {
                    $scope.replacement_to_hero = res;
                    $scope.replacement_to_hero.unshift({"employeeid": 0, "emp_first_name": "Select", "emp_last_name": ""});
                }
            });
        
    };
    init();

}
]);



