(function () {
  'use strict';
  // Source: src/html/scripts/travel-sales/form.js
  angular.module('travel', ['ui.bootstrap']);
  // Source: src/html/scripts/travel-sales/controllers/search-form.js
  angular.module('travel').controller('travels_SearchFormCtrl', [
    '$scope',
    '$http',
    '$window',
    '$location',
    '$log',
    'settings',
    function ($scope, $http, $window, $location, $log, settings) {
      // $log.info('travels_SearchFormCtrl', settings);
      var query = $scope.query;
      $scope.query.travel = $scope.query.travel || {};
      angular.extend($scope, {
        maxAdults: 4,
        maxChildren: 4,
        search: search,
        toggleCalendar: toggleCalendar,
        calendarsOpen: {},
        options: {
          adults: [
            1,
            2,
            3,
            4
          ],
          children: [
            0,
            1,
            2,
            3,
            4
          ],
          ages: [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
          ],
          durations: [
            5,
            7,
            14,
            15
          ],
          minStartDate: moment().startOf('day').toDate(),
          minEndDate: moment().add(1, 'days').startOf('day').toDate(),
          minEndMonth: moment().add(1, 'days').startOf('month').toDate(),
          datepickerTo: { initDate: '$parent.$parent.options.minEndDate' },
          months: [
            'Janvier',
            'F\xe9rvier',
            'Mars',
            'Avril',
            'Mais',
            'Juin',
            'Juillet',
            'Aout',
            'Septembre',
            'Octobre',
            'Novembre',
            'D\xe9cembre'
          ],
          types: [
            { name: 'Tout' },
            {
              code: 'tour',
              name: 'Circuit'
            },
            {
              code: 'stay',
              name: 'S\xe9jour'
            },
            {
              code: 'club',
              name: 'Club/All Inclusive'
            }
          ],
          vo: {
            countries: settings.vo.default.countries,
            themes: settings.vo.default.themes
          },
          cruise: { countries: settings.cruise.default.countries },
          clubmed: { countries: settings.clubmed.default.countries }
        }
      });
      $scope.query.travel.destination = $scope.options.vo.countries[0];
      $scope.query.travel.from = moment().format('YYYY-MM-DD');
      function search(type) {
        console.log('serach type', type);
        // if ($scope.query.travel.theme.toLowerCase() === 'tout') delete $scope.query.travel.theme;
        if (type === 'OMRA') {
          //from=2018-06-19&to=2018-08-18&lang=fr&type=omra
          var qt = $scope.query.travel;
          // var country = qt.destination.country;
          //var country = qt.destination;
          var q = {
              type: 'OMRA',
              lang: 'fr'
            };
          if (qt.from) {
            q.from = moment(qt.from).format('YYYY-MM-DD');
            // q.to = moment(q.from).add(30, 'days').format('YYYY-MM-DD');
            q.to = moment(q.from).add(2, 'M').format('YYYY-MM-DD');
          }
          var qs = [];
          angular.forEach(q, function (value, key) {
            qs.push(key + '=' + value);
          });
          console.log('$window.location.href', '/ngapps/omra-sales#/?' + qs.join('&'));
          // return ;
          $window.location.href = '/ngapps/omra-sales#/?' + qs.join('&');
        } else if (qt.destination !== undefined) {
          console.log('country variable', qt.destination);
          var qt = $scope.query.travel;
          // var country = qt.destination.country;
          var country = qt.destination;
          var q = {
              country: country.code + ':' + country.name,
              type: qt.type || type || 'vo',
              lang: qt.lang || 'fr'
            };
          if (qt.from) {
            q.from = moment(qt.from).format('YYYY-MM-DD');
            q.to = moment(q.from).add(2, 'M').format('YYYY-MM-DD');  // q.to = moment(q.from).add(60, 'days').format('YYYY-MM-DD');
          }
          if (qt.theme) {
            q.theme = qt.theme;
          }
          var qs = [];
          angular.forEach(q, function (value, key) {
            qs.push(key + '=' + value);
          });
          if ($window.location.pathname === '/voyages') {
            $location.path('/');
            $location.search(q);
          } else {
            if (type === 'VO') {
              $window.location.href = '/ngapps/vo-sales#/?' + qs.join('&');
            } else if (type === 'CLUBMED') {
              $window.location.href = '/ngapps/clubmed-sales#/?' + qs.join('&');
            } else if (type === 'CRUISE') {
              $window.location.href = '/ngapps/cruise-sales#/?' + qs.join('&');
            }
          }
        }
      }
      ;
      function toggleCalendar($event, cal) {
        $event.preventDefault();
        $event.stopPropagation();
        angular.forEach($scope.calendarsOpen, function (value, key) {
          // close others
          $scope.calendarsOpen[key] = false;
        });
        $scope.calendarsOpen[cal] = !$scope.calendarsOpen[cal];
      }
    }
  ]);  /*$http.get('/destinations', { headers: { 'accept': 'application/json' } }).then(function(res) {
            console.log("destination", res.data);
            $scope.searchDestination = res.data;
            $scope.query.travel = $scope.query.travel || {};
            $scope.query.travel.destination = res.data[0];
            $scope.query.travel.from = moment().format('YYYY-MM-DD');
          });*/
       /* {
  "_id": "53a017df7834cd2885869261",
  "_perms": {
    "r": [
      "*"
    ],
    "w": [
      "g:admin"
    ],
    "d": [
      "g:admin"
    ]
  },
  "country": {
    "code": "TR",
    "name": "Turquie"
  },
  "name": "Istanbul"
}*/
       // after select 
       /*
{
  "code": "TR",
  "name": "Turquie"
}
*/
}());