<?php

use yii\helpers\Url;
use home\assets\LinkAsset;
use home\assets\MainAsset;
use common\assets\ArtDialogAsset;
use yii\helpers\ArrayHelper;
use home\widgets\Alert;
use home\models\TagLink;
use home\modules\customer\models\Contacter;

LinkAsset::register($this);
ArtDialogAsset::register($this);

$this->title = '日程';

//默认值
if ($model->isNewRecord) {
    $request = Yii::$app->request;
    $model->link_type = $request->get('link_type');
    $model->link_id = $request->get('link_id');
    $schedule_time = $request->get('date_time');
    $model->schedule_time = !empty($schedule_time) ? date('Y-m-d', strtotime($schedule_time)) : date('Y-m-d');
    $link_customer_no = $request->get('link_customer_no');
    if (!empty($link_customer_no)) {
        $model->link_customer_no = $link_customer_no;
        $contacter = Contacter::find()->where(['customer_no' => $link_customer_no])->select('contacter_id, name')->orderBy(['create_time' => SORT_DESC])->one();
        if (!empty($contacter)) {
            $model->contacter_name = $contacter->name;
            $model->contacter_id = $contacter->contacter_id;
        }
    } else {
        $model->link_customer_no = '';
    }

    $model->join_names = Yii::$app->user->identity->realname;
    $model->join_ids = Yii::$app->user->identity->user_id;
} else {
    $model->contacter_name = ArrayHelper::getValue(\home\modules\customer\models\Contacter::findOne($model->contacter_id), 'name');
    $model->schedule_time = date('Y-m-d', $model->schedule_time);
    $model->join_names = empty($model->join_ids) ? '' : implode(',', ArrayHelper::getColumn(common\models\User::getUserByDot($model->join_ids), 'realname'));
}
if (!empty($model->link_type) && !empty($model->link_id)) {
    $model->link_name = ArrayHelper::getValue(common\models\Common::getLinkData($model->link_type, $model->link_id), 'name');
}
?>

<?= $this->render(!empty($form_name) ? $form_name : '_form', ['model' => $model, 'action' => $action]); ?>