<?php namespace Source\Lib;
use Dever;
class Data
{
    public function updateStart($data)
    {
        if (isset($data['content_version']) && $data['content_version']) {
            if ($data['content_version'] <= 0) {
                $data['info_data_content_id'] = 0;
            } else {
                $data['info_data_content_id'] = $data['content_version'];
            }
            unset($data['content_version']);
        }
        return $data;
    }

    public function update($data)
    {
        $db = Dever::db('info_data', 'source');
        $relation_id = Dever::call("source/value.getList", array($data['info_id']));
        $info = '更新素材配置：' . $data['name'] . ' / 素材类型：' . $db->value('type', $data) . ' / 读取资源：' . $db->value('relation', $data) . ' / 资源附件：' . ($data['relation'] == 2 ? Dever::in_array($relation_id, $data['relation_id']) : '-');
        if (isset($data['info_data_content_id'])) {
            if ($data['info_data_content_id'] > 0) {
                $content = Dever::db('info_data_content', 'source')->find($data['info_data_content_id']);
                $info .= ' / 文案版本：' . date('YmdHis', $content['cdate']);
            } else {
                $info .= ' / 文案版本：新增文案';
            }
        }
        Dever::load('log', 'source')->add($data['id'], $info);
        return $data;
    }

    # 获取素材详情页
    public function getView($page)
    {
        # 这里获取基本信息
        $db = Dever::db('info_data', 'source');
        $relation_link = Dever::url('source/data.location', array('id' => $page->info['relation_id']), true);
        $content = array
        (
            array
            (
                'name' => '素材类型',
                # 类型，text普通文本，tag标签，link链接，image图片 progress进度条 stat统计 timeline时间线 table表格
                'type' => 'text',
                'content' => $db->value('type', $page->info),
                # 样式primary success warning danger info exception
                'style' => 'primary',
            ),
            array
            (
                'name' => '读取资源',
                'type' => 'text',
                'content' => $db->value('relation', $page->info),
                'style' => 'primary',
            ),
            array
            (
                'name' => '资源附件',
                'type' => 'text',
                'content' => $page->info['relation'] == 2 ? '<a href="'.$relation_link.'" target="_blank">' . Dever::in_array(Dever::call("source/value.getList", array($page->info['info_id'])), $page->info['relation_id']) . '</a>' : '-',
                'style' => 'primary',
            ),
        );

        $button = array();
        $button[] = array
        (
            'name' => '修改配置',
            # fastedit、fastadd、oper、api、link、route
            'type' => 'fastedit',
            'path' => 'set_zone_source/source_info_data',
            'row' => array
            (
                'id' => $page->info['id'],
            ),
            'param' => array
            (
                'info_id' => $page->info['info_id'],
            ),
        );

        $value = array();
        $media = false;
        if ($page->info['relation'] == 2) {
            $data_value_where['info_id'] = $page->info['info_id'];
            if ($page->info['relation_id'] > 0) {
                $data_value_where['id'] = $page->info['relation_id'];
            }
            $data_value = Dever::db('info_value', 'source')->find($data_value_where);
            if ($data_value) {
                $media = true;
            }
        } elseif ($page->info['relation'] == 3) {
            $value = $this->getViewValue($page->info['id'], $content, $button, $media);
        }

        if ($media) {
            if ($page->info['type'] == 3) {
                $this->getViewMedia('视频', 'video', $page->info['id'], $content, $button);
            } elseif ($page->info['type'] == 10) {
                $data = $this->getViewMedia('合集', 'collection', $page->info['id'], $content, $button);
                if ($data) {
                    $this->getViewMedia('合集视频', 'video', $page->info['id'], $content, $button);
                }
            }
        }

        $text = $this->getViewText($page->info['info_data_content_id'], $page->info['id'], $content, $button);

        $button[] = array
        (
            'name' => '分发设置',
            'type' => 'fastedit',
            'path' => 'platform/role',
            'row' => array
            (
                'id' => $page->info['id'],
            ),
        );

        $info[] = array
        (
            # 类型，desc描述 table表格，表格有head和body即可
            'type' => 'desc',
            'name' => $page->info['name'],
            # 每行展示数量
            'column' => 1,
            # 是否有边框
            'border' => true,
            # 排列方向：horizontal横向 vertical纵向
            'direction' => 'horizontal',
            # 右侧按钮
            'button' => $button,
            # 具体内容
            'content' => $content,
        );

        $tab = array
        (
            'active' => 'text',
            'content' => array(),
        );
        if ($value) {
            $tab['active'] = 'value';
            $tab['content']['value'] = $value;
        }
        $tab['content']['text'] = $text;
        //$tab['content']['log'] = $this->getViewLog($page->info['id']);
        
        return array('info' => $info, 'tab' => $tab);
    }

