
const searchCtr = (req, res) => {
    let _resp_json = {
        result: "ok",
        reason: {
            content: "",
            body: "",
        },
        req_body: {},
        req_param: {}
    }

    try {
        let param = req.query;
        if (param) {
            let { page, limit, sort, order, filter } = param;

            let id, pos, name, cls, isApprove;
            let WHEREContext = "";
            if (filter) {
                const obj = JSON.parse(filter);
                id = obj.authorityType;
                pos = obj.pos;
                name = obj.name;
                cls = obj.cls;
                isApprove = obj.isApprove;
            }

            let check_filter = 0;
            let filters = [];
            let ret_id = _GTP.STR_VALIDER(id);
            if (ret_id) {
                check_filter++;
                let _filter_name = `"id" LIKE '${id}%'`;
                filters.push(_filter_name);
            }

            let ret_pos = _GTP.STR_VALIDER(pos);
            if (ret_pos) {
                check_filter++;
                let _filter_name = `"pos" LIKE '${pos}%'`;
                filters.push(_filter_name);
            }
            let ret_name = _GTP.STR_VALIDER(name);
            if (ret_name) {
                check_filter++;
                let _filter_name = `"name" LIKE '${name}%'`;
                filters.push(_filter_name);
            }
            let ret_cls = _GTP.STR_VALIDER(cls);
            if (ret_cls) {
                check_filter++;
                let _filter_name = `"cls" LIKE '${cls}%'`;
                filters.push(_filter_name);
            }
            let ret_isApprove = _GTP.BOOL_VALIDER(isApprove);
            if (ret_isApprove) {
                check_filter++;
                let _filter_name = `"isApprove"=${isApprove}`;
                filters.push(_filter_name);
            }
            

            if (check_filter == 1) {
                WHEREContext = `WHERE ${filters.join(', ')}`;
            }
            else if(check_filter >= 2) {
                WHEREContext = `WHERE `;
                for (let i = 0; i < filters.length; i++) {
                    WHEREContext += filters[i];
                    if (i != (filters.length - 1)) WHEREContext += ` AND `
                }
            }

            let ret_page = _GTP.STR_VALIDER(page);
            if (!ret_page) page = 1;
            let ret_limit = _GTP.STR_VALIDER(limit);
            if (!ret_limit) limit = 1;
            if (typeof page === 'string') page = Number(page)
            if (typeof limit === 'string') limit = Number(limit)
            if (page < 1) page = 1;
            if (limit < 1) limit = 1;

            let ret_sort = _GTP.STR_VALIDER(sort);
            if (!ret_sort) ret_sort = "CREATE_AT";
            else ret_sort = sort;
            let ret_order = _GTP.STR_VALIDER(order);
            if (!ret_order) ret_order = "DESC";
            else ret_order = order;

            let _query_data = {
                page: page,
                limit: limit,
                sort: ret_sort,
                order: ret_order,
                where: WHEREContext
            };

            _SPM.db.pgSql.Query_AUTH_SEARCH(_query_data).then((resRow) => {
                _resp_json.result = "ok";
                _resp_json.reason = `account search`;
                _resp_json.data = resRow.result_val;
                res.status(200).json(_resp_json);
            })
            .catch((error)=>{
                _resp_json.result = "fail";
                _resp_json.reason = 'Internal Select Query Error';
                res.status(500).json(_resp_json);
            })
        }
        else {
            _resp_json.result = "fail";
            _resp_json.reason = 'Bad Request';
            res.status(400).json(_resp_json);
        }
    }
    catch(error) {
        _resp_json.result = "fail";
        _resp_json.reason = error.message;
        res.status(500).json(_resp_json);
    }
}

module.exports = searchCtr;