<?php
use yii\helpers\Url;
use home\assets\LinkAsset;
use home\widgets\Alert;
use home\modules\task\models\Task;
use yii\helpers\ArrayHelper;

/* @var $this \yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \home\modules\task\models\Task */

$this->title = '添加任务';

LinkAsset::register($this);

$assetUrl = $this->getAssetManager()->getBundle(LinkAsset::className())->baseUrl;
$this->registerJsFile($assetUrl.'/js/task.create.js', ['depends' => [LinkAsset::className()]]);

$request = Yii::$app->request;
$parentId = (int) $request->get('parent_id');

if ($parentId != 0) {
    $model->parent_id = $parentId;
}

if ($model->parent_id != 0 && $parentTask = Task::findOne($model->parent_id)) {
    $model->parent_id = $parentTask->task_no;
    $model->parent_name = $parentTask->name;
}

if ($model->isNewRecord) {
	$model->task_no = Task::getNo();
    $model->workload = 8;
    $model->leader_id = 0;
    
    
}
else {
	$model->begin_time = (!empty($model->begin_time)) ? date('Y-m-d', $model->begin_time) : '';
	$model->end_time = (!empty($model->end_time)) ? date('Y-m-d', $model->end_time) : '';
	$model->leader_name = ArrayHelper::getValue(\common\api\User::getOne($model->leader_id), 'realname');
	$model->join_names = (!empty($model->join_ids)) ? implode(',', ArrayHelper::getColumn(\common\models\User::getUserByDot($model->join_ids), 'realname')) : '';
	$model->follow_names = (!empty($model->follow_ids)) ? implode(',', ArrayHelper::getColumn(\common\models\User::getUserByDot($model->follow_ids), 'realname')) : '';
	$model->link_name = (!empty($model->link_id)) ? ArrayHelper::getValue(common\models\Common::getLinkData($model->link_type, $model->link_id), 'name') : '';
}
$getFields = ['link_id', 'link_type', 'link_name', 'name', 'leader_id', 'leader_name', 'begin_time', 'end_time'];
foreach($getFields as $field) {
    $getValue = $request->get($field);
    if ($getValue !== null) {
        if (in_array($field, ['begin_time', 'end_time'])) {
            $getValue = date('Y-m-d');
        }
        $model->$field = $getValue;
    }
}
$linkable = !empty($model->parent_id) ? false : true;

?>

<script>
var ajaxGetTaskUrl = '<?= Url::to(['ajax-get-task']) ?>';
var isNewRecord = <?= ($model->isNewRecord) ? 1 : 0 ?>
</script>


<div class="modal-window">

<?= \home\widgets\Alert::widget(); ?>

<?= $this->render('_form', ['model' => $model, 'action' => $action, 'linkable' => $linkable]);  ?>

</div>