    public function getViewMedia($name, $table, $data_id, &$content, &$button)
    {
        $data = Dever::db('info_data_' . $table, 'source')->find(array('info_data_id' => $data_id));
        $type = 'fastadd';
        $id = 0;
        if ($data) {
            $type = 'fastedit';
            $id = $data['id'];
            if ($data['file']) {
                $content[] = array
                (
                    'name' => $name . '链接',
                    'type' => 'text',
                    'content' => '<a href="'.$data['file'].'" target="_blank">' . $data['file'] . '</a>',
                    'style' => 'primary',
                );
            }
        }
        $button[] = array
        (
            'name' => $name . '处理',
            'type' => $type,
            'path' => 'set_zone_source/source_info_data_' . $table,
            'row' => array
            (
                'id' => $id,
            ),
            'param' => array
            (
                'info_data_id' => $data_id,
            ),
        );
        return $data;
    }

    public function getViewValue($data_id, &$content, &$button, &$media)
    {
        $button[] = array
        (
            'name' => '文件上传',
            'type' => 'fastadd',
            'path' => 'set_zone_source/source_info_data_value',
            'row' => array
            (
                'id' => 0,
            ),
            'param' => array
            (
                'info_data_id' => $data_id,
                'field' => 'info_data_id,value,content,name,sort,video',
            ),
        );

        $tab = array();
        $data_value_where['info_data_id'] = $data_id;
        $data = Dever::db('info_data_value', 'source')->select($data_value_where);
        if ($data) {
            $media = true;
            $link = Dever::url('source/video.view', array('type' => 'info_data_value'), true);
            foreach ($data as &$v) {
                
            }

            $tab = array
            (
                'name' => '所有文件',
                'type' => 'table',
                'border' => true,
                //'height' => '200',
                'head' => array
                (
                    array
                    (
                        'key' => 'sort',
                        'name' => '排序',
                        'fixed' => 'fixed',
                    ),
                    array
                    (
                        'key' => 'name',
                        'name' => '名称',
                        'fixed' => 'fixed',
                    ),
                    array
                    (
                        'key' => 'content',
                        'name' => '内容',
                        'fixed' => 'fixed',
                    ),
                ),
                'button' => array
                (
                    array
                    (
                        'name' => '详情',
                        'type' => 'link',
                        'param' => $link,
                    ),
                    array
                    (
                        'name' => '编辑',
                        'type' => 'fastedit',
                        'path' => 'set_zone_source/source_info_data_value',
                        'param' => 'dever_info',
                    ),
                    array
                    (
                        'name' => '视频处理',
                        'type' => 'fastedit',
                        'path' => 'set_zone_source/source_info_data_value',
                        'param' => 'dever_video',
                    ),
                    array
                    (
                        'name' => '删除',
                        'type' => 'api',
                        'param' => Dever::url('source/data.delValue', array('info_data_id' => $data_id), true),
                    ),
                ),
                'body' => $data,
            );
        }

        
        return $tab;
    }

