import Mock from "mockjs";

let List = [];
const count = 200;

for (let i = 0; i < count; i++) {
    List.push(
        Mock.mock({
            // "id|+1": 1,
            id: Mock.Random.guid(),
            "title": "@cword(5)",
            "time": "@date('yy-MM-dd hh:mm:ss')",
            content: "@cword(50)"
        })
    )
}

function paramObj(url) {
    const search = url.split('?')[1];
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}

export default {
    getNoticList: config => {
        const { name, page = 1, limit = 10 } = paramObj(config.url);
        const mockList = List.filter(user => {
            if (name && user.name.indexOf(name) === -1 && user.addr.indexOf(name) === -1) return false;
            return true;
        })
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1));
        return {
            code: 200,
            count: mockList.length,
            list: pageList
        }
    },
    searchNoticList: config => {
        const { name } = paramObj(config.url);
        const searchList = List.filter(user => {
            return user.title === name;
        })
        return {
            code: 200,
            count: searchList.length,
            list: searchList
        }
    },
    detailNotic: config => {
        const { id } = paramObj(config.url);
        const detail = List.filter(user => {
            return user.id === id;
        })
        return {
            code: 200,
            data: detail
        }
    }
}