<?php

namespace common\models;

use Yii;
use yii\db\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "fix_car_t".
 *
 * @property integer $id
 * @property integer $car_id
 * @property string $car_no
 * @property string $engine_no
 * @property string $car_frame_no
 * @property string $new_price
 * @property integer $brand_id
 * @property integer $series_id
 * @property integer $displacement_id
 * @property string $car_year
 * @property string $id_man
 * @property string $id_phone
 * @property string $link_man
 * @property string $link_phone
 * @property string $company
 * @property string $insurer1_no
 * @property string $insurer2_no
 * @property string $receive_date
 * @property string $receive_type
 * @property string $accident_type
 * @property string $responsibility
 * @property integer $replace_car_id
 * @property integer $driver_id
 * @property integer $direction_id
 * @property integer $range_id
 * @property string $address
 * @property string $report_no
 * @property string $receive_mileage
 * @property string $receive_oil
 * @property string $factory_mileage
 * @property string $factory_oil
 * @property string $finish_date
 * @property string $submit_date
 * @property string $remark
 * @property integer $status
 * @property integer $user_id
 * @property string $baoan_man
 * @property string $baoan_time
 * @property string $baoan_address
 * @property string $chuxian_time
 * @property string $shigu_info
 * @property string $lipei_company
 * @property string $zhuche_money
 * @property string $sanzhe_money
 * @property string $wusun_money
 * @property string $tuoche
 * @property string $tuoche_money
 * @property string $dingsun_hour_money
 * @property string $dingsun_huanjian_money
 * @property integer $peifu_bili
 * @property string $peifu_money
 * @property string $kehu_money
 * @property integer $return_status
 * @property string $return_remark
 * @property integer $return_time
 * @property string $created_at
 * @property string $updated_at
 */
