/********************************************************
 Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved.
 创建人   : Ludwig Dong    
 创建时间 : 2017-02-07
 说明     : 市场活动-微信签到控制器
 *********************************************************/
(function () {
	'use strict';
	angular.module('xrmApp')
        .controller('campaignwechatsignController', ['$ionicLoading', '$compile', '$timeout', '$scope', 'wechatsignService', 'rt', function ($ionicLoading, $compile, $timeout, $scope, myService, rt) {

        	function init() {
        		localStorage.XrmBaseUrl = "http://" + window.location.host + "/";
        		$scope.vm = {};
        		$scope.vm.data = {};
        		$scope.vm.picklist = [];
        		$scope.vm.isoversign = false;
        		var $stateParams = myService.$stateParams;
        		$scope.vm.data.OwnerId = { Id: $stateParams.userid, name: "" };
        		$scope.vm.data.campaignId = { Id: $stateParams.id, name: "" };
        		_getPickList();
        	}
        	init();

        	function _getPickList() {
        		var url = "api/CampaignWeChatSignIn/GetPicklistData?filtername=gendercode";
        		rt.get(url).then(function (resp) {
        			if (!rt.isNull(resp.data)) {
        				$scope.vm.picklist = resp.data;
        			}
        		});
        	}
        	$scope.vm.signIn = function () {
        		if ($scope.vm.isoversign) {
        			if (document.getElementsByClassName("loadingwechat").length > 0) {
        				document.getElementsByClassName("loadingwechat")[0].className = "loading"
        			}
        			return rt.showErrorToast("已经完成签到了哦");
        		}
        		rt.showLoadingToast("提交中...");
        		if (rt.isNullOrEmptyString($scope.vm.data.name)) {
        			return rt.showErrorToast("姓名必填哦");
        		}
        		if (rt.isNullOrEmptyString($scope.vm.data.telephone)) {
        			return rt.showErrorToast("电话必填哦");
        		}
        		if (!rt.isPhoneNumberString($scope.vm.data.telephone)) {
        			return rt.showErrorToast("电话号码要填对哦");
        		}
        		if (rt.isNull($scope.vm.data.gendercode) || rt.isNull($scope.vm.data.gendercode.Value)) {
        			return rt.showErrorToast("性别必填哦");
        		}
        		var url = "api/CampaignWeChatSignIn/SignInFormWeChat";
        		rt.post(url, $scope.vm.data).then(function (resp) {
        			$scope.vm.showSuccessToast();
        			$scope.vm.isoversign = true;
        		}, function (resp) {
        			rt.showErrorToast(resp.data);
        			$scope.vm.isoversign = false;
        		});
        	};
        	/**
			* 弹出操作执行成功的Toast（自定图标）
			*/
        	$scope.vm.showSuccessToast = function () {
        		var template = '<div class="showload_div" ng-init="vm.updatecss()"><img src="../../../../../Asset/_imgs/campaign_wechat_img/submit_success@3x.png" /></div>';
        		var childScope = $scope.$new(false);
        		var content = $compile(template)(childScope);
        		$ionicLoading.show({
        			template: content[0].outerHTML,
        			showBackdrop: false,
        			duration: 2000
        		});
        	};
        	$scope.vm.updatecss = function () {
        		document.getElementsByClassName("loading")[0].className = "loadingwechat"
        	};
        }]);
})();