    # 获取文案
    private function getViewText($id, $data_id, &$content, &$button)
    {
        $db = Dever::db('info_data_content', 'source');
        $edit = 'fastadd';
        $text = '';
        if ($id) {
            $edit = 'fastedit';
            $info = $db->find($id);
            $text = '版本：' . $info['name'] ? $info['name'] : date('YmdHis', $info['cdate']);
            $text .= '<br />状态：' . $db->value('status', $info);
            if ($info['status'] == 2) {
                $text .= '<br />文案：生成中......<a href="javascript:;" onclick="location.reload()">[点此刷新]</a>';
            } else {
                $text .= '<br />文案：' . htmlspecialchars_decode($info['content']);
            }
            # 后续改成流式获取
            //$text = Dever::url('source/data.content', array('id' => $id));
        } else {
            $text = '暂无文案';
        }

        $content[] = array
        (
            'name' => '当前文案',
            'type' => 'text',
            'content' => $text,
            'style' => 'primary',
        );

        $button[] = array
        (
            'name' => '智能文案',
            'type' => $edit,
            'path' => 'set_zone_source/source_info_data_content',
            'row' => array
            (
                'id' => $id,
            ),
            'param' => array
            (
                'field' => 'name,prompt,info_data_id,ai_platform,ai_prompt_type,ai_prompt',
                'info_data_id' => $data_id,
            ),
        );
        /*
        $button[] = array
        (
            'name' => '打开智能对话',
            'type' => 'link',
            'path' => 'platform/role',
            'row' => array
            (
                'id' => $data_id,
            ),
        );
        */
        $button[] = array
        (
            'name' => '手动修改文案',
            'type' => $edit,
            'path' => 'set_zone_source/source_info_data_content',
            'row' => array
            (
                'id' => $id,
            ),
            'param' => array
            (
                'field' => 'name,content,info_data_id',
                'info_data_id' => $data_id,
            ),
        );

        $version = Dever::db('info_data_content', 'source')->select(array('info_data_id' => $data_id));
        if ($version) {
            foreach ($version as &$v) {
                $v['version'] = $v['name'] ? $v['name'] : date('YmdHis', $v['cdate']);
                if ($id == $v['id']) {
                    $v['version'] .= '[当前版本]';
                }
                $v['cdate'] = date('Y-m-d H:i:s', $v['cdate']);
                $v['status'] = $db->value('status', $v);
            }
        }

        $tab = array
        (
            'name' => '所有文案',
            'type' => 'table',
            'border' => true,
            //'height' => '200',
            'head' => array
            (
                array
                (
                    'key' => 'version',
                    'name' => '版本',
                    'fixed' => 'fixed',
                ),
                array
                (
                    'key' => 'status',
                    'name' => '状态',
                    'fixed' => 'fixed',
                ),
                array
                (
                    'key' => 'cdate',
                    'name' => '时间',
                    'fixed' => 'fixed',
                ),
            ),
            'button' => array
            (
                array
                (
                    'name' => '详情',
                    'type' => 'link',
                    'param' => 'https://www.baidu.com/?info_data_id='.$data_id,
                ),
                array
                (
                    'name' => '切换',
                    'type' => 'api',
                    'param' => Dever::url('source/data.set', array('info_data_id' => $data_id)),
                ),
            ),
            'body' => $version,
        );
        return $tab;
    }

    private function getViewLog($data_id)
    {
        return array
        (
            'name' => '操作日志',
            'type' => 'table',
            'border' => true,
            //'height' => '500',
            'head' => array
            (
                array
                (
                    'key' => 'username',
                    'name' => '操作人',
                    'fixed' => 'fixed',
                ),
                array
                (
                    'key' => 'cdate',
                    'name' => '操作时间',
                    'fixed' => 'fixed',
                ),
                array
                (
                    'key' => 'info',
                    'name' => '操作内容',
                    'fixed' => 'fixed',
                ),
            ),
            'button' => array
            (
                
            ),
            'body' => Dever::load('log', 'source')->getList($data_id),
        );
    }
}