"use strict";
appCore.controller("HeroesUploadFileCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", function ($state, $scope, $http, $timeout, vServices) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Template";
    $scope.dataList = [];
    $scope.empData = [];
    $scope.editform=false;
    $scope.addform=true;

    $scope.rows = [
        {
            title : '',
            description : '',
            file : {},
            isShow : false
        }
    ];

    $scope.addRow = function() {
        $scope.rows.push({
            title: '', 
            file: {}, 
            description: '',
            isShow: false
        });
    }

    $scope.removeRow = function(index) {
        $scope.rows.splice(index, 1);
    }     
    

    $scope.edit = (d) => {
        $scope.addform=false;
        $scope.editform=true;
        $scope.fileRow= $scope.dataList.filter(emp => emp.id == d);  
    };
    $scope.update = (d) => {
        vServices.callApi("commonfunc/updateHeroFile", "POST" , d)
        .then(function (res) {
                if (res) toastr.success(res, "Success!", {timeOut: 5000});
                $state.go($state.current, {}, {reload: true});
        });
    };

    $scope.delete = (d) => {
        vServices.callApi("commonfunc/deleteHeroesFile?id=" + d, "DELETE" , {})
        .then(function (res) {
                if (res) toastr.success(res, "Success!", {timeOut: 5000});
                $state.go($state.current, {}, {reload: true});
        });
    };


    $scope.add = () => {
        var abc = $scope.rows;
        abc.forEach(i=>{
            i.emp_id = $scope.params.employeeid;
            // let item = {title:i.title,description:i.description,emp_id:$scope.params.employeeid,isShow:i.isShow ? true : false};
            let a = {data:i.file};
            vServices.callApi("commonfunc/addOrEditHeroesFile", "POST", i,a)
            .then(function (res) {
                toastr.success(res, "Success!", {timeOut: 5000});
                window.location.reload();
            });       
        });
    };
    
    const init = function () {
            $scope.dataList = [];
            var emp_id = $scope.params.employeeid;
            vServices.callApi("employee/"+ emp_id, "GET", {}).then((res) => {
                $scope.empData = res;
            });
            vServices.callApi("commonfunc/getAllFileForHero?id="+ emp_id, "GET", {}).then((res) => {
                $scope.dataList = res;
            });
        }
    ;
    init();
}
]);



