<?php
namespace app\worker\controller;

use think\facade\Session;
use app\worker\BaseController;

use app\common\model\Worder as WorderModel;
use app\common\model\WorderCate as WorderCateModel;
use app\common\model\WorderLog as WorderLogModel;
use app\common\model\Worker as WorkerModel;
use app\common\model\Param as ParamModel;
use app\common\model\User as UserModel;
use app\common\model\UserIntegral as UserIntegralModel;

class Worder extends BaseController
{
	
	// 我的报名记录
	public function myLogList()
	{
		return view('worder/myloglist',[]);
	}
	
	public function delMyLog()
	{
		$workerid = $this->access_worker['id'];
		$idarr = input('idarr/a');
		$log = WorderLogModel::where(['getworkerid'=>$workerid])->whereIn('id',$idarr)->select();
		$result = $log->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listMyLog()
	{
		$workerid = $this->access_worker['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['getworkerid', '=', $workerid];
		$list = WorderLogModel::with(['setWorker','getWorker','worder'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['worder'=>['status_text']])->select();
		$count = WorderLogModel::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 logList()
	{
		$workerid = $this->access_worker['id'];
		$worderlist = WorderModel::where(['workerid'=>$workerid])->field('id,title')->order(['id'=>'desc'])->select();
		return view('worder/loglist',[
			'worderlist'		=> $worderlist
		]);
	}
	
	public function delLog()
	{
		$workerid = $this->access_worker['id'];
		$idarr = input('idarr/a');
		$log = WorderLogModel::where(['setworkerid'=>$workerid])->whereIn('id',$idarr)->select();
		$result = $log->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listLog()
	{
		$workerid = $this->access_worker['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['setworkerid', '=', $workerid];
		$worderid = input('worderid/d',0);
		if (!empty($worderid)){
			$map[] = ['worderid', '=', $worderid];
		}
		$list = WorderLogModel::with(['setWorker','getWorker','worder'])->where($map)->order('id','DESC')->limit($limit)->page($page)->select();
		$count = WorderLogModel::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 exportLog()
	{
		$workerid = $this->access_worker['id'];
		$map = array();
		$map[] = ['setworkerid', '=', $workerid];
		$worderid = input('worderid/d',0);
		if (!empty($worderid)){
			$map[] = ['worderid', '=', $worderid];
		}
		$xlsData = WorderLogModel::with(['setWorker','getWorker','worder'])->where($map)->order('id','DESC')->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('worder.title','订单标题'),
			array('getWorker.title','接单公司'),
			array('getWorker.realname','负责人'),
			array('getWorker.mobile','负责人电话'),
			array('createtime','接单时间'),
		);
		export_excel("订单接单记录",$xlsCell,$xlsData);
	}
	
	
	
	// 订单信息
	public function worderList()
	{
		$param = ParamModel::where(1)->findOrEmpty();
		return view('worder/worderlist',[
			'param' => $param
		]); 
	}
	 
	public function worderForm()
	{
		$worker = $this->access_worker;
		$id = input('id/d, 0');
		$worder = WorderModel::findOrEmpty($id);
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		$catelist = WorderCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		return view('worder/worderform',[
			'catelist'		=> $catelist,
			'worker'		=> $worker,
			'workerlist'	=> $workerlist,
			'worder'		=> $worder
		]);
	}
	
	public function editWorder()
	{
		$workerid = $this->access_worker['id'];
		$worker = WorkerModel::findOrEmpty($workerid);
		$user = UserModel::findOrEmpty($this->access_worker['userid']);
		if ( $user->isEmpty() ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "用户信息不存在"
			)));
		}
		$param = ParamModel::where(1)->findOrEmpty();
		$id = input('id/d', 0);
		$data = [
			'cateid'      		=> input('cateid/d', 0),
			'workerid'			=> $workerid,
			'title'				=> input('title/s', ""),
			'tags'				=> input('tags/a', array()),
			'otype'				=> input('otype/s', ""),
			'wagall'			=> input('wagall/s', ""),
			'telephone'			=> input('telephone/s', ""),
			'province'			=> input('province/s', ""),
			'city'				=> input('city/s', ""),
			'district'			=> input('district/s', ""),
			'jobdetails'		=> input('jobdetails/s', ""),
			'coodetails'		=> input('coodetails/s', ""),
			'status'			=> input('status/d', 0),
			'priority'			=> input('priority/d', 0),
			'updatetime'		=> input('updatetime/s', ""),
			'createtime'		=> input('createtime/s', ""),
			'volume'			=> input('volume/d', 0)
		];
		if (empty($id)){
			if ($user->integral < $param->addworder){
				page_result(1, "每次发布订单信息需要扣除".$param->addworder."积分，你当前积分不足。");
			}
			$data['priority'] = 0;
			$data['updatetime'] = date("Y-m-d H:i:s");
			$data['createtime'] = date("Y-m-d H:i:s");
			$data['volume'] = 0;
			$worder = WorderModel::create($data);
			$intdata = array(
			    'userid'		=> $user->id,
			    'title'			=> "发布订单信息扣除",
			    'intvalue'		=> 0 - $param->addworder,
			    'intmoney'		=> 0.00,
				'onlycontent'	=> "",
			    'remark'		=> input('title/s', ""),
			    'itype'			=> 5,
			    'createtime'	=> date("Y-m-d H:i:s"),
			    'yeartime'		=> date("Y"),
			    'monthtime'		=> date("Ym")
			);
			UserIntegralModel::create($intdata);
			$integral = intval($user->integral) - intval($param->addworder);
			$user->save([
				'integral'	=> $integral
			]);
		}else{
			$worder = WorderModel::find($id);
			WorderLogModel::update([ 'setworkerid' => input('workerid/d', 0) ],['worderid'=>$worder->id]); 
			$worder->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldWorder()
	{
		$workerid = $this->access_worker['id'];
		$id = input('id/d',0); 
		$worder = WorderModel::where(['workerid'=>$workerid])->findOrEmpty($id);
		if ($worder->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$worder->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delWorder()
	{
		$workerid = $this->access_worker['id'];
		$idarr = input('idarr/a');
		$worder = WorderModel::where(['workerid'=>$workerid])->whereIn('id',$idarr)->select();
		WorderLogModel::where(['workerid'=>$workerid])->whereIn('worderid',$idarr)->delete();
		$result = $worder->delete();
		if ($result){ 
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listWorder()
	{
		$workerid = $this->access_worker['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['workerid', '=', $workerid];
		$keywords = input('keywords/s', "");
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = WorderModel::with(['worker','worderCate'])->withCount(['worderLog'])->where($map)->order(['priority'=>'desc','updatetime'=>'desc','id'=>'desc',])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = WorderModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	
}
