<?php


namespace app\admin\controller\content;


use app\common\basics\AdminBase;
use app\common\server\JsonServer;
use app\common\model\DemandReport As thisModel;
use think\facade\Validate;

class DemandReport extends AdminBase
{
    /**
     * @NOTES: 列表
     * @author: 张无忌
     */
    public function lists()
    {
        $demandId = input('demand_id/d');
        if ($this->request->isAjax()) {
            try {
                $page = input('page/d', 1);
                $limit = input('limit/d', 10);

                $keyword = input('keyword/s');
                $status = input('status');

                $page = $page ?: 1;
                $limit = $limit ?: 10;
                $where = [];

                $order = [
                    'id' => 'desc'
                ];

                $where[] = ['demand_id', '=', $demandId];

                if (!empty($keyword)) {
                    $where[] = ['name|phone|company|price', 'like', '%'.trim($keyword).'%'];
                }

                if (isset($status) && $status != '') {
                    $where[] = ['status', '=', $status];
                }

                $count = thisModel::where($where)->count();

                $list =  thisModel::where($where)
                    ->order($order)
                    ->page($page,$limit)
                    ->select()
                    ->toArray();

                $data = [
                    'lists'          => $list,
                    'page_no'       => $page,
                    'page_size'     => $limit,
                    'count'         => $count,
                ];
                return JsonServer::success('获取成功', $data);
            } catch (\Exception $e) {
                return JsonServer::error('获取失败');
            }
        }

        return view('', ['demandId' => $demandId]);
    }

    /**
     * @NOTES: 编辑
     * @author: 张无忌
     */
    public function edit()
    {
        $id = input('id');
        $item = thisModel::where('id', $id)->find();
        if ($this->request->isAjax()) {
            $input = input('post.');

            $item->save([
                'remarks' => $input['remarks'],
                'status' => $input['status'],
            ]);

            return JsonServer::success('编辑成功');
        }

        return view('', [
            'detail'   => $item,
        ]);
    }

    /**
     * @NOTES: 删除
     * @author: 张无忌
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            thisModel::where('id', input('id/d'))->delete();
            return JsonServer::success('删除成功');
        }

        return JsonServer::error('异常');
    }
}