<?php

namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use think\Db;

class Specialist extends Permissions
{
    private function getModel()
    {
        return new \app\common\model\Specialist();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $where = [];
            if (isset($post['ids']) and !empty($post['ids'])) {
                $where['id'] = ['in', $post['ids']];
            }
            if (!empty($post["name"])) {
                $where["name"] = ['like', '%' . $post["name"] . '%'];
            }
            if (!empty($post["title"])) {
                $where["title"] = ['like', '%' . $post["title"] . '%'];
            }
            if (!empty($post["head_pic"])) {
                $where["head_pic"] = ['like', '%' . $post["head_pic"] . '%'];
            }
            if (isset($post["sex"]) and "" != $post["sex"]) {
                $where["sex"] = $post["sex"];
            }
            if (!empty($post["desc"])) {
                $where["desc"] = ['like', '%' . $post["desc"] . '%'];
            }
            if (!empty($post["consultation_direction"])) {
                $where["consultation_direction"] = ['like', '%' . $post["consultation_direction"] . '%'];
            }
            if (isset($post["address_id"]) and "" != $post["address_id"]) {
                $where["address_id"] = $post["address_id"];
            }

            $model = $this->getModel();
            $count = $model->where($where)->count();
            $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();

            foreach ($data as $key => $value) {
                $value['head_pic_url'] = geturl($value['head_pic']);
                $value['sex_text'] = $value->sex_text;
                $value['address_name'] = $value->address ? $value->address->title : '';
                $data[$key] = $value;
            }
            return array('code' => 0, 'count' => $count, 'data' => $data);
        } else {
            $this->assign('addresslist', \app\common\model\Address::all());
            return $this->fetch();
        }
    }


    public function publish()
    {
        $id = $this->request->param('id', 0, 'intval');
        $model = $this->getModel();
        $post = $this->request->post();
        if ($this->request->isPost()) {
            //验证
            $validate = new \think\Validate([
                ['name|名字', 'require|max:50'],
                ['title|职称', 'require|max:50'],
                ['head_pic|头像', 'max:255'],
                ['sex|性别', 'number'],
                ['desc|简介', 'max:500'],
                ['consultation_direction|咨询方向', 'max:500'],
                ['address_id|地址', 'require|number'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
        } else {
            $this->assign('addresslist', \app\common\model\Address::all());
        }

        if ($id > 0) {
            //修改
            $data = $model->where('id', $id)->find();
            if (empty($data)) {
                $this->error('id不正确');
            }
            if ($this->request->isPost()) {
                if (false == $model->allowField(true)->save($post, ['id' => $id])) {
                    $this->error('修改失败');
                } else {
                    $this->success('修改成功');
                }
            } else {
                $this->assign('data', $data);
                return $this->fetch();
            }
        } else {
            //新增
            if ($this->request->isPost()) {
                if (false == $model->allowField(true)->save($post)) {
                    $this->error('添加失败');
                } else {
                    $this->success('添加成功', 'index');
                }
            } else {
                return $this->fetch();
            }
        }
    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'intval');
            if (false == $this->getModel()->where('id', $id)->delete()) {
                $this->error('删除失败');
            } else {
                $this->success('删除成功', 'index');
            }
        }
    }

    public function deletes()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ids = $post['ids'];
            if ($this->getModel()->where('id', 'in', $ids)->delete()) {
                $this->success('删除成功');
            }
        }
    }

}