<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\models\ImportForm;
use app\models\DynamicModel;
use app\models\Users;
use app\models\District;
use yii\helpers\ArrayHelper;
use app\models\Userrole;

/**
 * ExportController implements the CRUD actions for Export model.
 */
class ExportController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Arvmaster models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new ImportForm();
        $fields = ['record_date','cdate', 'rdate'];
				 

        if($model->load(Yii::$app->request->post())) {
            try {
								
		        $Models = new DynamicModel();
                $Models->setTableName($model->name);
				print_r($Models->setTableName($model->name));
				Exit;
				
				$sloc=Yii::$app->user->identity->signedin_loc;
				$suser=Yii::$app->user->identity->users_name;
				
				$Usermodel = Users::find()->where(['users_name' => $suser])->one();			   
			    $UserRole= $Usermodel->user_role;	
				
				$sdistrict=Yii::$app->user->identity->district;
				
				 $Locs = ArrayHelper::map(Userrole::find()->where(["user_id" => $Usermodel->usr_id])->asArray()->all(), 'authorized_loc', 'authorized_loc');
                 $loc = "'" . implode($Locs, "','") . "'";
				
				if($UserRole == 1)
			       $allModels = $Models::find()->all();
				elseif($UserRole == 2) 
				   $allModels = $Models::find()->where("artccode IN ($loc)")->all();
			    elseif($UserRole == 3) 
				   $allModels = $Models::find()->where(["artccode"=>$sloc])->all();                				  
			  
                //$fileName = str_replace("cms_", "", $Models::getTableSchema()->name);
				
				$fileName = $Models::getTableSchema()->name;
                $columns = $Models::getTableSchema()->getColumnNames();
                foreach ($columns as $key => $value) {
                    // if($value == "record_date") {
                    //     unset($columns[$key]);
                    // }
                    if(in_array($value, $fields))
                        $columns[$key] = $value.":date";
                    if($value == "create_time" || $value == "update_time")
                        $columns[$key] = $value.":datetime";
					//if($value == "rtime")
					//	$columns[$key] = $value.":time";

                    $headers[$value] = $value;
                }

                if($model->type != "") {
                    if($model->type == "xls")
                        $format = "Excel5";
                    elseif($model->type == "xlsx")
                        $format = "Excel2007";
                    elseif($model->type == "csv")
                        $format = "CSV";
                    $fileName .= ".".$model->type;
                }
                else {
                    $fileName .= ".xls";
                    $format = "Excel5";
                }
                // print_r($columns);
                // exit;

                \moonland\phpexcel\Excel::widget([
                    'models' => $allModels,
                    'fileName' => $fileName,
                    'mode' => 'export', //default value as 'export'
                    'format' => $format,
                    'columns' => $columns, 
                    'headers' => $headers, 
                ]);
                Yii::$app->session->setFlash('success', "Data Imported Successful");
                return $this->redirect(['/'.Yii::$app->controller->id]);
            } catch(Exception $error) {
                print_r($error);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

}
