<?php

namespace app\admin\controller;
use app\common\model\WorkerBalance;
use think\App;
use think\Db;
use think\Exception;

class PhUser extends Auth {
    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->model = new \app\common\model\PhUser();
        $this->table = 'ph_user';
    }
    public function index() {
        if (request()->isAjax()) {
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            $name = input('name','','trim');
            if ($name != '') {
                $ids =Db::name('user')
                    ->alias('a')
                    ->join('user_org b','a.id = b.user_id')
                    ->where('a.real_name','=',$name)
                    ->where('b.org_id',$this->orgId)
                    ->column('a.id');
                $map[] = ['user','in',$ids];
            }
            $map[] = ['org_id','=',$this->orgId];
            $map[] = ['del','=',0];
            $map= empty($map) ? true: $map;
            //数据查询
            $user= Db::name('user')
                ->alias('a')
                ->join('user_org b','a.id = b.user_id')
                ->where('b.org_id','=',$this->orgId)
                ->where('a.del','=',0)
                ->where('a.enable','=',1)
                ->column('a.real_name','a.id');
            $dep= Db::name('dep')
                ->where('org_id','=',$this->orgId)
                ->where('del','=',0)
                ->where('enable','=',1)
                ->column('title','id');
            $list = Db::name($this->table)->where($map)->limit($start,$length)->order('id desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['userName'] = $user[$v['user']];
                $depList = explode(',',$v['dep']);
                foreach ($depList as $k1=>$v1){
                   $depList[$k1] = $dep[$v1];
                }
                $list[$k]['depName'] = implode(',',$depList);
            }
            $totalCount = Db::name($this->table)->where($map)->count();
            $totalPage = ceil($totalCount/$length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $list;
            return json($result);
        }
        else {
            return $this->fetch();
        }
    }
    /**
     * 新增/编辑
     */
    public function add($id = 0) {
        $model = $this->model;
        if (request()->isPost()) {
            $res = $model->updates($this->orgId);
            if ($res) {
                $this->success('操作成功', url('index'));
            }
            else {
                $this->error($model->getError());
            }
        }
        else {
            if ($id) {
                $info = Db::name($this->table)->where('id',$id)->find();
                $info['dep'] = explode(',',$info['dep']);
                $this->assign('info', $info);
            }
            $depLisr = model('Dep')->getList();
            $this->assign('depList', $depLisr);
            $userList= Db::name('user')
                ->alias('a')
                ->join('user_org b','a.id = b.user_id')
                ->where('b.org_id','=',$this->orgId)
                ->where('a.del','=',0)
                ->where('a.enable','=',1)
                ->field('a.id,a.real_name as title')
                ->select();
            $this->assign('userList', $userList);
            return $this->fetch();
        }
    }
    /**
     * 删除记录
     * @param int $id
     */
    public function del($id = 0) {
        if (!$id) {
            $this->error('参数错误');
        }
        $res = db('user')->where('id', $id)->setField('del', 1);
        if ($res) {
            $this->success('删除成功');
        }
        else {
            $this->error('删除失败');
        }
    }
    /**
     * 改变字段值
     * @param int $fv
     * @param string $fn
     * @param int $fv
     */
    public function changeField($id = 0, $fn = '', $fv = 0) {
        if (!$fn || !$id) {
            $this->error('参数错误');
        }
        if ($fn == 'enable') {
            $res = Db::name($this->table)->where('id', $id)->update([$fn => $fv]);
        }
        if ($res) {
            $this->success('操作成功');
        }
        else {
            $this->error('操作失败');
        }
    }

}