<?php

namespace Demand\Lib;

use Dever;

class Manage
{
    /**
     * 更新需求信息
     *
     * @return mixed
     */
    public function demandUpdate($id, $name, $data)
    {
        # 更新分类id
        $category = Dever::param('category', $data);
        if ($category) {
            if (is_array($category)) {
                $category_id = end($category);
                $top_category_id = $category[0];
                if (isset($category[1])) {
                    $second_category_id = $category[1];
                } else {
                    $second_category_id = $category[0];
                }
                
            } else {
                $category_id = $category;
                $top_category_id = $category;
                $second_category_id = $category;
            }
            

            $update['top_category_id'] = $top_category_id;
            $update['second_category_id'] = $second_category_id;
            $update['category_id'] = $category_id;
            
        }

        $pic = Dever::param('pic', $data);
        $pic_cover = Dever::param('pic_cover', $data);
        if ($pic && !$pic_cover) {
            if (is_string($pic)) {
                $pic = explode(',', $pic);
            }
            $update['pic_cover'] = $pic[0];
        }

        if (isset($update)) {
            $update['where_id'] = $id;
            Dever::db('demand/info')->update($update);
        }
    }

    /**
     * 更新需求属性信息
     *
     * @return mixed
     */
    public function attrUpdate($id, $name, $data)
    {
        print_r($data);die;
        foreach ($data as $k => $v) {

        }
    }

    /**
     * 显示需求信息
     *
     * @return mixed
     */
    public function demandInfo($id)
    {
        $info = Dever::db('demand/info')->one($id);
        if ($info) {
            $table['需求编号'] = $info['id'];
            $table['需求标题'] = $info['name'];
            $table['需求分类'] = Dever::load("category/api.string", $info['category']);

            if ($info['pic_cover']) {
                $table['封面图'] = '<img src="'.$info['pic_cover'].'" width="150" />';
            }
            
            $table['信息服务费'] = '无';
            if ($info['view_price'] > 0) {
                $table['信息服务费'] = $info['view_price'] . '元';
            }

            if ($info['poster_uid'] > 0) {
                $user = Dever::load('passport/user-one', $info['poster_uid']);
                $table['创建人'] = $user['username'] . '(UID：'.$info['poster_uid'].')';
                if ($poster_pay_type == 1) {
                    //$table['支付状态'] = '';
                }
            }

            //$table['属性'] = $this->attrInfo($info);

            if ($info['cdate']) {
                $table['发布时间'] = date("Y-m-d H:i:s", $info['cdate']);
            }
        }

        return Dever::table($table);
    }

    private function attrInfo($info)
    {
        $table = array();

        $demand = Demand::init($info['top_category_id']);

        if ($demand) {
            $info = $demand->info($info, 'id');
            if ($info && isset($info['attr'])) {
                foreach ($info['attr'] as $k => $v) {
                    $table[$v['name']] = $v['value_string'];
                }
            }
        }
        
        return Dever::table($table);
    }

    public function area($id, $name, $data)
    {
        # 不再执行syncone等后续操作
        Dever::config('base')->after = 1;

        $type = Dever::input('type');

        $area = Dever::param('area', $data);

        if ($area) {

            if (isset($area[2])) {
                $update['district_id'] = $area[2];
            }

            if (isset($area[1])) {
                $update['city_id'] = $area[1];
            }

            if (isset($area[0])) {
                $update['province_id'] = $area[0];
            }

            if (isset($update)) {
                $update['where_id'] = $id;

                Dever::db($type . '/info')->update($update);
            }
        }
    }
}