<?php

namespace Act\Lib;

use Dever;

class Form
{
    # 获取当前可用的formid
    public function get($uid, $type = 1, $system = 1)
    {
        $where['uid'] = $uid;
        if ($type > 0) {
        	$where['type'] = $type;
        	if ($type == 2) {
        		# 支付表单有3次机会
        		$where['num'] = 3;
        	} else {
        		# 普通表单只有一次机会
        		$where['num'] = 1;
        	}
        }
        $where['cate_id'] = $system;
        $info = Dever::db('act/form_id')->getAll($where);

        if ($info) {
            $key = array_rand($info);
            if (isset($info[$key])) {
            	$update['where_id'] = $info[$key]['id'];
            	$update['num'] = $info[$key]['num'] + 1;
            	Dever::db('act/form_id')->update($update);
            	return $info[$key]['form_id'];
            }
        } else {
            return false;
        }
    }

    # 提交formid
    public function submit($uid, $id, $type = 1, $system = 1)
    {
        $where['uid'] = $uid;
        $where['type'] = $type;
        $where['cate_id'] = $system;
        $where['form_id'] = $id;
        $info = Dever::db('act/form_id')->one($where);
        if (!$info) {
        	$where['num'] = 0;
            Dever::db('act/form_id')->insert($where);
        }

        return true;
    }
}