<?php
@header("Content-type: text/html; charset=utf-8");
require_once(dirname(__FILE__) . '/../common/db.class.php');
require_once(dirname(__FILE__) . '/../common/CacheFactory.php');
require_once(dirname(__FILE__) . '/../common/session_helper.php');
require_once(dirname(__FILE__) . '/../common/function.php');
require_once(dirname(__FILE__) . '/../models/system_config_model.php');
$action=$_GET['action'];
switch ($action){
	case 'setconfig':
		setconfig();
		break;
	case 'switchname':
		switchname();
		break;
	case 'switchphone':
		switchphone();
		break;
	case 'saveextentioncolumn':
		saveextentioncolumn();
		break;
	case 'getextentioncolumn':
		getextentioncolumn();
		break;
	case 'delextentioncolumn':
		delextentioncolumn();
		break;
	case 'saveflagconfig':
		saveflagconfig();
		break;
}
//保存签到配置信息
function saveflagconfig(){
	$params=array();
	$params['reserved_infomation_match']=isset($_POST['reserved_infomation_match'])?intval($_POST['reserved_infomation_match']):1;
	$params['reserved_infomation_verify']=isset($_POST['reserved_infomation_verify'])?intval($_POST['reserved_infomation_verify']):1;
	$params['reserved_infomation_csv_attachmentid']=isset($_POST['reserved_infomation_csv_attachmentid'])?intval($_POST['reserved_infomation_csv_attachmentid']):0;
	$load=Loader::getInstance();
	$load->model('Flag_model');
	if (!empty($_FILES['filecsv']['type'])) {
		setlocale(LC_ALL, 'zh_CN');
		$load->flag_model->clearReservedInfomation();
		processCsv($_FILES['filecsv']['tmp_name']);
		// require_once('../common/FileUploadFactory.php');
		// $fuf=new FileUploadFactory(SAVEFILEMODE);
		// $file=$fuf->SaveFormFile($_FILES['filecsv']);

		$allowtypes='application/vnd.ms-excel';
		// $load=Loader::getInstance();
	    $load->model('Attachment_model');
	    $file=$load->attachment_model->saveFormFile($_FILES['filecsv'],$allowtypes);
		
		$params['reserved_infomation_csv_attachmentid']=$file['id'];
		
	}
	if($params['reserved_infomation_csv_attachmentid']==0){
		$load->flag_model->clearReservedInfomation();
	}
	$return=$load->flag_model->setConfig($params);
	if($return){
		echo returnmsg(1,'导入信息设置修改完成');
		return;
	}
	echo returnmsg(-1,'导入信息设置修改失败');
	return;
}
//处理csv文件,写入数据库
function processCsv($filename){
	$filecontent=file_get_contents($filename);
	$encode=mb_detect_encoding($filecontent, array("ASCII",'UTF-8',"GB2312","GBK"));
	// exit();
	$newfilecontent=mb_convert_encoding($filecontent,'UTF-8',$encode);
	$filecontent_arr=explode("\r\n",$newfilecontent);
	$data=array();
	for($i=0,$l=count($filecontent_arr);$i<$l;$i++){
		if(empty($filecontent_arr[$i])) continue;
		$row=str_getcsv($filecontent_arr[$i]);
		array_push($data,$row);
	}

	$reservedinfomation=array();
	$load=Loader::getInstance();
	$load->model('Flag_model');
	for($i=0,$l=count($data);$i<$l;$i++){
		if($i==0)continue;
		$reservedinfomation['realname']=$data[$i][0];
		$reservedinfomation['phone']=$data[$i][1];
		$reservedinfomation['info']=$data[$i][2];
		$load->flag_model->addReservedInfomation($reservedinfomation);
	}
	return true;
}
//删除一个扩展字段
function delextentioncolumn(){
	$id=isset($_GET['id'])?intval($_GET['id']):0;
	if($id<=0){
		echo returnmsg(-1,'数据格式错误');
		return;
	}
	$load=Loader::getInstance();
	$load->model('Flag_model');
	$result=$load->flag_model->deleteExtentionColumnsById($id);
	if($result){
		echo returnmsg(1,'删除成功');
		return;
	}else{
		echo returnmsg(-2,'删除失败');
		return;
	}
}
//获取一个扩展字段设置
function getextentioncolumn(){
	$id=isset($_GET['id'])?intval($_GET['id']):0;
	if($id<=0){
		echo returnmsg(-1,'数据格式错误');
		return;
	}
	$load=Loader::getInstance();
	$load->model('Flag_model');
	$extentioncolumninfo=$load->flag_model->getExtentionColumnsById($id);
	if(!$extentioncolumninfo){
		echo returnmsg(-2,'没有数据');
		return;
	}else{
		if($extentioncolumninfo['coltype']=='select'){
			$options=unserialize($extentioncolumninfo['options']);
			foreach($options as $v){
				$extentioncolumninfo['optionstext'].=$v."\n";
			}
			$extentioncolumninfo['optionstext']=trim($extentioncolumninfo['optionstext']);
		}
		echo returnmsg(1,'',$extentioncolumninfo);
		return;
	}
}
//保存扩展字段
function saveextentioncolumn(){
	$params=array();
	$params['id']=isset($_POST['id'])?intval($_POST['id']):0;
	$params['ordernum']=isset($_POST['ordernum'])?intval($_POST['ordernum']):1;
	$params['title']=isset($_POST['title'])?strval($_POST['title']):'';
	$params['coltype']=isset($_POST['coltype'])?strval($_POST['coltype']):'text';
	$params['placeholder']=isset($_POST['placeholder'])?strval($_POST['placeholder']):'';
	$options=isset($_POST['options'])?strval($_POST['options']):'';
	
	$params['defaultvalue']=isset($_POST['defaultvalue'])?strval($_POST['defaultvalue']):'';
	$params['ismust']=isset($_POST['ismust'])?intval($_POST['ismust']):1;
	

	if(empty($params['title'])){
		echo returnmsg(-1,'字段标题必须填写');
		return;
	}
	if($params['coltype']=='select' and empty($options)){
		echo returnmsg(-2,'类型为选择框时，必须有选项才能正常运行');
		return;
	}

	$options_arr=explode("\n",$options);
	$new_options_arr=array();
	foreach($options_arr as $k=>$v){
		$v=trim($v);
		if(!empty($v)){
			$new_options_arr[]=$v;
		}
	}
	$params['options']=serialize($new_options_arr);
	$params['defaultvalue']=trim($params['defaultvalue']);
	$load=Loader::getInstance();
	$load->model('Flag_model');
	//保存自定义字段
	$return=$load->flag_model->saveExtentionColumn($params);
	if($return){
		echo returnmsg(1,'保存成功');
		return;
	}
	echo returnmsg(-3,'保存失败');
	return;
}
//修改是否填写姓名
function switchname(){
	$status=isset($_POST['status'])?intval($_POST['status']):1;
	$system_config_model=new System_Config_model();
	$result=$system_config_model->set('qiandaosignname',$status);
	if($result){
		$resultdata=array('code'=>1,'message'=>'修改成功');
		echo json_encode($resultdata);
		return;
	}else{
		$resultdata=array('code'=>-1,'message'=>'修改失败');
		echo json_encode($resultdata);
		return;
	}
}
//修改是否填写手机号
function switchphone(){
	$status=isset($_POST['status'])?intval($_POST['status']):1;
	$system_config_model=new System_Config_model();
	$result=$system_config_model->set('qiandaophone',$status);
	if($result){
		$resultdata=array('code'=>1,'message'=>'修改成功');
		echo json_encode($resultdata);
		return;
	}else{
		$resultdata=array('code'=>-1,'message'=>'修改失败');
		echo json_encode($resultdata);
		return;
	}
}
//签到是否需要审核
function setconfig(){
	$key=isset($_GET['key'])?$_GET['key']:'';
	$val=isset($_GET['val'])?intval($_GET['val']):1;
	if(!in_array($key,array('signnameshowstyle','qiandaoshenhe'))){
		echo '{"code":-1,"message":"数据格式错误"}';
		return ;
	}
	$val=($val>3 ||$val<1)?1:$val;
	$system_config_model=new System_Config_model();
	$return=$system_config_model->set($key,$val);
	if($return){
		echo '{"code":1,"message":"修改成功"}';
		return ;
	}else{
		echo '{"code":-2,"message":"修改失败"}';
		return ;
	}
}