<?php

namespace app\admin\controller;
use think\App;
use think\Db;
use think\Exception;

class OrderConveyPay extends Auth {

    public function index() {
        if (request()->isAjax()) {
            //分页参数
            $length = input('rows', 10, 'intval');   //每页条数
            $page = input('page', 1, 'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx', 'id', 'trim');      //排序列
            $sort = input('sord', 'desc', 'trim');        //排序方式
            $order = $sortRow . ' ' . $sort;
            $map[] = ['org_id', '=', $this->orgId];
            $title = input('sn','','trim');
            if($title){
                $map[] = ['sn','like','%'.$title.'%'];
            }
            $enable = input('type','','trim');
            if($enable != ''){
                $map[] = ['type','=',$enable];
            }
            $enable = input('status','','trim');
            if($enable != ''){
                $map[] = ['status','=',$enable];
            }
            $map = empty($map) ? true : $map;
            //数据查询
            $lists = Db::name('order_convey_pay')
                ->where($map)
                ->limit($start, $length)
                ->order($order)
                ->select();
            //数据返回
            $totalCount = Db::name('order_convey_pay')->where($map)->count();
            $totalPage = ceil($totalCount / $length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        } else {
            return $this->fetch();
        }
    }


    public function pay($id=0){
        if(request()->isPost()){
            $remark = input('remark','','trim');
            $info = Db::name('order_convey_pay')->where('id',$id)->find();
            if(!$info){
                $this->error('订单不存在');
            }
            if($info['status'] == 1){
                $this->error('订单已支付');
            }
            $res = Db::name('order_convey_pay')->where('id',$id)->update([
                'type' => 1,
                'status' => 1,
                'remark' => $remark,
                'pay_time' => date('Y-m-d H:i:s')
            ]);
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error('操作失败');
            }
        }else{
            if($id){
                $info = Db::name('order_convey_pay')->where('id',$id)->find();
                $this->assign('info',$info);
            }
            return $this->fetch();
        }
    }
}