<?php namespace Pay\Yspay;
use Dever;

class Merchant
{
    public function selectRelate_api()
    {
        $value = Dever::input('value');
        $id = Dever::input('id');
        $data = array();
        if ($id) {
            $data = Dever::db('pay/yspay_merchant')->find($id);
            if ($value != $data['project_id']) {
                $data['relate_id'] = -1;
            }
        }
        
        $config['relate_id'] = Dever::db('pay/yspay_merchant')->config['struct']['relate_id'];
        $config['relate_id']['update'] = 'select';
        $config['relate_id']['update_search'] = 'pay/yspay/merchant.search?project_id=' . $value;

        $result = Dever::load('manage/database')->update_struct(array('struct' => $config), false, $data, -1, '', true);

        return $result;
    }

    public function search_api()
    {
        $id = Dever::input('id');
        $keyword = Dever::input('keyword');
        $where = array();
        if ($keyword) {
            $where['name'] = $keyword;
        }
        $id = Dever::input('where_id');
        if ($id) {
            $where['id_no'] = $id;
        }
        $project_id = Dever::input('project_id', 1);
        $project = Dever::db('pay/yspay_project')->find($project_id);
        return Dever::search($project['table'], $where);
    }
}