<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 保修
 */
class Maintain extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function addone()
    {
        $info     = input('info','');
        $filedata = input('filedata','','htmlspecialchars_decode');
        $mobile   = input('mobile','');
        $address  = input('address','');

        //视频类型追加缩略图
        $filedata = json_decode($filedata,true);
        foreach($filedata as $key => $file){
            if($file['type'] == 'video'){
                $file_url = explode('.', $file['url']);
                unset($file_url[count($file_url) - 1]);
                $file['images_thumb'] = implode('.', $file_url) . '_0.jpg';
            }else{
                $file['images_thumb'] = $file['url'];
            }
            $filedata[$key] = $file;
        }
        $filedata = json_encode($filedata);

        //写入
        $data = [
            'orderno' => createUniqueNo('',''),
            'user_id' => $this->auth->id,
            'company_id' => $this->auth->company_id,
            'createtime' => time(),
            'updatetime' => time(),
            'info' => $info,
            'filedata' => $filedata,
            'mobile' => $mobile,
            'address' => $address,
            'status' => 0,
        ];

        $order_id = Db::name('maintain')->insertGetId($data);
        $this->success('保修成功请等待审核', $order_id);
    }

    public function lists(){
        $status = input('status',0); //默认待审核

        $map = [
            'user_id' => $this->auth->id,
            'status'  => $status,
        ];
        $list = Db::name('maintain')->field('id,orderno,createtime,info,filedata,status,eva_time')
            ->where($map)->order('id desc')
            ->autopage()->select();
        if(!empty($list)){
            $header_mobile = Db::name('user_company')->where('user_id',$this->auth->id)->value('header_mobile');//负责人的电话

            foreach($list as $key => $val){
                $list[$key]['header_mobile'] = $header_mobile;
                $list[$key]['status_text'] = $this->status_data($val['status']);
            }
        }

        $this->success(1,$list);
    }

    public function info(){
        $id = input('id',0);

        $map = [
            'user_id' => $this->auth->id,
            'id'      => $id,
        ];

        $info = Db::name('maintain')
            ->where($map)
            ->find();

        $info['status_text'] = $this->status_data($info['status']);

        $header_mobile = Db::name('user_company')->where('user_id',$this->auth->id)->value('header_mobile');//负责人的电话
        $info['header_mobile'] = $header_mobile;

        //追加维修师傅
        $worker = [
            'avatar' => 'http://weibao.com/assets/img/avatar.png',
            'truename' => '李师傅',
            'mobile' => '17666666666',
        ];
        $info['worker_info'] = $worker;

        //追加进度
        $jindu = Db::name('maintain_jindu')->field('id,title,createtime')->where('order_id',$id)->order('id desc')->select();
        $info['jindu'] = $jindu;

        $this->success(1, $info);
    }

    //取消上报
    public function cancel(){
        $id = input('id',0);

        $map = [
            'user_id' => $this->auth->id,
            'id'      => $id,
        ];

        $info = Db::name('maintain')
            ->where($map)
            ->find();
        if(empty($info)){
            $this->error('不存在的订单');
        }

        if($info['status'] != 0){
            $this->error('现在已经不能取消了');
        }

        $nowtime = time();
        $update = [
            'status' => 3,
            'canceltime' => $nowtime,
            'finishtime' => $nowtime,
            'updatetime' => $nowtime,
        ];
        $rs = Db::name('maintain')
            ->where($map)->update($update);

        $this->success('取消成功');
    }

    //报价详情
    public function baojia_info(){
        $order_id = input('order_id',0);
        //找出最新报价日志
        $baojia_log = Db::name('maintain_baojia')->where('order_id',$order_id)->where('status',30)->order('id desc')->find();

        $baojia_log['status_text'] = $this->status_data($baojia_log['status']);

        $this->success(1,$baojia_log);
    }

    //报价审核
    public function baojia_audit(){
        $id = input('order_id',0);
        $status = input('status',2);
        $reason = input('reason','','trim');

        //必填
        if($status == 2 && empty($reason)){
            $this->error('请输入拒绝原因');
        }

        //检查订单
        $map = [
            'user_id' => $this->auth->id,
            'id'      => $id,
        ];
        $info = Db::name('maintain')->where($map)->find();
        if(empty($info)){
            $this->error('不存在的订单');
        }
        if($info['status'] != 30){
            $this->success('订单错误,请刷新重试');
        }
        //找出最新报价日志
        $baojia_log = Db::name('maintain_baojia')->where('order_id',$id)->where('status',30)->order('id desc')->find();

        //更新订单
        //更新报价记录
        if($status == 2){
            $update = [
                'status' => 14,  // '报价用户审核驳回',  //等待重新报价
                'updatetime' => time(),
            ];
            $update_baojia = [
                'status' => 14,  // '报价用户审核驳回',  //等待重新报价
                'updatetime' => time(),
                'baojia_useraudit_reason' => $reason,
            ];

            $remark = '报价已拒绝,即将重新报价';
        }else{
            $update = [
                'status' => 40,
                'updatetime' => time(),
            ];
            $update_baojia = [
                'status' => 40,
                'updatetime' => time(),
            ];

            $remark = '报价已通过,即将委派师傅';
        }

        Db::startTrans();
        $rs1 = Db::name('maintain')->where('id',$id)->update($update);
        if($rs1 === false){
            Db::rollback();
            $this->error('审核失败');
        }

        $rs2 = Db::name('maintain_baojia')->where('id',$baojia_log['id'])->update($update_baojia);
        if($rs2 === false){
            Db::rollback();
            $this->error('审核失败');
        }

        Db::commit();
        $this->success($remark);
    }

    //评价
    public function evaluate(){
        $id = input('order_id',0);
        $eva_info  = input('eva_info','');
        $eva_score = input('eva_score',5);

        //检查订单
        $map = [
            'user_id' => $this->auth->id,
            'id'      => $id,
        ];
        $info = Db::name('maintain')->where($map)->find();
        if(empty($info)){
            $this->error('不存在的订单');
        }
        if($info['status'] != 60){
            $this->success('订单未完成,请刷新重试');
        }
        if($info['eva_time'] != 0){
            $this->success('订单已评价,无需重复评价');
        }

        //更新
        $update = [
            'eva_info'  => $eva_info,
            'eva_score' => $eva_score,
            'eva_time'  => time(),
        ];
        $rs1 = Db::name('maintain')->where('id',$id)->update($update);

        //维保公司的平均分修改

        $this->success('评价完成');
    }

    //状态枚举
    private function status_data($status){
        //状态:0=待审核,2=已取消,4=审核未通过,10=待报价,20=报价待审,30=报价用户待审,40=待处理,41=选师傅,60=已完成
        $data = [
            0  => '待审核',
            2  => '已取消',
            4  => '审核未通过',
            10 => '待报价',        //等待初次报价
            12 => '报价审核驳回',   //等待再次报价
            14 => '报价用户审核驳回',//等待再次报价
            20 => '报价待审',      //通过就去30,驳回就到12
            30 => '报价用户待审',   //通过就去40,驳回就到14
            40 => '待处理',        //报价都审核过了,可派师傅了
            50 => '已选师傅',
            60 => '已完成',
        ];
        return $data[$status];
    }


}