"use strict";

appCore.controller("importExcelCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Import CSV";
    $scope.uploadimage = '';
    $scope.data = [];
    $scope.sortedArray = [];
    $scope.cols = [];
    $scope.cot = [{
                    'col' : 0 
                  },{
                    'col' : 1  
                  },{
                    'col' : 2 
                  },{
                    'col' : 3 
                  },{
                    'col' : 4 
                  },{
                    'col' : 5 
                  },{
                    'col' : 6
                  },{
                    'col' : 7
                  },{
                    'col' : 8
                  },{
                    'col' : 9
                  },{
                    'col' : 10
                }];
    $scope.data2 = [];
    $scope.uploadFile = () =>{
        var file = {'uploadimage':$scope.uploadimage};
        vServices.callApi("datamigration/importExcel", "POST",  {} , file).then((res) => {
            if (res) {
                let arr1 = [];
                $scope.data2 = $scope.transposeXY(res.data);
                $scope.data = res.conData;
            }
        });
    }

    $scope.createData = () =>{
        var colx = $scope.cot;
        var datax =  $scope.data2;
        let saveDataArr = [];
        var a = {};
        for(let i = 0; i<colx.length;i++){
          if(colx[i].col==0){
            a = datax[i].map(x=>{
              return ({'emp_first_name':x});
            })
          }
          if(colx[i].col==1){
            a = datax[i].map(x=>{
              return ({'emp_last_name':x});
            })
          }
          if(colx[i].col==2){
            a = datax[i].map(x=>{
              return ({'emp_email':x});
            })
          }
          if(colx[i].col==3){
            a = datax[i].map(x=>{
              return ({'emp_mobile':x});
            })
          }
          if(colx[i].col==4){
            a = datax[i].map(x=>{
              return ({'emp_revenue_center':x});
            })
          }
          if(colx[i].col==5){
            a = datax[i].map(x=>{
              return ({'emp_doj':x});
            })
          }
          if(colx[i].col==6){
            a = datax[i].map(x=>{
              return ({'emp_dob':x});
            })
          }
          if(colx[i].col==7){
            a = datax[i].map(x=>{
              return ({'emp_country':x});
            })
          }
          if(colx[i].col==8){
            a = datax[i].map(x=>{
              return ({'emp_code':x});
            })
          }
          if(colx[i].col==9){
            a = datax[i].map(x=>{
              return ({'emp_dept':x});
            })
          }
          if(colx[i].col==10){
            a = datax[i].map(x=>{
              return ({'emp_designation':x});
            })
          }
          saveDataArr.push(a);
        };
        
        const finalData = $scope.transposeXY(saveDataArr);
        var lct = [];
        for(let i = 0;i<finalData.length;i++){
          var t = finalData[i];
          t.push({'createdAt':'NOW()'});
          t.push({'updatedAt':'NOW()'});
          lct.push(t.reduce(((r, c) => Object.assign(r, c)), {}));
        }
        vServices.callApi("datamigration/saveData", "POST",  {'csv':JSON.stringify(lct)}).then((res) => {
          if(res){
            toastr.success(res + " heroes has bee inserted","Success");
          }
          else{
            toastr.error("Already exists.","Error");
          }
        });
    }


    $scope.transposeXY = (a) => {

        // Calculate the width and height of the Array
        var w = a.length || 0;
        var h = a[0] instanceof Array ? a[0].length : 0;
      
        // In case it is a zero matrix, no transpose routine needed.
        if(h === 0 || w === 0) { return []; }
      
        /**
         * @var {Number} i Counter
         * @var {Number} j Counter
         * @var {Array} t Transposed data is stored in this array.
         */
        var i, j, t = [];
      
        // Loop through every item in the outer array (height)
        for(i=0; i<h; i++) {
      
          // Insert a new row (array)
          t[i] = [];
      
          // Loop through every item per item in outer array (width)
          for(j=0; j<w; j++) {
      
            // Save transposed data.
            t[i][j] = a[j][i];
          }
        }
      
        return t;
      }

    const init = function () {
      console.log($scope.cot);

    };
    init();
}
]);
