<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\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;

class Worder extends BaseController
{
	
	
	// 报名记录
	public function logList()
	{
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		$worderlist = WorderModel::field('id,title')->order(['id'=>'desc'])->select();
		return view('worder/loglist',[
			'workerlist'		=> $workerlist,
			'worderlist'		=> $worderlist
		]);
	}
	
	public function delLog()
	{
		$idarr = input('idarr/a');
		$log = WorderLogModel::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()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$setworkerid = input('setworkerid/d',0);
		if (!empty($setworkerid)){
			$map[] = ['setworkerid', '=', $setworkerid];
		}
		$getworkerid = input('getworkerid/d',0);
		if (!empty($getworkerid)){
			$map[] = ['getworkerid', '=', $getworkerid];
		}
		$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()
	{
		$map = array();
		$setworkerid = input('setworkerid/d',0);
		if (!empty($setworkerid)){
			$map[] = ['setworkerid', '=', $setworkerid];
		}
		$getworkerid = input('getworkerid/d',0);
		if (!empty($getworkerid)){
			$map[] = ['getworkerid', '=', $getworkerid];
		}
		$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('setWorker.title','发单公司'),
			array('getWorker.title','接单公司'),
			array('createtime','接单时间'),
		);
		export_excel("订单接单记录",$xlsCell,$xlsData);
	}
	
	
	
	// 订单信息
	public function worderList()
	{
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		return view('worder/worderlist',[
			'workerlist'		=> $workerlist
		]);
	}
	
	public function worderForm()
	{
		$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,
			'workerlist'	=> $workerlist,
			'worder'		=> $worder
		]);
	}
	
	public function editWorder()
	{
		$id = input('id/d', 0);
		$data = [
			'cateid'      		=> input('cateid/d', 0),
			'workerid'			=> input('workerid/d', 0),
			'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)){
			$worder = WorderModel::create($data);
		}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()
	{
		$id = input('id/d',0); 
		$worder = WorderModel::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()
	{
		$idarr = input('idarr/a');
		$worder = WorderModel::whereIn('id',$idarr)->select();
		WorderLogModel::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()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$keywords = input('keywords/s', "");
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$workerid = input('workerid/d', 0);
		if (!empty($workerid)){
			$map[] = ['workerid', '=', $workerid];
		}
		$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
		)));
	}
	
	
	// 分类
	public function cateList()
	{
		return view('worder/catelist');
	}
	
	public function cateForm()
	{
		$id = input('id/d, 0');
		$cate = WorderCateModel::findOrEmpty($id);
		return view('worder/cateform',[
			'cate'	=> $cate
		]);
	}
	
	public function listCate()
	{
		$limit = input('limit');
		$page = input('page');
		$list = WorderCateModel::order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = WorderCateModel::count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}	
	
	public function fieldCate()
	{
		$id = input('id/d');
		$cate = WorderCateModel::find($id);
		if ($cate==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "分类信息不存在"
			)));
		}else{
			$cate->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function editCate()
	{
		$id = input('id/d');
		if (empty($id)){
			$cate = WorderCateModel::create([
				'title' 	=> input('title/s'),
				'status'    => input('status/d')==1 ? 1 : 2,
				'priority'  => input('priority/d')
			]);
		}else{
			$cate = WorderCateModel::find($id);
			$cate->save([
				'title' 	=> input('title/s'),
				'status'    => input('status/d')==1 ? 1 : 2,
				'priority'  => input('priority/d')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delCate()
	{
		$id = input('id/d');
		WorderCateModel::where('id',$id)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}
	
}