class FixCarT extends \common\models\Base
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fix_car_t';
    }

    public function scenarios()
    {
        return [
            'business-order' => ['car_no','!car_id','engine_no','car_frame_no','new_price','brand_id','series_id','displacement_id','car_year','link_man','link_phone','id_man','id_phone','accident_type','responsibility','company','insurer1_no','insurer2_no','insurer_type','receive_date','receive_type','replace_car_id','driver_id','direction_id','range_id','address','status','!submit_date'],//业务提单
            'check-order' => ['car_no','!car_id','engine_no','car_frame_no','new_car_purchase_price','brand_id','series_id','displacement_id','car_year','link_man','link_phone','id_man','id_phone','accident_type','responsibility','company','insurer1_no','insurer2_no','insurer_type','receive_date','receive_type','replace_car_id','driver_id','direction_id','range_id','address','status'],//提单审核
            'driver-order' => ['car_no', 'engine_no', 'car_frame_no', 'new_car_purchase_price', 'brand_id', 'series_id', 'displacement_id', 'car_year', 'link_man', 'link_phone', 'id_man', 'id_phone', 'accident_type', 'responsibility', 'company', 'insurer1_no', 'insurer2_no','insurer_type', 'receive_date', 'receive_type', 'replace_car_id', 'driver_id', 'direction_id', 'range_id', 'address', 'remark', 'status', 'receive_mileage', 'receive_oil', 'finish_date', 'driving_this', 'driver_licesne', 'identity_card_paper', 'car_key', 'policy_of_insurance', 'police_to_prove', 'follow_car_items'],   //司机接车
            'front-desk-check'=>['car_no','engine_no','car_frame_no','new_car_purchase_price','brand_id','series_id','displacement_id','car_year','link_man','link_phone','id_man','id_phone','accident_type','responsibility','company','insurer1_no','insurer2_no','insurer_type','receive_date','receive_type','replace_car_id','driver_id','direction_id','range_id','address','receive_mileage','receive_oil','factory_mileage','factory_oil','!remark','driving_this','driver_licesne','identity_card_paper','car_key','policy_of_insurance','police_to_prove','follow_car_items','status'],   //前台审核
            'setting-loss' => ['car_no','engine_no','car_frame_no','new_car_purchase_price','brand_id','series_id','displacement_id','car_year','link_man','link_phone','id_man','id_phone','accident_type','responsibility','company','insurer1_no','insurer2_no','insurer_type','receive_date','receive_type','replace_car_id','driver_id','direction_id','range_id','address','receive_mileage','receive_oil','finish_date','remark','driving_this','driver_licesne','identity_card_paper','car_key','policy_of_insurance','police_to_prove','follow_car_items','status'],    //查勘定损
            'front-desk' => ['accident_type', 'responsibility', 'new_car_purchase_price', '!remark', 'receive_date', 'receive_type', 'status', 'range_id', 'direction_id', 'displacement_id', 'car_year', 'car_frame_no', 'engine_no', 'car_no', 'link_man', 'link_phone', 'service_date', 'insurance_date', 'driving_date', 'brand_id', 'series_id', 'address', 'driving_this', 'driver_licesne', 'identity_card_paper', 'car_key', 'policy_of_insurance', 'police_to_prove', 'follow_car_items','!is_in_work_shop','!status','receive_mileage','receive_oil','factory_mileage','factory_oil','!the_factory_time','finish_date','into_factory_date'],//前台派工
            'default' => ['accident_type', 'responsibility', 'new_car_purchase_price', '!remark', 'receive_date', 'receive_type', 'status', 'range_id', 'direction_id', 'displacement_id', 'car_year', 'car_frame_no', 'engine_no', 'car_no', 'link_man', 'link_phone', 'service_date', 'insurance_date', 'driving_date', 'brand_id', 'series_id', 'address', 'driving_this', 'driver_licesne', 'identity_card_paper', 'car_key', 'policy_of_insurance', 'police_to_prove', 'follow_car_items'],
            'metal-plate' => ['new_car_purchase_price', '!status', 'car_no', 'engine_no', 'car_frame_no', 'new_price', 'brand_id', 'series_id', 'displacement_id', 'car_year', 'finish_date', '!remark','status_1','status_2'],
            'status' => ['!status']
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['car_id', 'brand_id', 'series_id', 'displacement_id', 'replace_car_id', 'driver_id', 'direction_id', 'range_id', 'status', 'user_id', 'peifu_bili', 'return_status', 'return_time', 'police_to_prove', 'policy_of_insurance', 'car_key', 'driving_this', 'driver_licesne', 'identity_card_paper','is_in_work_shop','status_1','status_2'], 'integer'],
            [['receive_date', 'finish_date', 'submit_date', 'created_at', 'updated_at', 'service_date', 'insurance_date', 'driving_date','into_factory_date'], 'safe'],
            [['zhuche_money', 'sanzhe_money', 'wusun_money', 'tuoche_money', 'dingsun_hour_money', 'dingsun_huanjian_money', 'peifu_money', 'kehu_money'], 'number'],
            [['car_no', 'car_year', 'id_phone', 'link_man', 'link_phone', 'receive_type', 'report_no', 'receive_mileage', 'receive_oil', 'factory_mileage', 'factory_oil', 'baoan_time', 'chuxian_time'], 'string', 'max' => 50],
            [['engine_no', 'car_frame_no', 'new_price', 'id_man', 'company', 'insurer1_no', 'insurer2_no', 'accident_type', 'responsibility', 'baoan_man', 'baoan_address', 'lipei_company', 'tuoche', 'return_remark', 'follow_car_items'], 'string', 'max' => 100],
            [['address', 'remark', 'shigu_info'], 'string', 'max' => 200],
            [['car_no'],'unique','message'=>'该车牌号已存在'],
            [['new_car_purchase_price', 'series_id', 'car_no', 'accident_type', 'id_man', 'id_phone', 'link_man', 'link_phone', 'company', 'receive_date', 'receive_type', 'accident_type', 'responsibility', 'brand_id', 'address'], 'required', 'on' => 'default'],
            [['car_no','new_price','brand_id','link_man','link_phone','id_man','id_phone','company','insurer1_no','insurer2_no','insurer_type','receive_date','receive_type','direction_id','range_id','address','status'], 'required', 'on' => 'business-order'], //业务提单
            [['car_no', 'brand_id', 'status', 'link_man', 'link_phone', 'accident_type', 'receive_date', 'receive_type'], 'required', 'on' => 'check-order'], //提单审核
            [['car_no','brand_id','series_id','link_man','link_phone','accident_type','receive_date','receive_type','address','receive_mileage','receive_oil','driving_this','driver_licesne','identity_card_paper','car_key','policy_of_insurance','police_to_prove'], 'required', 'on' => 'driver-order'],//司机接车
            [['car_no','engine_no','car_frame_no','new_car_purchase_price','brand_id','series_id','displacement_id','car_year','link_man','link_phone','id_man','id_phone','accident_type','responsibility','company','insurer1_no','insurer2_no','insurer_type','receive_date','receive_type','replace_car_id','driver_id','direction_id','range_id','address','receive_mileage','receive_oil','factory_mileage','factory_oil','!remark','driving_this','driver_licesne','identity_card_paper','car_key','policy_of_insurance','police_to_prove','follow_car_items','status'], 'required', 'on' => 'front-desk-check'],//前台审核
            [['into_factory_date'], 'required', 'on' => 'front-desk'],//前台派工
            [['status', 'finish_date'], 'required', 'on' => 'metal-plate']

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'car_id' => 'Car ID',
            'car_no' => '车牌号',
            'engine_no' => '引擎号',
            'car_frame_no' => '车架号',
            'new_price' => '新车购置价',
            'brand_id' => '品牌',
            'series_id' => '车系',
            'displacement_id' => 'Displacement ID',
            'car_year' => 'Car Year',
            'id_man' => '被保险人',
            'id_phone' => '被保险人电话',
            'link_man' => '联系人',
            'link_phone' => '联系人电话',
            'company' => '保险公司',
            'insurer1_no' => '商业保单号',
            'insurer2_no' => '交强保单号',
            'receive_date' => '接车日期',
            'service_date' => '保养时间',
            'insurance_date' => '保险时间',
            'driving_date' => '驾驶证时间',
            'receive_type' => '交车方式',
            'accident_type' => '事故类型',
            'responsibility' => '责任',
            'replace_car_id' => '代步车',
            'driver_id' => '接车司机',
            'direction_id' => '方位',
            'range_id' => '范围',
            'address' => '接车地址',
            'report_no' => 'Report No',
            'receive_mileage' => '接车里程数',
            'receive_oil' => '接车油表数',
            'factory_mileage' => '进厂里程数',
            'factory_oil' => '进厂油表数',
            'finish_date' => '预计出厂时间',
            'submit_date' => '提单时间',
            'remark' => 'Remark',
            'status' => 'Status',
            'user_id' => 'User ID',
            'return_status' => 'Return Status',
            'return_remark' => 'Return Remark',
            'return_time' => 'Return Time',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'status_1'=>'Status 1',
            'status_2'=>'Status 2',
            'insurer_type'=>'险项'
        ];
    }

    public function getUser()
    {
        return $this->hasOne(UserT::className(), ['id' => 'user_id']);
    }

    public function getDriver()
    {
        return $this->hasOne(UserT::className(), ['id' => 'driver_id']);
    }

    public function getCar()
    {
        return $this->hasOne(CarT::className(), ['id' => 'car_id']);
    }

    public function getStatus()
    {
        $result = '';
        switch ($this->status) {
            case 0:
                $result = '业务提单';
                break;
            case 1:
                $result = '提单审核';
                break;
            case 2:
                $result = '司机接车';
                break;
            case 3:
                $result = '前台审核';
                break;
            case 4:
                $result = '查勘定损';
                break;
            case 5:
                $result = '前台派工';
                break;
            case 6://钣金
            case 7://底子工
            case 8://喷漆工
            case 9://钣金安装
            case 10://抛光
            case 11://机电
            case 12://美容装饰
            case 13://美容装饰
                $result = $this->getShowMiddleStatusName();
                break;
            case 14:
                $result = '总检';
                break;
            case 15:
                $result = '出纳';
                break;
            case 16:
                $result = '会计';
                break;
            case 17:
                $result = '出厂';
                break;
            default:
                $result = '';
        }

        return $result;
    }

    public function getModelScenario()
    {
        $scenario = null;
        switch ($this->status) {
            case 0: //业务提单
                $scenario = 'business-order';
                break;
            case 1: //提单审核
                $scenario = 'check-order';
                break;
            case 2: // 司机接车
                $scenario = 'driver-order';
                break;
            case 3: //前台审核
                $scenario = 'front-desk-check';
                break;
            case 4://查勘定损
                $scenario = 'setting-loss';
                break;
            case 5: //前台派工
                $scenario = 'default';
                break;
            case 6:
                $scenario = 'front-desk';
                break;
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
                $scenario = 'metal-plate';
                break;
            case 15:
                $scenario = 'metal-plate';
                break;
            default:
                $scenario = 'default';
        }

        return $scenario;
    }

    public function getStatusMark($status = 0)
    {
        $mark = null;
        switch ($status) {
            case 1:
                $mark = '提交到提单审核';
                break;
            case 2:
                $mark = '提交到司机接车';
                break;
            case 3:
                $mark = '提交到前台审核';
                break;
            case 4:
                $mark = '提交到查勘定损';
                break;
            case 5:
                $mark = '提交到前台派工';
                break;
            case 6:
                $mark = '提交到车间';
                break;
            case 7:
                $mark = '提交到底子工';
                break;
            case 8:
                $mark = '提交到喷漆工';
                break;
            case 9:
                $mark = '提交到钣金安装';
                break;
            case 10:
                $mark = '提交到抛光';
                break;
            case 11:
                $mark = '提交到机电';
                break;
            case 13:
                $mark = '提交到美容装饰';
                break;
            case 14:
                $mark = '提交到总检';
                break;
            case 15:
                $mark = '提交到财务(出纳)';
                break;
        }
        return $mark;
    }

    public function getDirection()
    {
        return $this->hasOne(DirectionT::className(), ['id' => 'direction_id']);
    }

    public function getRange()
    {
        return $this->hasOne(RangeT::className(), ['id' => 'range_id']);
    }

    public function getBrand()
    {
        return $this->hasOne(BrandT::className(), ['id' => 'brand_id']);
    }

    public function getSeries()
    {
        return $this->hasOne(SeriesT::className(), ['id' => 'series_id']);
    }

    public function getDisplacement()
    {
        return $this->hasOne(DisplacementT::className(), ['id' => 'displacement_id']);
    }

    public function getItems()
    {
        return $this->hasMany(FixCarItemT::className(), ['fix_id' => 'id']);
    }

    public function getFixCarImg()
    {
        return $this->hasMany(FixCarImg::className(), ['fix_id' => 'id']);
    }

    public function getFollowCar(){

        $new_array = explode(",",$this->follow_car_items);
        return $new_array;
    }
    public function getFinance()
    {
        return $this->hasOne(CarFinanceT::className(), ['fix_id' => 'id']);
    }
    public function getMiddleFixCarT(){
        return $this->hasMany(MiddleFixCarT::className(),['fix_id'=>'id']);
    }
    private function getShowMiddleStatusName(){
        $status = ['6'=>'钣金','7'=>'底子','8'=>'喷漆','9'=>'钣金安装','11'=>'机电','12'=>'美容装饰','13'=>'美容装饰'];
        $middle = MiddleFixCarT::find()->select('status')->where(['fix_id'=>$this->id])->all();
        $status_name = '';
        foreach($middle as $val){
            if( 0 === $val->status || $val->status > 13) continue;
            if(13 === $val->status){
                return $status_name = $status['12'];
            }
            $status_name .= '/'.$status[$val->status];
        }
        return $status_name;
    }

    /**
     *------------------------------------------------------------
     *
     * @param middle_fix_car_t.status 13 美容装饰
     * @return void
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub

        if(!$insert && 6 == $this->status) {

            $groups = FixCarItemT::find()->select('fix_group')->where(['fix_id' => $this->id, 'status' => 0, 'fix_status' => 0, 'fix_user' => 0, 'lock_id' => 0])->groupBy('fix_group')->asArray()->all();
            $groups = array_column($groups,'fix_group');

            if(count(MiddleFixCarT::findAll(['fix_id' => $this->id])) < 1){
                $data = [];
                //钣金路线
                if (in_array(7, $groups)) {
                    $data[0] = ['fix_id' => $this->id, 'type' => 1, 'status' => 6];
                    //底子路线
                } elseif (!in_array(7, $groups) && in_array(13, $groups)) {
                    $data[0] = ['fix_id' => $this->id, 'type' => 1, 'status' => 7];
                    //喷漆路线
                }elseif (!in_array(7, $groups) && in_array(14, $groups)) {
                    $data[0] = ['fix_id' => $this->id, 'type' => 1, 'status' => 8];
                    //没有底子、喷漆，只有美容
                }elseif(!in_array(7,$groups) && in_array(12,$groups)){
                    $data[0] = ['fix_id' => $this->id, 'type' => 1, 'status' => 13];
                }else{
                    $data[0] = ['fix_id' => $this->id, 'type' => 1, 'status' => 6];
                }

                //只有美容
                if(!in_array(11,$groups) && in_array(12,$groups)){
                    $data[1] = ['fix_id' => $this->id, 'type' => 2, 'status' => 13];
                    //机电
                }else{
                    $data[1] = ['fix_id' => $this->id, 'type' => 2, 'status' => 11];
                }
                $data[2] = ['fix_id' => $this->id, 'type' => 3, 'status' => 0];

                if(1 == $this->status_1 && 1 == $this->status_2){
                    $data[0]['status'] = 13;
                }
                Yii::$app->db->createCommand()->batchInsert(MiddleFixCarT::tableName(), ['fix_id', 'type', 'status'], $data)->execute();
            }
        }
    }
}