<?php

namespace app\admin\controller;



use think\facade\Db;
use think\facade\Request;

class Project extends Base
{

    //项目列表
    public function projectList()
    {
        $page = Request::param('page',1);
        $limit = Request::param('limit',config('app.limit'));
        $project_name = Request::param('keyword');
        $where = [];
        if($project_name) {
            $where[] = ['project_name','like','%'.$project_name.'%'];
        }
        $res['count'] = Db::name('project')->where($where)->count();
        $data = Db::name('project')->where($where)->page($page,$limit)->select()->toArray();
        $res['data'] = changeField($data);
        return $this->returnJson($res);
    }



    //添加|修改项目
    public function addProject()
    {
        $project_id = Request::param('project_id');
        $data['project_name'] = Request::param('project_name');
        if($project_id) {
            $is_exit = Db::name('project')->where($data)->where('project_id',"<>",$project_id)->find();
        } else{
            //查看是否存在
            $is_exit = Db::name('project')->where($data)->find();
        }
        if($is_exit) {
            return $this->returnJson([],'项目已存在',400);
        }
        $data['explain'] = Request::param('explain');
        $data['create_time'] = time();
        if($project_id) {
            $where['project_id'] = $project_id;
            $operate =  Db::name('project')->where($where)->update($data);
        } else {
            $operate =  Db::name('project')->insert($data);
        }
        if($operate !==false) {
            return $this->returnJson([],'操作成功!');
        } else {
            return $this->returnJson([],'操作失败!',400);
        }
    }


    //添加|修改子项目
    public function addProjectPart(){
        $project_part_id = Request::param('project_part_id');
        $data['project_id'] = Request::param('project_id');
        $data['project_part_name'] = Request::param('project_part_name');

        if($project_part_id) {
            //查看是否存在
            $is_exit = Db::name('project_part')->where($data)->where('project_part_id','<>',$project_part_id)->find();
        } else {
            //查看是否存在
            $is_exit = Db::name('project_part')->where($data)->find();
        }
        if($is_exit) {
            return $this->returnJson([],'项目已存在!',400);
        }
        $data['content'] = json_encode(Request::param('content'));
        $data['principal'] = json_encode(Request::param('principal'));
        $data['create_time'] = time();
        $data['status'] = Request::param('status',0);
        $data['explain'] = Request::param('explain');
        if($project_part_id) {
            $where['project_part_id'] = $project_part_id;
            $operater =  Db::name('project_part')->where($where)->update($data);
        } else {
            $operater =  Db::name('project_part')->insert($data);
        }
        if($operater) {
            return $this->returnJson([],'操作成功!');
        } else {
            return $this->returnJson([],'操作失败!',400);
        }
    }

    //查询相关的子项目的详情
    public function detailProjectPart(){
        $where['part.project_part_id'] = Request::param('project_part_id');
        $data = Db::name('project_part')
            ->alias('part')
            ->leftJoin('project p','part.project_id = p.project_id')
            ->where($where)
            ->field('part.*,p.project_name,p.explain as project_explain')
            ->find();
        $data['content'] =  json_decode($data['content'],true);
        $data['principal'] =  json_decode($data['principal'],true);
        return $this->returnJson(changeField($data));
    }


    //子项目列表


}