text: angular.module('dog-system')
    .controller('AgendaCtrl', ['ngNotify', 'formatSexFilter', 'ServiceApplication', '$timeout', 'base64', '$scope', 'ServiceProxy', 'ServicePathConstants', 'MessageUtils', '$uibModal', '$rootScope', '$location', 'AngularUtils', '$routeParams',
      function (ngNotify, formatSexFilter, ServiceApplication, $timeout, base64, $scope, ServiceProxy, ServicePathConstants, MessageUtils, $uibModal, $rootScope, $location, AngularUtils, $routeParams) {
        var self = this;
  
        self.agenda = {};
        self.agendas = [];
        self.showAddEditagenda = false;
        self.showList = true;
        self.facePanel = 0;
        self.showFilter = true;
  
        self.buscar = buscar;
        self.buscaragenda = buscaragenda;
        self.recarregar = recarregar;
        self.limpar = limpar;
        self.buscarHoras = buscarHoras;
        self.podeCancelar = podeCancelar;
        self.formatNumber = formatNumber;
        self.formateDate = formateDate;
        self.eventKeyPet = eventKeyPet;
        self.eventKeyService = eventKeyService;
        self.openPopup = openPopup;
        self.setFacePanel = setFacePanel;
        self.edit = edit;
        self.remover = remover;
        self.add = add;
        self.enviar = enviar;
  
        self.gridOptions = {
          columnDefs: getColumnDefs('agenda'),
          rowData: null
        };
  
        var _agendaUrl = ServicePathConstants.PRIVATE_PATH.concat('/agenda');
        var _petUrl = ServicePathConstants.PRIVATE_PATH + '/pet';
  
        self.horas = {
          options: ['08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00']
        };
  
  
        init();
        function init() {
          if (!$rootScope.authDetails.authenticated) {
            $location.path('/login');
          } else {
            if ($routeParams.id) {
              findyPet(base64.decode($routeParams.id));
            }
  
            $timeout(function () {
              self.gridOptions.api.setRowData(undefined);
            }, 500);
          }
        }
  
        function setFacePanel(face) {
          self.facePanel = face;
        }
  
        function findyPet(id) {
          ServiceProxy.find(_petUrl + /id/ + id, function (data) {
            self.agenda.pet = data;
            self.gridOptions.api.setRowData(undefined);
            setFacePanel(1);
          });
        }
  
        function formateDate(date) {
          return AngularUtils.formatDatePtBr(date);
        }
  
        function limpar() {
          self.agenda.schedulingDate = undefined;
          self.agenda.time = undefined;
        }
  
  
        function eventKeyPet($event) {
          if ($event.keyCode == 9) {
            self.openPopup('pet', self.agenda.pet ? self.agenda.pet.name : undefined);
          }
        }
  
        function eventKeyService($event) {
          if ($event.keyCode == 9) {
            self.openPopup('service', (self.agenda.service ? self.agenda.service.name : undefined), self.agenda.pet.size);
          }
        }
  
  
        function recarregar() {
          buscar();
        }
  
        function buscar(ignore) {
          try {
            if (self.inicial == null) {
              throw 'Data inicial é obrigatoria, favor verifique.';
            }
  
            var _url = ServicePathConstants.PRIVATE_PATH + '/agenda/getAgendamentos';
  
            _url = _url.concat('?datainicial=').concat(self.inicial);
            if (self.final) {
              _url = _url.concat('&datafinal=').concat(self.final);
            }
  
            if (self.agenda.pet && self.agenda.pet.id) {
              _url = _url.concat('&pet=').concat(self.agenda.pet.id);
            }
  
            if (self.agenda.service && self.agenda.service.id) {
              _url = _url.concat('&service=').concat(self.agenda.service.id);
            }
  
            if (ServiceApplication.hasPermissionUser()) {
              _url = _url + '&user=' + Number(ServiceApplication.getIdLogado());
            }
  
            ServiceProxy.find(_url, function (data) {
              self.gridOptions.api.setRowData(data);
              var rowData = self.gridOptions.api.getRowNode(0);
              if (rowData) {
                rowData.setSelected(true);
              }
              modifyTela(false);
  
              self.showFilter = false;
  
            });
  
          } catch (error) {
            MessageUtils.error(error);
          }
        }
  
        function formatNumber(number) {
          return parseFloat(number).toFixed(2);
        }
  
        function buscarHoras() {
  
          if (self.agenda.schedulingDate) {
            ServiceProxy.find(_agendaUrl + '/getTime?data=' + self.agenda.schedulingDate, function (data) {
              var options = AngularUtils.toArray(data);
  
              if (options.length == 0) {
                MessageUtils.info('Não existe horário disponível para esse dia, selecione outro dia');
              }
              self.horas = {
                options: options
              };
            });
          } else {
            self.horas = {
              options: undefined
            };
          }
        }
  
        function add() {
          self.agenda = {};
  
          self.agenda = {
            pet: {
              image: {
                filetype: 'image/svg+xml;utf8',
                base64: 'PD94bWw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TU2LjUxMSw3NS44NDFDMTk2LjI4NSwxNDMuODQ1LDIyMS42MzEsMTc3Ljg1OSwyNTIuNzk2LDE3Ny44NTl6IiBmaWxsPSIjMDAwMDAwIi8+CgkJPHBhdGggZD0iTTM0NS41OTUsMTM4LjkxOGMtMjQuOTc1LDAtNDQuNTIxLDI1LjkwMS00NC41MjEsNTguOTY3YzAsMzMuMDUxLDE5LjU1OCw1OC45NTUsNDQuNTIxLDU4Ljk1NSAgICBjMjQuOTYxLDAsNDQuNTMxLTI1LjkwNCw0NC41MzEtNTguOTU1QzM5MC4xMjYsMTY0LjgyLDM3MC41NjgsMTM4LjkxOCwzNDUuNTk1LDEzOC45MTh6IiBmaWxsPSIjMDAwMDAwIi8+CgkJPHBhdGggZD0iTTg5LjA0OCwxOTcuODg1YzAtMzMuMDY1LTE5LjU1OC01OC45NjctNDQuNTIyLTU4Ljk2N0MxOS41NjEsMTM4LjkxOCwwLDE2NC44MiwwLDE5Ny44ODUgICAgYzAsMzMuMDUxLDE5LjU2MSw1OC45NTUsNDQuNTI2LDU4Ljk1NUM2OS40OTEsMjU2Ljg0LDg5LjA0OCwyMzAuOTM2LDg5LjA0OCwxOTcuODg1eiIgZmlsbD0iIzAwMDAwMCIvPgoJPC9nPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+Cjwvc3ZnPgo='
              }
            }
          };
  
          setFacePanel(1);
        }
  
        function modifyTela(condicao) {
          self.showAddEditagenda = condicao;
          self.showList = !condicao;
        };
  
        function buscaragenda() {
          ServiceProxy.find(_agendaUrl + '/' + self.nameagenda, function (data) {
            self.agendas = data;
            modifyTela(false);
          });
        }
  
        function openPopup(tipo, criteria, param) {
          self.isPetInvalido = false;
          self.isServiInvalido = false;
  
          var header = 'Serviços';
          var title = ' o serviço';
          var url = '/services';
  
          if (tipo == 'pet') {
            header = 'Animais';
            title = ' o animal';
            url = '/pet';
  
            if (ServiceApplication.hasPermissionUser()) {
              url = url + '?user=' + Number(ServiceApplication.getIdLogado());
            }
          }
  
          var modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: 'modal-title',
            ariaDescribedBy: 'modal-body',
            templateUrl: 'src/components/popup/popup.html',
            controller: 'ModalInstanceCtrl',
            controllerAs: 'ctrl',
            size: 'lg',
            backdrop: false,
            resolve: {
              data: function () {
                return {
                  columnDefs: getColumnDefs(tipo),
                  url: ServicePathConstants.PRIVATE_PATH + url,
                  title: title
                }
              }
            }
          });
  
          modalInstance.result.then(
            function (data) {
              self.agenda[tipo] = data;
            });
        }
  
        function getColumnDefs(tipo) {
          if (tipo == 'pet') {
            return [
              { headerName: "Código", field: "id", width: 100, cellStyle: { 'text-align': 'right' } },
              { headerName: "Nome pet", field: "name" },
              {
                headerName: "Data Nascimento", field: "dateBirth", valueGetter: function chainValueGetter(params) {
                  return AngularUtils.formatDate(params.data.dateBirth);
                }
              },
              { headerName: "Sexo", field: "sex" },
              {
                headerName: "Usa DogLove", width: 150, suppressFilter: true, field: "usaDogLove",
                cellRenderer: function (params) {
                  var icon = params.data.usaDogLove ? 'fa-toggle-on' : 'fa-toggle-off';
                  return '<i class="fa ' + icon + '" aria-hidden="true" style="font-size: 26px;" ></i>';
                }
  
              },
              { headerName: "Porte", field: "breed.porte" },
              {
                headerName: "Especie", field: "tipoAnimal", width: 129, suppressFilter: true,
                cellRenderer: function (params) {
                  var icon = params.data.breed.tipoAnimal == 'Cão' ? 'dog.svg' : 'cat.svg';
                  return '<img src="img/' + icon + '" style="width: 24px;"></i>';
                }
              },
              { headerName: "Raça", field: "breed.name" },
              { headerName: "Nome propriétario", field: "user.name", width: 265 },
            ];
          }
  
          if (tipo == 'agenda') {
            return [
              { headerName: "Código", field: "id", width: 100, cellStyle: { 'text-align': 'right' } },
              {
                headerName: "Data do agendamento", field: "schedulingDate", cellStyle: { 'text-align': 'right' },
                valueGetter: function chainValueGetter(params) {
                  return AngularUtils.formatDate(params.data.schedulingDate);
                }
              },
              { headerName: "Hora", field: "time", cellStyle: { 'text-align': 'right' }, width: 100 },
              { headerName: "Serviço", field: "service.name" },
              { headerName: "Nome animal", field: "pet.name" },
              { headerName: "Raça", field: "pet.breed.name", width: 150 },
              {
                headerName: "Sexo", field: "pet.sex", width: 100, valueGetter: function (params) {
                  return formatSexFilter(params.data.pet.sex);
                }
              },
              { headerName: "Porte", field: "pet.breed.porte", width: 100 },
              { headerName: "Observações", field: "note", width: 265 },
            ];
          }
  
          if (tipo == 'service') {
            return [
              { headerName: "Código", field: "id", width: 100, cellStyle: { 'text-align': 'right' } },
              { headerName: "Name", field: "name", width: 300 },
              { headerName: "Preço", field: "price", width: 200, cellClass: 'number-cell', valueFormatter: currencyFormatter },
              { headerName: "Porte", field: "size", width: 200 }
            ];
          }
        }
  
        function currencyFormatter(params) {
          return 'R$ ' + AngularUtils.formatNumber(params.value);
        }
  
        function enviar() {
          try {
  
            if (self.agenda.pet.id == undefined) {
              self.isPetInvalido = true;
              throw 'Animal';
            }
            self.isPetInvalido = false;
  
            if (self.agenda.service == undefined) {
              self.isServiInvalido = true;
              throw 'Serviço';
            }
            self.isServiInvalido = false;
  
            if (self.agenda.schedulingDate == undefined) {
              self.isSchedulingDate = true;
              throw 'Data';
            }
            self.isSchedulingDate = false;
  
            if (self.agenda.time == undefined) {
              self.isTime = true;
              throw 'Hora';
            }
            self.isTime = false;
  
            if (angular.isUndefined(self.agenda.id)) {
              ServiceProxy.add(_agendaUrl, self.agenda, function (response) {
                self.gridOptions.api.updateRowData({ add: [response.data] });
                self.agenda = response.data;
  
                var rowData = self.gridOptions.api.getRowNode(0);
                if (rowData) {
                  rowData.setSelected(true);
                }
  
                ngNotify.set(response.atributeMessage.mensagem);
              });
  
            } else {
              ServiceProxy.edit(_agendaUrl, self.agenda, function (response) {
                self.gridOptions.api.refreshCells();
                ngNotify.set(response.atributeMessage.mensagem);
              });
            }
          } catch (error) {
            MessageUtils.error('Campo ' + error + ' é obrigatório');
          }
        }
  
        function edit(param) {
          var selected = self.gridOptions.api.getSelectedRows();
          self.agenda = selected[0];
          if (self.agenda) {
            var schedulingDate = new Date(self.agenda.schedulingDate);
            schedulingDate.setDate(schedulingDate.getDate() + 1);
            self.agenda.schedulingDate = schedulingDate;
            setFacePanel(1);
          } else if (angular.isUndefined(param)) {
            MessageUtils.error('Selecione uma agenda para editar');
          }
        }
  
        function remover() {
          var selectedData = self.gridOptions.api.getSelectedRows();
          self.agenda = selectedData[0];
  
          if (self.agenda) {
            MessageUtils.confirmeDialog('Deseja realmente cancelar esse agendamento?').then(
              function () {
                ServiceProxy.delete(_agendaUrl, agenda, function () {
                  self.gridOptions.api.updateRowData({ remove: selectedData });
                  var rowData = self.gridOptions.api.getRowNode(0);
                  if (rowData) {
                    rowData.setSelected(true);
                  }
                });
              });
          } else {
            MessageUtils.error('Selecione uma agenda para remover');
          }
        }
  
        function podeCancelar(date) {
          var schedulingDate = new Date(date);
          schedulingDate.setDate(schedulingDate.getDate() + 1);
          return schedulingDate > AngularUtils.getToday();
        }
  
        // Disable weekend selection
        function disabled(data) {
          var date = data.date,
            mode = data.mode;
          return mode === 'day' && (date.getDay() === 0);
        }
  
        $scope.dateOptions = {
          dateDisabled: disabled,
          formatYear: 'yy',
          maxDate: new Date(2020, 5, 22),
          startingDay: 1
        };
  
        $scope.dateOptions2 = {
          dateDisabled: false,
          formatYear: 'yy',
          maxDate: new Date(2020, 5, 22),
          minDate: AngularUtils.getToday(),
          startingDay: 1
        };
  
        $scope.open1 = function () {
          $scope.popup1.opened = true;
        };
  
        $scope.popup1 = {
          opened: false
        };
  
        $scope.open2 = function () {
          $scope.popup2.opened = true;
        };
  
        $scope.popup2 = {
          opened: false
        };
  
        $scope.open3 = function () {
          $scope.popup3.opened = true;
        };
  
        $scope.popup3 = {
          opened: false
        };
      }]);
  
  
  