"use strict";
appCore.controller("ContactListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document",
    function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
      $scope.$root.page.title = "List of Contacts";
      $scope.contacts = [];

      $scope.deleteContact = (id) => {
        alert(id);
        vServices.callApi("contact/delete/" + id, "DELETE", {}).then(function (res) {
          if (res) {
            toastr.success(res, "Success");
            $state.go($state.current, {}, {reload: true});
          }
        });
      };

      const init = function () {
        vServices.callApi("contact/all", "GET", {}).then(function (res) {
          if (res && res.length > 0) {
            $scope.contacts = res;
            $scope.$root.callDataTable("#contactList");
          }
        });
      };
      init();

      /* Model code start*/
      $scope.deleteContactModel = (obj) => {
        let modalInstance = $uibModal.open({
          animation: true,
          ariaLabelledBy: "modal-title",
          ariaDescribedBy: "modal-body",
          templateUrl: "DeleteContact.html",
          controller: "DeleteContactCtrl",
          controllerAs: "$scope",
          size: "sm",//sm md lg
          backdrop: "static",
          resolve: {
            data: () => {
              return {
                contact: obj
              };
            }
          }
        });

        modalInstance.result.then((data) => {
          $scope.deleteContact(data.contact.contactid);
        }, () => {
          toastr.warning("Action has been canceled", "Cancel");
        });
      };
      /* Model code end*/

    }
  ])
  .controller("DeleteContactCtrl", function ($uibModalInstance, data, $scope) {
    $scope.data = data;
    $scope.ok = () => $uibModalInstance.close($scope.data);
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
  });
