<?php
namespace app\mainapp\controller;

use app\mainapp\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 setForm()
	{
		$formid = input('formid/d', 0);
		$form = FormModel::with(['formItem'])->withCount(['formLog'])->findOrEmpty($formid);
		if ($form->isEmpty()){
			page_result(1, "表单信息不存在。");
		}
		$userid = input('userid/d', 0);
		$user = UserModel::findOrEmpty($userid);
		if ($user->isEmpty()){
			page_result(1, "用户信息不存在");
		}
		$formlog = FormLogModel::create([
			'userid'		=> $userid,
			'formid'		=> $formid,
			'remark'		=> "",
			'createtime'	=> date("Y-m-d H:i:s")
		]);
		$formval = new FormLogvalModel;
		$formvallist = array();
		foreach($form->formItem as $k=>$v){
			$ivalue = trim(input('item'.$v->id.'/s', ""));
			$formvallist[] = array(
				'logid'			=> $formlog->id,
				'formid'		=> $formid,
				'itemid'		=> $v->id,
				'ivalue'		=> $ivalue,
				'createtime'	=> date("Y-m-d H:i:s")
			);
		}
		$formval->saveAll($formvallist);
		page_result(0, "", array(
			'form'			=> $form 
			));
	}
	
	public function getForm()
	{
		$formid = input('formid/d', 0);
		$form = FormModel::with(['formItem'])->withCount(['formLog'])->findOrEmpty($formid);
		if ($form->isEmpty()){
			page_result(1, "表单信息不存在。");
		}
		page_result(0, "", array(
			'form'			=> $form 
			));
	}
	
	
}