<?php
/**
 * Created by PhpStorm.
 * User: liuyingjie
 * Date: 2017/10/30
 * Time: 14:53
 */

namespace frontend\controllers;

use common\models\FixCarImg;
use Yii;
use common\models\FixCarImageCategory;
use yii\helpers\ArrayHelper;
use yii\web\Response;

class ImageController extends FixCarBaseController
{
    /**
     *------------------------------------------------------------
     *  webuploader图片上传弹窗
     * @param intger type,id
     * @return view
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionImageUpload(){
        $request = Yii::$app->request;
        $type = $request->get('type',0);
        $id = $request->get('id',0);

        $image_category = $this->getImageCategory($this->getType2Condition($type));
        return $this->renderPartial('image-upload',['type'=>$type,'id'=>$id,'image_category'=>$image_category]);
    }
    /**
     *------------------------------------------------------------
     * 查看图片
     * @param int type 图片分类 ; id fix_car_t 表主键
     * @return json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionLookImage(){
        $request = Yii::$app->request;
        $type = $request->get('type',0);
        $id = $request->get('id',0);
        $images = FixCarImg::find()->where('fix_id='.$id.' AND type<='.$type)->asArray()->all();
        $images = ArrayHelper::index($images,null,'type');
        return $this->render('look-image',['images'=>$images]);
    }
    /**
     *------------------------------------------------------------
     *下载图片
     * @param int id fix_car_img 表外键
     * @return json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionDownloadImg()
    {
        $request = Yii::$app->request;
        $id = $request->get('id');
        $model = FixCarImg::findOne($id);
        return Yii::$app->response->sendFile(realpath($model->name));
    }
    /**
     *------------------------------------------------------------
     *Ajax 删除图片
     * @param id fix_car_img 表id
     * @return json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionRmImg()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $id = $request->post('id', 0);

        $result['success'] = false;
        $result['msg'] = '删除失败';
        if ($request->isAjax) {
            $model = FixCarImg::findOne($id);
            if (unlink($model->name)) {
                $model->delete();

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }
    /**
     *------------------------------------------------------------
     *  更具给定条件获取图片分类
     * @param string condition
     * @return object
     * @author liuyingjie
     *------------------------------------------------------------
     */
    private function getImageCategory($condition = ''){
        $query = FixCarImageCategory::find();
        if($condition) $query = $query->andWhere($condition);
        return $query->all();
    }

    /**
     *------------------------------------------------------------
     *  获取各个环节对应的图片类型条件
     * @param intger type 对应每个节点
     * @return string condition
     * @author liuyingjie
     *------------------------------------------------------------
     */
    private function getType2Condition($type = ''){
        if('' === $type) return $type;
        switch($type){
            case 2://司机接车
            case 3://前台审核
                $condition = 'id BETWEEN 1 AND 2';
                break;
            case 4://查勘定损
                $condition = 'id = 1 OR id = 3';
                break;
            default:
                $condition = '';
                break;
        }
        return $condition;
    }
}