"use strict";

appCore.controller("ContactAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Add Contacts";
    $scope.createButton = {
        heading: "Add Contact",
        text: "Create Contact",
        icon: "fa-save"
    };
    const stateInfo = $state.current;
    $scope.param = $state.params;
    $scope.showAll = false;
    $scope.accounts = [];

    $scope.gstatus = [{label: "Select", value: ""}, {label: "Active", value: "active"}, {label: "Inactive", value: "inactive"},];
    $scope.contact = {
        first_name: "",
        middle_name: "",
        last_name: "",
        department: "",
        description: "",
        email: "",
        phone: "",
        alternate_email: "",
        alternate_phone: "",
        address: "",
        city: "",
        state: "",
        country: "",
        zip: "",
        status: "active",
    };

    const forFocus = (d) => $(document).find("#" + d).focus();
    $scope.createContact = () => {
        const mobilePattern = new RegExp("^[0-9()+ -]*$");
        // console.log(mobilePattern.test($scope.contact.acc_primany_contact));
        /*if (!$scope.contact.acc_first_name) {
          toastr.warning("Please fill first name", "Required", {timeOut: 5000});
          forFocus("acc_first_name");
          return false;
        }*/
        /*if (!$scope.contact.acc_company_name || typeof $scope.contact.acc_company_name === "object") {
          toastr.warning("Please fill company name", "Required", {timeOut: 5000});
          forFocus("acc_company_name");
          return false;
        }*/
        /*if (!$scope.contact.acc_primany_contact) {
          toastr.warning("Please fill mobile number", "Required", {timeOut: 5000});
          forFocus("acc_primany_contact");
          return false;
        }
        if (!mobilePattern.test($scope.contact.acc_primany_contact)) {
          toastr.warning("Please fill valid mobile number", "Required", {timeOut: 5000});
          forFocus("acc_primany_contact");
          return false;
        }
        if (!$scope.contact.acc_email) {
          toastr.warning("Please fill valid email address", "Required", {timeOut: 5000});
          forFocus("acc_email");
          return false;
        }*/
        /*if (!$scope.contact.acc_status) {
          toastr.warning("Please select the contact status", "Required", {timeOut: 5000});
          forFocus("acc_status");
          return false;
        }*/
        if (!$state.params.contactid) {
            vServices.callApi("contact/add", "POST", $scope.contact).then((res) => {
                if (res && typeof res === 'string') {
                    toastr.success(res, "Success!", {timeOut: 5000});
                    $scope.$root.updateMenu();
                    $state.go("contactList", {}, {reload: true});
                }
            });
        }
        else {
            vServices.callApi("contact/edit", "PUT", $scope.contact).then((res) => {
                if (res && typeof res === 'string') {
                    toastr.success(res, "Success!", {timeOut: 5000});
                    $state.go("contactList", {}, {reload: true});
                } else {
                    toastr.error("Edit not working", "Not Working", {timeOut: 5000});
                }
            });
        }

    };

    const init = function () {
        if ($scope.param && typeof $scope.param.contactid !== "undefined") {
            $scope.createButton = {heading: "Edit Contact", text: "Edit Contact", icon: "fa-pen"};
            vServices.callApi("contact/" + $scope.param.contactid, "GET", {}).then((res) => {
                if (res) {
                    $scope.contact = res;
                    $scope.contact.acc_status = $scope.gstatus[1].value;
                    $scope.$root.page.title = "Contact Edit: " + $scope.contact.acc_first_name + ' ' + $scope.contact.acc_last_name;
                }
            });
        }
        vServices.callApi('account/all', 'GET', {}).then((res) => {
            if (res) {
                $scope.accounts = res;
                $scope.accounts.unshift({
                    'acc_id': 0, 'acc_company_name': 'Select', 'acc_primany_contact': ''
                });
            }
        });
    };
    init();
}
]);
