"use strict";

appCore.controller("TipAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Add Tip";
    $scope.createButton = {
        heading: "Add Tip",
        text: "Create Tip",
        icon: "fa-save"
    };
	
    const stateInfo = $state.current;
    $scope.param = $state.params;
    $scope.showAll = false;
    $scope.accounts = [];
	$scope.today = moment().format("DD-MM-YYYY");
    $scope.gstatus = [{label: "Select", value: ""}, {label: "Active", value: "active"}, {label: "Inactive", value: "inactive"},];
    $scope.tip = { 
        id: $scope.param.id,
        title: '',
        body: '',
        valid_till: $scope.today,
        status: "active",
        uploadimage: '',
    };
	
    const forFocus = (d) => $(document).find("#" + d).focus();
    $scope.createTip = () => {
       // const mobilePattern = new RegExp("^[0-9()+ -]*$");
        let data = $scope.tip;
		let attachment = {'data':data,'image':data.uploadimage};
		
        if (!$state.params.id) {
			
            vServices.callApi("tip/add", "POST", data.uploadimage,data).then((res) => {
                if (res && typeof res === 'string') {
                    toastr.success(res, "Success!", {timeOut: 5000});
                    $scope.$root.updateMenu();
                    $state.go("tipList", {}, {reload: true});
					
                }
            });
        }
        else {
            vServices.callApi("tip/edit", "POST", data.uploadimage,data).then((res) => {
                if (res && typeof res === 'string') {
                    toastr.success(res, "Success!", {timeOut: 5000});
                    $scope.$root.updateMenu();
                    $state.go("tipList", {}, {reload: true});
                } else {
                    toastr.error("Edit not working", "Not Working", {timeOut: 5000});
                }
            });
        }

    };

    const init = function () {
        if ($scope.param && typeof $scope.param.id !== "undefined") {
			
			
            $scope.createButton = {heading: "Edit Tip", text: "Edit Tip", icon: "fa-pen"};
            vServices.callApi("tip/tip_single?id=" + $scope.param.id, "GET", {}).then((res) => {
                if (res) {
                    $scope.tip = res;	
                }
            });
        }

    };
    init();
}
]);
