<?php

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

class Worker extends Auth {
    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->model = new \app\common\model\Worker();
    }
    public function index() {
        if (request()->isAjax()) {
            //分页参数
            $length = input('rows', 10, 'intval');   //每页条数
            $page = input('page', 1, 'intval');      //第几页
            $title = input('title', '', 'trim');
            $map = [];
            if ($title) {
                $map['name'] = $title;
            }
            $recommend = input('recommend', '', 'trim');
            if ($recommend != '') {
                $map['recommend'] = $recommend;
            }
            $enable = input('enable', '', 'trim');
            if ($enable != '') {
                $map['enable'] = $enable;
            }
            $gender = input('gender', '', 'trim');
            if ($gender != '') {
                $map['gender'] = $gender;
            }
            //数据查询
            $result = $this->model->lists($this->orgId, $map, $page, $length);
            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 {
            $meta_title = '新增护工';
            if ($id) {
                $info = $model->getUserInfo($id);
                $this->assign('info', $info);
                $meta_title = '编辑护工';
            }
            $deps = model('Dep')->getList();
            $this->assign('deps', $deps);
            $roles = model('Roles')->getList(1);
            $this->assign('roles', $roles);
            $this->assign('meta_title', $meta_title);
            return $this->fetch();
        }
    }
    /**
     * 修改余额
     */
    public function edit_money($id = 0) {
        $model = new \app\common\model\Worker();
        $user_id = Db::name('worker')
            ->where('id',$id)
            ->value('user_id');
        if (request()->isPost()) {
            $balanceModel = new WorkerBalance();
            $data = request()->post();
            if ($data['balance'] > 0 || $data['balance'] < 0) {
                if ($data['balance'] > 0) {
                    $type = 3;
                }
                else {
                    $type = 4;
                }
                $res = $balanceModel->saveWorkerBalance($this->orgId,
                    $data['balance'], $this->userId, $id, $type, $data['remark']);
                if ($res) {
                    $this->success('操作成功', url('index'));
                }
                else {
                    $this->error($balanceModel->getError());
                }
            }
            else {
                $this->success('操作成功', url('index'));
            }
        }
        else {
            $meta_title = '修改余额';
            if ($id) {
                $info = $model->getUserInfo($user_id);
                $this->assign('info', $info);
                $meta_title = '编辑护工';
            }
            $this->assign('meta_title', $meta_title);
            return $this->fetch();
        }
    }
    public function js_money($id) {
        $balanceModel = new WorkerBalance();
        $model = new \app\common\model\Worker();
        $user_id = Db::name('worker')
            ->where('id',$id)
            ->value('user_id');
        $info = $model->getUserInfo($user_id);
        $res = $balanceModel->saveWorkerBalance($this->orgId,
            -$info['balance'], $this->userId, $id, 2, '');
        if ($res) {
            $this->success('操作成功');
        }
        else {
            $this->error($balanceModel->getError());
        }
    }
    public function ye_log($id) {
        $list = Db::name('worker_balance')
            ->where('org_id', $this->orgId)
            ->where('worker_id', $id)
            ->order('id', 'desc')
            ->paginate(10, false, [
                'query' => input()
            ]);
        $render = $list->render();
        $data = $list->toArray();
        foreach ($data['data'] as $k => $v) {
            $data['data'][$k]['uName'] = Db::name('user')
                ->where('id', $v['user_id'])
                ->value('real_name');
        }
        $this->assign('list', $data['data']);
        $this->assign('page', $render);
        $this->assign('meta_title', '余额记录');
        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('user')->where('id', $id)->update([$fn => $fv]);
        }
        else {
            $res = Db::name('worker')->where('id', $id)->update([$fn => $fv]);
        }
        if ($res) {
            $this->success('操作成功');
        }
        else {
            $this->error('操作失败');
        }
    }

}