angular.module('push')
.controller('CloudAddConnInfoCtrl', function ($scope, $ionicHistory, $ionicActionSheet, $ionicModal, $ionicPopup, $timeout, UtilService, DockingService, UserService, AuthorityService,
CloudFolderService, ModelService, $ionicScrollDelegate, HandleService, $ionicSlideBoxDelegate, SqliteStorageService, ConfigService) {
if($scope.app){
$scope.setStatusBar(0);
}
var text = document.getElementById("add_list_content");
autoTextarea(text);// 调用
$scope.search = {searchkey: ""};
$scope.level = 0;
$scope.checkfolder = "";//被选中的文件夾id
//记录新建类型 0:首页,1:本地,2:云
if (ModelService.getCreateType() == 0) {
$scope.folderlist = angular.copy(ModelService.getFolderList());
} else if (ModelService.getCreateType() == 1) {
$scope.folderlist = angular.copy(ModelService.getLocalFolderList());
} else {
$scope.folderlist = angular.copy(ModelService.getCloudFolderList());
}
//默认选择文件夹
$scope.checkdefolder = ModelService.getCheckdeFolder();
//获取选择的文件夹的id/localid
if ($scope.checkdefolder.isCloud == 0) {
$scope.checkfolder = $scope.checkdefolder.localid;
}
if ($scope.checkdefolder.isCloud == 1) {
$scope.checkfolder = $scope.checkdefolder.id + "";
}
//筛选选择文件夹
angular.forEach($scope.folderlist, function (value, index) {
if ($scope.checkdefolder.isCloud == 0) {
if (value.localid == $scope.checkdefolder.localid) {
$scope.folderlist[index].value = true;
}
} else {
if (value.id == $scope.checkdefolder.id) {
$scope.folderlist[index].value = true;
}
}
});
//是否是第一次打开 0:是,1:不是
if (ModelService.getIsFirstOpen() == 1) {
$scope.record = ModelService.getConninfo();
} else {
var tempdate = UtilService.formatDate();
$scope.record = {
date: tempdate.formattime3,
conntime: tempdate.formattime,
title: "",
flag: 0,
content: ""
};
}
$scope.imagelist = ModelService.getImageList();//图片
$scope.cardlist = ModelService.getCardList();
angular.forEach($scope.cardlist, function (value, index) {
if (angular.isDefined(value.personlist) && value.personlist.length > 0) {
var contactsrt = [];
angular.forEach(value.personlist, function (val, ind) {
contactsrt.push(val.contact);
});
$scope.cardlist[index].contactsrt = contactsrt.join("、");
}
});
$ionicModal.fromTemplateUrl('templates/modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.modal = modal;
});
//关闭选择文件夹
$scope.closeModel = function () {
$scope.modal.hide();
// angular.forEach($scope.folderlist, function (value, ind) {
// if (value.value) {
// $scope.checkdefolder = value;
// }
// });
};
//选择分类
$scope.chooseNewUndid = function (folder, index) {
angular.forEach($scope.folderlist, function (value, ind) {
$scope.folderlist[ind].value = false;
});
$scope.folderlist[index].value = true;
$scope.checkdefolder = folder;
if (folder.isCloud == 0) {
$scope.checkfolder = folder.localid;
}
if (folder.isCloud == 1) {
$scope.checkfolder = folder.id + "";
}
};
//切换标记
$scope.checkFlag = function () {
$scope.record.flag = $scope.record.flag == 1 ? 0 : 1;
};
var tempimages = [];
var uploadImages = function (temprecord) {
tempimages = [];
var upimages = [];
angular.forEach($scope.imagelist, function (data) {
upimages.push(data.photo_name);
});
UtilService.uploadFile(upimages, 0, "image/jpeg").then(function (response) {
// console.log(response);
angular.forEach(response, function (value) {
if (value.status) {
tempimages.push({
photo_name: value.userPhoto,
original_name: value.originalPhoto,
source_name: value.sourcePhoto,
source_size: value.source_size
});
}
});
$timeout(function () {
if (UtilService.isDefined($scope.checkdefolder.localid)) {
saveConninfoToLocal(temprecord);
} else {
saveInformation(temprecord);
}
}, 100);
}, function () {
if (UtilService.isDefined($scope.checkdefolder.localid)) {
saveConninfoToLocal(temprecord);
} else {
$scope.hideLoadingToast();
UtilService.showMess("网络不给力,请重试");
}
})
};
var saveInformation = function (temprecord) {
temprecord.claid = $scope.checkdefolder.id;
DockingService.newCreateConninfo(temprecord, tempimages, $scope.cardlist).then(function (response) {
HandleService.changeflg = true;
UtilService.showMess("新建记录成功");
if (UserService.viplevel == 1) {
$scope.deductUserMemberCount(3);
}
$scope.hideLoadingToast();
$timeout(function () {
$scope.goback();
}, 1500);
ModelService.resetTempData();
}, function () {
$scope.hideLoadingToast();
UtilService.showMess("网络不给力,请重试");
});
};
$scope.immediateRelease = function () {
if (!UtilService.isDefined($scope.record.title)) {
UtilService.showMess("标题不能为空");
return;
}
var temprecord = angular.copy($scope.record);
var tempstr = $("#appDateTime").val();
if (UtilService.isDefined(tempstr)) {
temprecord.conntime = UtilService.formatTime(tempstr);
} else {
temprecord.conntime = UtilService.formatTime($scope.record.date);
}
if (UtilService.isDefined(temprecord.content)) {
temprecord.content = UtilService.replaceEnterTag(temprecord.content);
}
$scope.showLoadingToast();
if (UtilService.checkNetWork() == "None") {
saveConninfoToLocal(temprecord);
} else if (UtilService.isDefined($scope.checkdefolder.localid)) {
if ($scope.imagelist.length > 0) {
uploadImages(temprecord);
} else {
saveConninfoToLocal(temprecord);
}
} else {
if (UserService.viplevel == 1) {
AuthorityService.isHasEnoughCount(3).then(function (response) {
if (response.isHasEnoughCount == 1) {
if ($scope.imagelist.length > 0) {
uploadImages(temprecord);
} else {
saveInformation(temprecord);
}
} else {
UtilService.showMess("本月新建记录次数已用完");
$scope.hideLoadingToast();
}
}, function () {
UtilService.showMess("网络不给力,请重试");
$scope.hideLoadingToast();
});
}else {
if ($scope.imagelist.length > 0) {
uploadImages(temprecord);
} else {
saveInformation(temprecord);
}
}
}
};
var checknum = 1;
$scope.addisover = 0;
var saveConninfoToLocal = function (temprecord) {
try {
var conninfodate = UtilService.formatDate();
var conninfodatelocalid = conninfodate.timestamp;
//对接信息数据生成
temprecord.localid = conninfodatelocalid + "";
temprecord.localclaid = $scope.checkdefolder.localid;
temprecord.id = 0;
temprecord.claid = $scope.checkdefolder.id;
temprecord.creator = UserService.id;
temprecord.synchrotype = 1;
temprecord.createtime = conninfodate.formattime;
temprecord.updatetime = conninfodate.formattime;
temprecord.creatorcomefrom = ConfigService.comefrom;
temprecord.status = 2;
temprecord.defaultFlag = 0;
temprecord.commcount = 0;
temprecord.favourcount = 0;
temprecord.visitcount = 0;
var tempimagelist = tempimages.length > 0 ? tempimages : $scope.imagelist;
//图片列表数据生成
if (tempimagelist.length > 0) {
checknum++;
}
angular.forEach(tempimagelist, function (value, index) {
tempimagelist[index].localid = conninfodatelocalid + index + "";
tempimagelist[index].localinfoid = conninfodatelocalid + "";
tempimagelist[index].infoid = temprecord.id;
tempimagelist[index].id = 0;
});
//名片(单位)数据生成
if ($scope.cardlist.length > 0) {
checknum++;
}
var personlist = [];
var themelist = [];
var productlist = [];
var iur1list = [];
var iur2list = [];
var calendarlist = [];
var date = new Date();
angular.forEach($scope.cardlist, function (value, index) {
var tempcardlocalid = conninfodatelocalid + index;
date.setSeconds(date.getSeconds() + index);
$scope.cardlist[index].localid = tempcardlocalid + "";
$scope.cardlist[index].localinfoid = conninfodatelocalid + "";
$scope.cardlist[index].id = 0;
$scope.cardlist[index].infoid = temprecord.id;
$scope.cardlist[index].creator = UserService.id;
$scope.cardlist[index].synchrotype = 1;
$scope.cardlist[index].creatorcomefrom = ConfigService.comefrom;
$scope.cardlist[index].createtime = date.Format("yyyy-MM-dd hh:mm:ss");
$scope.cardlist[index].updatetime = date.Format("yyyy-MM-dd hh:mm:ss");
//人员数据生成
angular.forEach(value.personlist, function (value1, ind) {
var tempperson = value1;
tempperson.localid = tempcardlocalid + ind + "";
tempperson.localuid = tempcardlocalid + "";
tempperson.id = 0;
tempperson.uid = 0;
tempperson.infoid = temprecord.id;
personlist.push(tempperson);
});
//主题数据生成
angular.forEach(value.themelist, function (value2, ind) {
var temtheme = value2;
temtheme.localid = tempcardlocalid + ind + "";
temtheme.localuid = tempcardlocalid + "";
temtheme.id = 0;
temtheme.uid = 0;
temtheme.infoid = temprecord.id;
themelist.push(temtheme);
});
//产品数据生成
angular.forEach(value.productlist, function (value2, ind) {
var temproduct = value2;
temproduct.localid = tempcardlocalid + ind + "";
temproduct.localuid = tempcardlocalid + "";
temproduct.id = 0;
temproduct.uid = 0;
temproduct.infoid = temprecord.id;
productlist.push(temproduct);
});
//产学研经历数据生成
angular.forEach(value.iur1list, function (value2, ind) {
var temiur1 = value2;
temiur1.localid = tempcardlocalid + ind + "";
temiur1.localuid = tempcardlocalid + "";
temiur1.id = 0;
temiur1.uid = 0;
temiur1.infoid = temprecord.id;
iur1list.push(temiur1);
});
//重大科研项数据生成
angular.forEach(value.iur2list, function (value2, ind) {
var temiur2 = value2;
temiur2.localid = tempcardlocalid + ind + "";
temiur2.localuid = tempcardlocalid + "";
temiur2.id = 0;
temiur2.uid = 0;
temiur2.infoid = temprecord.id;
iur2list.push(temiur2);
});
//历年申报项目数据生成
angular.forEach(value.calendarlist, function (value2, ind) {
var tempproject = value2;
tempproject.localid = tempcardlocalid + ind + "";
tempproject.localuid = tempcardlocalid + "";
tempproject.id = 0;
tempproject.uid = 0;
tempproject.infoid = temprecord.id;
calendarlist.push(tempproject);
});
});
if (personlist.length > 0) {
checknum++;
}
if (themelist.length > 0) {
checknum++;
}
if (productlist.length > 0) {
checknum++;
}
if (iur1list.length > 0) {
checknum++;
}
if (iur2list.length > 0) {
checknum++;
}
if (calendarlist.length > 0) {
checknum++;
}
// console.log("===========================================");
// console.log(temprecord);
// console.log($scope.imagelist);
// console.log($scope.cardlist);
// console.log(personlist);
// console.log(themelist);
// console.log(productlist);
// console.log(iur1list);
// console.log(iur2list);
// console.log("===========================================");
} catch (e) {
$scope.hideLoadingToast();
}
SqliteStorageService.insertSingleData("conninfo", temprecord).then(function () {
$scope.addisover++;
var tempobj = {
localid: $scope.checkdefolder.localid,
updatetime: conninfodate.formattime
};
SqliteStorageService.updateSingleData("conninfofolder", tempobj);
}, function () {
$scope.hideLoadingToast();
});
if (tempimagelist.length > 0) {
SqliteStorageService.insertBatchData("conninfopicture", tempimagelist).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if ($scope.cardlist.length > 0) {
SqliteStorageService.insertBatchData("conninfounit", $scope.cardlist).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if (personlist.length > 0) {
SqliteStorageService.insertBatchData("conninfoperson", personlist).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if (themelist.length > 0) {
SqliteStorageService.insertBatchData("conninfotheme", themelist).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if (productlist.length > 0) {
SqliteStorageService.insertBatchData("conninfoproduct", productlist).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if (iur1list.length > 0) {
SqliteStorageService.insertBatchData("conninfoiur", iur1list).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if (iur2list.length > 0) {
SqliteStorageService.insertBatchData("conninforesearch", iur2list).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
if (calendarlist.length > 0) {
SqliteStorageService.insertBatchData("yearproject", calendarlist).then(function () {
$scope.addisover++;
}, function () {
$scope.hideLoadingToast();
});
}
};
//监听是否结束
var overwatch = $scope.$watch("addisover", function (newValue, oldValue, scope) {
if (newValue == checknum) {
UtilService.showMess("新建成功");
$scope.hideLoadingToast();
$timeout(function () {
$scope.goback();
}, 1500);
HandleService.changeflg = true;
ConfigService.isedit = 1;
SqliteStorageService.updateEditFlg(1);
ModelService.resetTempData();
} else {
}
});
var saveTempDate = function () {
var tempstr = $("#appDateTime").val();
if (UtilService.isDefined(tempstr)) {
$scope.record.date = tempstr;
}
ModelService.setCheckdeFolder($scope.checkdefolder);
ModelService.setConninfo($scope.record);
ModelService.setImageList($scope.imagelist);
ModelService.setCardList($scope.cardlist);
ModelService.setIsFirstOpen(1);
};
$scope.showAction = function () {
$ionicActionSheet.show({
buttons: [
{text: '添加照片'},
{text: '添加名片'}
],
cancelText: '取消',
buttonClicked: function (index) {
if (index == 0) {
getPictures();
} else if (index == 1) {
saveTempDate();
$scope.go('addListCard');
}
return true;
}
});
};
$scope.openCamera = function () {
if ($scope.imagelist.length >= 18) {
UtilService.showMess("最多选取18张图片");
return;
}
UtilService.getPicture(1).then(function (results) {
// console.log(results);
$scope.imagelist.push({
id: 0,
photo_name: results,
original_name: results,
source_name: results
});
$scope.totalImglength = 600 * $scope.imagelist.length;
$ionicScrollDelegate.$getByHandle("scrollimage").resize();
}, function (err) {
});
};
var verifyStorage = function () {
window.imagePicker.verifyStorage(
function (results) {
if (results == "1") {
getPic();
}
}, function (error) {
}
);
};
var getPic = function () {
if ($scope.imagelist.length >= 18) {
UtilService.showMess("最多选取18张图片");
return;
}
UtilService.getPictureList(18 - $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, source_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.deletePhoto = function (index) {
$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 //结束年份
};
/*$("#appDateTime").mobiscroll($.extend(opt['date'], opt['default']));*/
var optDateTime = $.extend(opt['datetime'], opt['default']);
$("#appDateTime").mobiscroll(optDateTime).datetime(optDateTime);
});
//选择文件夹与列表匹配
$scope.queryCheckFolder = function (folder) {
if (ModelService.getCreateType() == 0) {
if (folder.isCloud == 1) {
return $scope.checkdefolder.id == folder.id;
} else {
return $scope.checkdefolder.localid == folder.localid;
}
} else if (ModelService.getCreateType() == 1) {
return $scope.checkdefolder.localid == folder.localid;
} else {
return $scope.checkdefolder.id == folder.id;
}
};
//权限设置
$scope.Authority = function () {
$ionicActionSheet.show({
buttons: [
{text: '权限设置'}
],
cancelText: '取消',
buttonClicked: function (index) {
$scope.go("Authority");
}
});
};
$scope.addListBack = function () {
ModelService.resetTempData();
$scope.goback();
};
//显示更多名片部分
$scope.showMoreCard = function (index) {
$(".parentWrapCard").eq(index).toggle();
$(".showCardWrap").eq(index).toggle();
$(".changeArrowButton").eq(index).toggleClass("upArrow");
/* var cardText = $(".downArrow").eq(index).text();
if (cardText == '展开') {
$(".downArrow").eq(index).text("收起");
} else {
$(".downArrow").eq(index).text("展开");
}*/
};
var cardDelect = function (cardindex) {
$scope.cardlist.splice(cardindex, 1);
};
//重新编辑名片
$scope.editCardContent = function (cardindex) {
$ionicActionSheet.show({
buttons: [
/* {text: '同步到高校库'},*/
{text: '编辑名片'},
{text: '删除名片'}
],
cancelText: '取消',
buttonClicked: function (index) {
/*if (index == 0) {
$scope.go("buildResourceInit");
}*/
if (index == 0) {
saveTempDate();
$scope.go("addListCard", {index: cardindex});
} else {
cardDelect(cardindex);
}
return true;
}
});
};
//重新编辑参与单位结果
$scope.editThemeContent = function (theme, listindex) {
$ionicActionSheet.show({
buttons: [
{text: '编辑内容'}
],
cancelText: '取消',
buttonClicked: function (index) {
if (index == 0) {
$scope.go("DockingTheme", {theme: theme, index: listindex});
}
}
});
};
//重新编辑参与单位结果
$scope.editUnitContent = function (company, listindex) {
$ionicActionSheet.show({
buttons: [
{text: '编辑内容'}
],
cancelText: '取消',
buttonClicked: function (index) {
if (index == 0) {
$scope.go("ParticipatingUnits", {company: company, index: listindex});
}
}
});
};
//点击图片放大
$scope.bigImage = false; //初始默认大图是隐藏的
$scope.hideBigImage = function () {
$timeout(function () {
if($scope.app){
$scope.setStatusBar(0);
}
$scope.bigImage = false;
}, 400);
};
$scope.shouBigImage = function (index) { //传递一个参数(图片的URl)
if($scope.app){
$scope.setStatusBar(1);
}
$scope.bigImage = true; //显示大图
// console.log(index);
$(function () {
$('div.pinch-zoom').each(function () {
new RTP.PinchZoom($(this), {});
});
});
$ionicSlideBoxDelegate.update();//重绘,让图片显示出来
//图片总数量
setTimeout(function () {
// console.log(parseInt($ionicSlideBoxDelegate.slidesCount()));
// console.log(parseInt($ionicSlideBoxDelegate.currentIndex()));
$ionicSlideBoxDelegate.$getByHandle('slide_detail').slide(index, -5);
// var nowIndex=parseInt($ionicSlideBoxDelegate.currentIndex());
var totalIndex = parseInt($ionicSlideBoxDelegate.slidesCount());
// console.log(index);
$scope.nowIndex = index + 1;
$scope.totalIndex = totalIndex;
//获取图片
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')
}
}
}, 0);
};
$scope.slideHasChanged = function (index) {
// console.log(index);
$scope.nowIndex = index + 1;
};
//查看原图
$scope.viewOldImg = function (index) {
// console.log($scope.imagelist[index]);
// console.log("查看原图" + index);
$(".bigimage").eq(index).attr("src", $scope.imgUrl + $scope.imagelist[index].source_name);
$timeout(function () {
$scope.imagelist[index].loadsource = true;
}, 20);
};
//返回上一级目录
$scope.backUpperFolder = function () {
ModelService.deleteSelectLevelList($scope.level);
$scope.level--;
if ($scope.level == 0) {
//记录新建类型 0:首页,1:本地,2:云
if (ModelService.getCreateType() == 0) {
$scope.folderlist = angular.copy(ModelService.getFolderList());
} else if (ModelService.getCreateType() == 1) {
$scope.folderlist = angular.copy(ModelService.getLocalFolderList());
} else {
$scope.folderlist = angular.copy(ModelService.getCloudFolderList());
}
} else {
var tempfolder = ModelService.getSelectLevelList()[$scope.level];
if (tempfolder.isCloud == 0) {
queryLocalFolder(tempfolder);
} else {
queryCloudFolder(tempfolder);
}
$scope.currentfoldername = tempfolder.name;
}
$scope.checkfolder = "";
};
//打开下一级目录
$scope.openNextFolder = function (folder) {
//记录本级目录
var tempobj = {
localid: folder.localid,
id: folder.id,
level: folder.level,
isCloud: folder.isCloud,
name: folder.name
};
ModelService.addSelectLevelList(tempobj);
$scope.currentfoldername = folder.name;
$scope.folderlist = [];
$scope.level = folder.level + 1;
if (folder.isCloud == 0) {
queryLocalFolder(folder);
} else {
queryCloudFolder(folder);
}
$scope.checkfolder = "";
};
//查询本地子文件夹
var queryLocalFolder = function (folder) {
var q_sql = "select * from conninfofolder where localparentid = '" + folder.localid + "'";
SqliteStorageService.queryData(q_sql).then(function (response) {
// console.log(response);
$scope.folderlist = response;
}, function () {
});
};
//查询云子文件夹
var queryCloudFolder = function (folder) {
DockingService.newGetConnInfoList(folder.id, folder.level, 1).then(function (response) {
// console.log(response);
$scope.folderlist = response.connFolderlist;
}, function () {
})
};
});