let userList = [
  {
    id: 1,
    name: "王小明1",
    date: "2016-05-04",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 2,
    name: "王小明2",
    date: "2016-05-04",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 3,
    name: "王小明3",
    date: "2016-05-05",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 4,
    name: "王小明4",
    date: "2016-05-05",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 5,
    name: "王小明5",
    date: "2016-05-08",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 6,
    name: "王小明6",
    date: "2016-05-08",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 7,
    name: "王小明7",
    date: "2016-05-09",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 8,
    name: "王小明8",
    date: "2016-05-09",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 9,
    name: "王小明9",
    date: "2016-05-04",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 10,
    name: "王小明10",
    date: "2016-05-04",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 11,
    name: "王小明11",
    date: "2016-05-04",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
  {
    id: 12,
    name: "王小明12",
    date: "2016-05-04",
    address: "上海市普陀区金沙江路 1517 弄",
    avatar: "http://192.169.0.41:8091/sanh-ui/lib/img/avatar.e147bdeb.png",
  },
];

const demo = {
  // 列表
  "post|/demo/list": (options) => {
    let param = JSON.parse(options.body);
    return {
      code: 0,
      msg: "成功",
      data: userList.slice(
        (param.page - 1) * param.limit,
        param.page * param.limit
      ),
      count: userList.length,
    };
  },

  // 新增
  "post|/demo/add": (options) => {
    let user = JSON.parse(options.body);
    user.id = Vue.prototype.$guid();
    userList.unshift(user);
    return {
      code: 0,
      msg: "保存成功",
      data: null,
    };
  },

  // 删除
  "post|/demo/delete": (options) => {
    let id = JSON.parse(options.body).id;
    userList = userList.filter((item) => {
      return item.id != id;
    });
    return {
      code: 0,
      msg: "删除成功",
      data: null,
    };
  },
  // 修改
  "post|/demo/update": (options) => {
    let user = JSON.parse(options.body);
    userList.forEach((item, index) => {
      if (item.id == user.id) {
        user.id = Vue.prototype.$guid();
        userList[index] = user;
      }
    });
    return {
      code: 0,
      msg: "修改成功",
      data: null,
    };
  },

  // 提交
  "post|/demo/submit": (options) => {
    let param = JSON.parse(options.body);
    return {
      code: 0,
      msg: "提交成功！",
      data: param,
      count: param.length,
    };
  },

  // 校验
  "post|/demo/validate": (options) => {
    let param = JSON.parse(options.body);
    let res =
      param.value3 == "12345678"
        ? {
            code: 0,
            msg: "成功",
            data: null,
          }
        : {
            code: 1,
            msg: "输入错误",
            data: null,
          };
    return res;
  }
};

export default demo;
