<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\User as UserModel;
use app\common\model\Form as FormModel;
use app\common\model\FormItem as FormItemModel;
use app\common\model\FormLog as FormLogModel;
use app\common\model\FormLogval as FormLogvalModel;

class Form extends BaseController
{
	
	// 表单记录
	public function logList()
	{
		$formid = input('formid/d', 0);
		$form = FormModel::with(['formItem'])->findOrEmpty($formid);
		return view('appcenter/form/loglist',[
			'form'	=> $form
		]); 
	}
	
	public function logDetail()
	{
		$logid = input('logid/d', 0);
		$log = FormLogModel::with(['user', 'form'])->findOrEmpty($logid);
		$itemlist = FormItemModel::where(['formid'=>$log->formid])->order(['priority'=>'ASC','id'=>'DESC'])->append(['itype_text'])->select();
		$logvallist = FormLogvalModel::where(['logid'=>$logid])->column('*', 'itemid');
		foreach( $itemlist as $k=>$v ){
			if ($v->itype==5 || $v->itype==8){
				$logvallist[$v->id]['ivalue'] = explode("###", $logvallist[$v->id]['ivalue']);
			}
		}
		return view('appcenter/form/logdetail',[
			'log'			=> $log,
			'itemlist'		=> $itemlist,
			'logvallist'	=> $logvallist
		]); 
	}
	
	public function editLog()
	{
		$id = input('id/d', 0);
		$data = array(
			'remark' 	=> input('remark/s', "")
		);
		$log = FormLogModel::find($id);
		$log->save($data);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function listLog()
	{
		$formid = input('formid/d', 0);
		$itemidarr = FormItemModel::where(['formid'=>$formid])->order(['priority'=>'ASC','id'=>'DESC'])->column('id');
		$itemids = implode(',', $itemidarr);	
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$list = FormLogModel::with(['user', 'formLogval' => function($query) use($itemids) {
					$query->orderRaw("FIELD ( itemid,".$itemids." )");
				}])->where(['formid'=>$formid])->order('id','DESC')->limit($limit)->page($page)->select();
		$count = FormLogModel::where(['formid'=>$formid])->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	public function exportLog()
	{
		$formid = input('formid/d', 0);
		$itemidlist = FormItemModel::where(['formid'=>$formid])->order(['priority'=>'ASC','id'=>'DESC'])->select();
		$xlsData = FormLogModel::with(['user'])->where(['formid'=>$formid])->order('id','DESC')->select()->toArray();
		foreach( $xlsData as $k=>$v ){
			$logvallist = FormLogvalModel::where(['logid'=>$v['id']])->select()->toArray();
			foreach( $logvallist as $valk=>$valv ){
				$xlsData[$k]['item'.$valv['itemid']] = $valv['ivalue'];
			}
		}
		$xlsCell = array(
			array('id','表ID'),
			array('user.realname','姓名'),
			array('user.mobile','手机号'),
			array('remark','备注'),
			array('createtime','提交时间'),
		);
		foreach( $itemidlist as $k=>$v ){
			$xlsCell[] = array('item'.$v->id, $v->title);
		}
		export_excel("表单提交记录",$xlsCell,$xlsData);
	}
	
	public function fieldLog()
	{
		$id = input('id/d', 0);
		$info = FormLogModel::find($id);
		$info->save([
			input('field/s')  => input('value/s', "")
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delLog()
	{
		$idarr = input('idarr/a');
		$log = FormLogModel::whereIn('id',$idarr)->select();
		FormLogvalModel::whereIn('logid',$idarr)->delete(); 
		$result = $log->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	
	// 自定义表单
	public function formList()
	{
		return view('appcenter/form/formlist',[]); 
	}
	
	public function itemForm()
	{
		$id = input('id/d', 0);
		$item = FormItemModel::with(['form'])->findOrEmpty($id);
		if (!$item->isEmpty()){
			$item->options = implode("\r\n", $item->options);
		}
		$formlist = FormModel::order(['id'=>'desc'])->select();
		return view('appcenter/form/itemform',[
			'item'		=> $item,
			'formlist'	=> $formlist
		]);
	}
	
	public function formForm()
	{
		$id = input('id/d', 0);
		$form = FormModel::findOrEmpty($id);
		return view('appcenter/form/formform',[
			'form'		=> $form
		]);
	}
	
	public function editForm()
	{
		$id = input('id/d', 0);
		$data = array(
			'title' 		=> input('title/s', ""),
			'explain' 		=> input('explain/s', ""),
			'usernumber' 	=> input('usernumber/d', 0),
			'sharetil' 		=> input('sharetil/s', ""),
			'sharepic' 		=> input('sharepic/s', "")
		);
		if (empty($id)){
			$form = FormModel::create($data);
		}else{
			$form = FormModel::find($id);
			$form->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function editItem()
	{
		$id = input('id/d', 0);
		$data = array(
			'formid'		=> input('formid/d', 0),
			'title' 		=> input('title/s', ""),
			'imust'			=> input('imust/d', 1),
			'itype'			=> input('itype/d', 1),
			'options'		=> explode("\n", str_replace("\r\n", "\n", input('options/s', ""))),
			'picnumber'		=> input('picnumber/d', 1),
			'placeholder' 	=> input('placeholder/s', ""),
			'priority'  	=> input('priority/d', 0)
		);
		if (empty($id)){
			$search = FormItemModel::create($data);
		}else{
			$search = FormItemModel::find($id);
			$search->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function listForm()
	{
		$limit = input('limit');
		$page = input('page');
		$list = FormModel::withCount(['formItem','formLog'])->order(['id'=>'desc'])->limit($limit)->page($page)->select();
		$count = FormModel::count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}	
	
	public function listItem()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$formid = input('formid/d', 0);
		$map[] = ['formid', '=', $formid];
		$list = FormItemModel::where($map)->order(['priority'=>'asc','id'=>'desc'])->limit($limit)->page($page)->append(['imust_text','itype_text'])->select();
		$count = FormItemModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	public function fieldForm()
	{
		$id = input('id/d', 0);
		$info = FormModel::find($id);
		$info->save([
			input('field/s')  => input('value/s', "")
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldItem()
	{
		$id = input('id/d', 0);
		$info = FormItemModel::find($id);
		$info->save([
			input('field/s')  => input('value/s', "")
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delForm()
	{
		$id = input('id/d', 0);
		$form = FormModel::where('id',$id)->select();
		FormItemModel::where('formid', '=', $id)->delete(); 
		FormLogModel::where('formid', '=', $id)->delete(); 
		FormLogvalModel::where('formid', '=', $id)->delete(); 
		$result = $form->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function delItem()
	{
		$id = input('id/d', 0);
		$item = FormItemModel::where('id',$id)->select();
		$result = $item->delete();
		FormLogvalModel::where('itemid', '=', $id)->delete(); 
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
}