angular.module('push')
.controller('technicalBrokerEditTrainCtrl', function ($scope, $stateParams, $ionicActionSheet, TechnicalBrokerPublishTrainService, UtilService, $ionicSlideBoxDelegate,
$timeout, $ionicScrollDelegate, $ionicModal,$ionicHistory,$state) {
// var trainid = $stateParams.trainid;
var today = UtilService.formatDate().formattime3.substring(0, 20);
var trainid = $stateParams.trainid;
// var trainid = 19;
$scope.imagelist = [];
var delimageidlist = [];
// 协办方
$scope.cosponsorList = [{
id: 0,
sid: 0,
stype: 2,
orgtype: 0,
sponsorname: "",
introduce: "",
categorydetail: "",
logo: ""
}];
var delsponsors = [];
// 讲师
$scope.lecturerList = [{
id: 0,
lecturername: ""
}];
var dellecturers = [];
// 初始化培训信息
var initTrainInfo = function () {
TechnicalBrokerPublishTrainService.getTrainInfo(trainid).then(function (response) {
console.log(response);
$scope.trainListobj = {
isUpdate: 2,//1新增 2编辑
id: trainid,
trainbegintime: response.trainModel.trainbegintime,// 培训时间
traintype: response.trainModel.traintype,// 培训种类
traintypename: response.trainModel.traintypename,
title: response.trainModel.title,// 培训标题
content: response.trainModel.content,// 培训内容
signupenddate: response.trainModel.signupenddate,// 报名截止日期
areaid: response.trainModel.areaid,// 培训地区id
areaname: response.trainModel.areaname,// 培训地区名称
detailaddress: response.trainModel.detailaddress,// 培训地址
orgtype: response.trainModel.orgtype,// 机构类别
orgtypename: response.trainModel.orgtypename,// 机构类别名称
categorydetail: response.trainModel.categorydetail,// 领域code
categoryname: response.trainModel.categoryname,// 领域名称
isactive: response.trainModel.isactive,// 培训状态
actives: response.trainModel.isactive == 1,
sponsor: response.trainModel.sponsorlist[0]// 主办方
};
$("#trainDateTime").attr("value", today);
$("#deadlineTime").attr("value", today);
$scope.cosponsorList = response.trainModel.cosponsorlist;// 协办方
$scope.lecturerList = response.trainModel.lecturerlist;// 讲师
$scope.$broadcast("recregistplace", response.trainModel.areaname);
$scope.imagelist = response.trainModel.imagelist;
}, function () {
})
};
initTrainInfo();
// 获取培训种类列表
var buttons = [];
var getTraintypeList = function () {
TechnicalBrokerPublishTrainService.getTraintypeList().then(function (response) {
if (UtilService.isDefined(response.traintypeList) && response.traintypeList.length > 0) {
angular.forEach(response.traintypeList, function (value, index) {
var temphtml = {
text: '' + value.itemkey + '',
traintype: value.itemvalue,
traintypename: value.itemkey
};
buttons.push(temphtml);
});
}
}, function () {
})
};
getTraintypeList();
// 获取机构类型列表
var orgtypebuttons = [];
var getOrgtypeList = function () {
TechnicalBrokerPublishTrainService.getOrgtypeList().then(function (response) {
if (UtilService.isDefined(response.orgtypeList) && response.orgtypeList.length > 0) {
angular.forEach(response.orgtypeList, function (value, index) {
var temphtml = {
text: '' + value.itemkey + '',
orgtype: value.itemvalue,
orgtypename: value.itemkey
};
orgtypebuttons.push(temphtml);
});
console.log(orgtypebuttons);
}
}, function () {
})
};
getOrgtypeList();
// 获取行业领域列表
var categorybuttons = [];
var getCategoryList = function () {
TechnicalBrokerPublishTrainService.getCategoryList().then(function (response) {
if (UtilService.isDefined(response.categoryList) && response.categoryList.length > 0) {
angular.forEach(response.categoryList, function (value, index) {
var temphtml = {
text: '' + value.name + '',
categorydetail: value.code,
categoryname: value.name
};
categorybuttons.push(temphtml);
});
}
}, function () {
})
};
getCategoryList();
//保存任务信息
var saveflg = 0;
$scope.saveActivitySchedule = function () {
var tempstr = $("#trainbegintime").val();
if (UtilService.isDefined(tempstr)) {
$scope.trainListobj.trainbegintime = UtilService.formatTime(tempstr);
}
if (!UtilService.isDefined($scope.trainListobj.trainbegintime)) {
UtilService.showMess("培训时间不能为空");
return;
}
if (!UtilService.isDefined($scope.trainListobj.traintype) || $scope.trainListobj.traintype == 0) {
UtilService.showMess("请选择培训种类");
return;
}
if (!UtilService.isDefined($scope.trainListobj.sponsor.sponsorname)) {
UtilService.showMess("主办方不能为空");
return;
}
/*if (!UtilService.isDefined($scope.trainListobj.otherCoName)) {
UtilService.showMess("协办方不能为空");
return;
}*/
var tempstr = $("#signupenddate").val();
if (UtilService.isDefined(tempstr)) {
$scope.trainListobj.signupenddate = UtilService.formatTime(tempstr);
}
if (!UtilService.isDefined($scope.trainListobj.signupenddate)) {
UtilService.showMess("培训报名截止时间不能为空");
return;
}
if (!UtilService.isDefined($scope.trainListobj.areaid)) {
UtilService.showMess("培训所在地区不能为空");
return;
}
if (!UtilService.isDefined($scope.trainListobj.detailaddress)) {
UtilService.showMess("培训详细地址不能为空");
return;
}
if (!UtilService.isDefined($scope.trainListobj.title)) {
UtilService.showMess("需求名称不能为空");
return;
}
if (!UtilService.isDefined($scope.trainListobj.content)) {
UtilService.showMess("培训内容不能为空");
return;
}
if (saveflg != 0) {
return;
}
saveflg = 1;
$scope.showLoadingToast();
if ($scope.imagelist.length > 0) {
uploadImages();
} else {
saveInfo();
}
};
var tempimages = [];
var uploadImages = function () {
tempimages = [];
var upimages = [];
angular.forEach($scope.imagelist, function (data) {
if (data.id == 0) {
upimages.push(data.photo_name);
} else {
tempimages.push(data);
}
});
UtilService.uploadFile(upimages, 0, "image/jpeg").then(function (response) {
// console.log(response);
angular.forEach(response, function (value) {
if (value.status) {
tempimages.push({
id: 0,
title: "",
photo_name: value.userPhoto,
original_name: value.originalPhoto,
source_name: value.sourcePhoto,
source_size: value.source_size
});
}
});
$timeout(function () {
saveInfo();
}, 100);
}, function () {
$scope.hideLoadingToast();
UtilService.showMess("网络不给力,请重试");
saveflg = 0;
})
};
var saveInfo = function () {
// console.log($scope.trainListobj);
$scope.trainListobj.sponsors = angular.toJson($scope.cosponsorList);
$scope.trainListobj.delsponsors = delsponsors.join(",");
$scope.trainListobj.lecturers = angular.toJson($scope.lecturerList);
$scope.trainListobj.dellecturers = dellecturers.join(",");
$scope.trainListobj.delimages = delimageidlist.join(",");
TechnicalBrokerPublishTrainService.saveTrainInfo($scope.trainListobj, tempimages).then(function (response) {
// console.log(response);
if (response.isSuccess == 1) {
UtilService.showMess("修改成功");
$timeout(function () {
$scope.goback();
}, 1500);
} else {
UtilService.showMess("网络不给力,请重试");
}
$scope.hideLoadingToast();
saveflg = 0;
}, function () {
UtilService.showMess("网络不给力,请重试");
$scope.hideLoadingToast();
saveflg = 0;
})
};
//选择培训类别
$scope.showTrainStatus = function () {
$ionicActionSheet.show({
buttons: buttons,
cancelText: '取消',
buttonClicked: function (index) {
$scope.trainListobj.traintype = buttons[index].traintype;
$scope.trainListobj.traintypename = buttons[index].traintypename;
return true;
}
});
};
//选择机构类型
$scope.showOrgTypeStatus = function () {
$ionicActionSheet.show({
buttons: orgtypebuttons,
cancelText: '取消',
buttonClicked: function (index) {
$scope.trainListobj.sponsor.orgtype = orgtypebuttons[index].orgtype;
$scope.trainListobj.sponsor.orgtypename = orgtypebuttons[index].orgtypename;
return true;
}
});
};
// 选择专业领域
$scope.showStudyStatus=function () {
$ionicActionSheet.show({
cancelOnStateChange: true,
cssClass: 'action_s',
cancelText: '取消',
buttons: categorybuttons,
buttonClicked: function (index) {
$scope.trainListobj.sponsor.categorydetail = categorybuttons[index].categorydetail;
$scope.trainListobj.sponsor.categoryname = categorybuttons[index].categoryname;
return true;
},
destructiveButtonClicked: function () {
return true;
}
});
}
//添加、删除更多协办方
$scope.AddOrDelCosponsor = function (ind) {
if (ind == 0) {
$scope.cosponsorList.push({
id: 0,
sid: 0,
stype: 2,
orgtype: 0,
sponsorname: "",
introduce: "",
categorydetail: "",
logo: ""
});
} else {
if ($scope.cosponsorList[ind].id != 0) {
delsponsors.push($scope.cosponsorList[ind].id);
}
$scope.cosponsorList.splice(ind, 1);
}
};
//添加、删除更多讲师
$scope.AddOrDellecturer = function (ind) {
if (ind == 0) {
$scope.lecturerList.push({
id: 0,
lecturername: ""
});
} else {
if ($scope.lecturerList[ind].id != 0) {
dellecturers.push($scope.lecturerList[ind].id);
}
$scope.lecturerList.splice(ind, 1);
}
};
//接收城市选择数据
var reciveregplace = $scope.$on("registplace", function (event, data) {
$scope.trainListobj.areaid = data.areaid;
$scope.trainListobj.areaname = data.registplace;
});
//页面销毁时 回收广播
$scope.$on('$destroy', function () {
reciveregplace();
});
// 切换状态
$scope.changeToggle = function () {
$scope.trainListobj.isactive = $scope.trainListobj.actives ? 1 : 0;
};
$scope.openCamera = function () {
if ($scope.imagelist.length >= 9) {
UtilService.showMess("最多选取9张图片");
return;
}
UtilService.getPicture(1).then(function (results) {
// console.log(results);
$scope.imagelist.push({id: 0, photo_name: results, original_name: results});
$scope.totalImglength = 600 * $scope.imagelist.length;
$ionicScrollDelegate.$getByHandle("scrollimage").resize();
}, function (err) {
});
};
$scope.show = function () {
$ionicActionSheet.show({
buttons: [
{text: '相册中添加照片'},
{text: '拍照'}
],
cancelText: '取消',
buttonClicked: function (index) {
if (index == 0) {
getPictures();
} else if (index == 1) {
$scope.openCamera();
}
return true;
}
});
};
var verifyStorage = function () {
window.imagePicker.verifyStorage(
function (results) {
if (results == "1") {
getPic();
}
}, function (error) {
}
);
};
var getPic = function () {
if ($scope.imagelist.length >= 9) {
UtilService.showMess("最多选取9张图片");
return;
}
UtilService.getPictureList(9 - $scope.imagelist.length).then(function (results) {
// console.log(results);
angular.forEach(results, function (value, index) {
$scope.imagelist.push({id: 0, photo_name: value, original_name: value})
});
$scope.totalImglength = 600 * $scope.imagelist.length;
$ionicScrollDelegate.$getByHandle("scrollimage").resize();
}, function (err) {
});
};
var getPictures = function () {
if (device.platform == "Android") {
verifyStorage();
} else {
getPic();
}
};
//点击图片放大
$scope.bigImage = false; //初始默认大图是隐藏的
$scope.hideBigImage = function () {
/* $scope.setStatusBar(0);*/
$timeout(function () {
$scope.bigImage = false;
}, 400);
};
$scope.shouBigImage = function (index) { //传递一个参数(图片的URl)
/* $scope.setStatusBar(1);*/
$(function () {
$('div.pinch-zoom').each(function () {
new RTP.PinchZoom($(this), {});
});
});
$scope.bigImage = true;//显示大图
$ionicSlideBoxDelegate.update();//重绘,让图片显示出来
//图片总数量
setTimeout(function () {
$ionicSlideBoxDelegate.$getByHandle('slide_detail').slide(index, -10);
//获取图片
var imgObj = document.getElementsByClassName('bigimage');
var n;
for (n = 0; n < imgObj.length; n++) {
// 获取图片的原始高度和宽度
var oldWid = imgObj[n].naturalWidth;
var oldHei = imgObj[n].naturalHeight;
var screen = document.body.offsetWidth;
var screenH = window.innerHeight;
// console.log(oldWid / oldHei);
// console.log(screen);
var cc = screen / (oldWid / oldHei);
imgObj[n].style.height = cc + 'px';
if (cc < screenH) {
imgObj[n].style.marginTop = (screenH - cc) / 2 + 'px';
// console.log("e:"+imgObj[n].style.marginTop);
} else {
imgObj[n].style.marginTop = 0 + 'px';
// console.log((cc-screenH) +'px')
}
}
}, 10);
};
//删除图片
$scope.deletePhoto = function (index) {
if ($scope.imagelist[index].id != 0) {
delimageidlist.push($scope.imagelist[index].id);
}
$scope.imagelist.splice(index, 1);
};
//培训时间
$(function () {
var currYear = (new Date()).getFullYear();
var opt = {};
opt.date = {preset: 'date'};
opt.datetime = {preset: 'datetime'};
opt.time = {preset: 'time'};
opt.default = {
theme: 'android-ics light', //皮肤样式
display: 'modal', //显示方式
mode: 'scroller', //日期选择模式
dateFormat: 'yyyy年mm月dd日',
lang: 'zh',
showNow: true,
nowText: "今天",
startYear: currYear - 50, //开始年份
endYear: currYear + 10 //结束年份
};
var optDateTime = $.extend(opt['datetime'], opt['default']);
$("#trainbegintime").mobiscroll(optDateTime).datetime(optDateTime);
});
//报名截止日期
$(function () {
var currYear = (new Date()).getFullYear();
var opt = {};
opt.date = {preset: 'date'};
opt.datetime = {preset: 'datetime'};
opt.time = {preset: 'time'};
opt.default = {
theme: 'android-ics light', //皮肤样式
display: 'modal', //显示方式
mode: 'scroller', //日期选择模式
dateFormat: 'yyyy年mm月dd日',
lang: 'zh',
showNow: true,
nowText: "今天",
startYear: currYear - 50, //开始年份
endYear: currYear + 10 //结束年份
};
var optDateTime = $.extend(opt['datetime'], opt['default']);
$("#signupenddate").mobiscroll(optDateTime).datetime(optDateTime);
});
//跳转到添加主办方
$scope.goAddMain = function () {
$scope.go("technicalBrokerAddMainSponsor");
};
$ionicModal.fromTemplateUrl('./modules/technicalBroker/views/technicalBrokerAddMainSponsor.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.mainsponsor = modal;
});
$scope.showView = function () {
$scope.mainsponsor.show();
};
//返回
var gobackflg = 0;
$scope.hideView = function () {
$scope.cosponsorList.push({
id: $scope.trainListobj.sponsor.id,
sid: $scope.trainListobj.sponsor.sid,
stype: 1,
orgtype: $scope.trainListobj.sponsor.orgtype,
sponsorname: $scope.trainListobj.sponsor.sponsorname,
introduce: $scope.trainListobj.sponsor.introduce,
categorydetail: $scope.trainListobj.sponsor.categorydetail,
logo: $scope.trainListobj.sponsor.logo
});
$scope.mainsponsor.hide();
};
$scope.saveSponsor = function () {
$scope.mainsponsor.hide();
};
// 编辑头像
$scope.show_header = function () {
// 弹出头像选择框
$ionicActionSheet.show({
cancelOnStateChange: true,
cssClass: 'action_s',
cancelText: '取消',
buttons: [
{text: "拍照"},
{text: "从相册上传"}
],
buttonClicked: function (index) {
if (index == 0) {
openCamera(1);
} else {
openCamera(0);
}
return true;
},
destructiveButtonClicked: function () {
return true;
}
});
};
var openCamera = function (srcType) {
var options = {
// Some common settings are 20, 50, and 100
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
// In this app, dynamically set the picture source, Camera or photo gallery
sourceType: srcType,
encodingType: Camera.EncodingType.JPEG,
mediaType: Camera.MediaType.PICTURE,
allowEdit: false,
correctOrientation: true //Corrects Android orientation quirks
};
navigator.camera.getPicture(function (imageData) {
$scope.imgurl = imageData;
$scope.showLoadingToast();
UtilService.uploadFile([imageData], 0, "image/jpeg").then(function (response) {
// console.log(response);
$scope.trainListobj.sponsor.logo = response[0].userPhoto;
$scope.hideLoadingToast();
}, function () {
$scope.hideLoadingToast();
});
}, function (message) {
}, options);
};
});