"use strict";

appCore.controller("AccountAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Add Account";
    $scope.revenueCenters = [];
    $scope.createButton = {
        heading: "Add Account",
        text: "Create Account",
        icon: "fa-save"
    };
    const stateInfo = $state.current;
    $scope.param = $state.params;
    $scope.gstatus = [{label: "Select", value: ""}, {label: "Active", value: "active"}, {label: "Inactive", value: "inactive"},];
    $scope.account = {
        acc_code: "",
        acc_company_name: "",
        acc_primany_contact: "",//Ref:https://www.quora.com/What-countries-have-a-16-digit-phone-number
        acc_first_name: "",
        acc_last_name: "",
        acc_email: "",
        acc_status: $scope.gstatus[0].value,
        acc_revenue_center: ''
    };

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

    };

    const init = function () {
        vServices.callApi("/account/all_revenue_centers", "GET", {}).then(function (res) {
            if (res && res.length > 0) {
                $scope.revenueCenters = res;
            }
        });
        if ($scope.param && typeof $scope.param.accountid !== "undefined") {
            $scope.createButton = {heading: "Edit Account", text: "Edit Account", icon: "fa-pen"};
            vServices.callApi("account/" + $scope.param.accountid, "GET", {}).then((res) => {
                if (res) {
                    $scope.account = res;

                    for (let i = 0; i < $scope.gstatus.length; i++) {
                        if ($scope.gstatus[i].value === $scope.account.acc_status) {
                            $scope.account.acc_status = $scope.gstatus[i].value;
                        }
                    }
                    $scope.$root.page.title = "Account Edit: " + $scope.account.acc_first_name + ' ' + $scope.account.acc_last_name;
                }
            });
        }
    };
    init();
}
]);
