const qs = require('qs')
const Mock = require('mockjs')
import mockStorge from '../../src/utils/mockStorge'
import { getBody } from '../utils'

const dic = {
  1: {
    1: [1, 2],
    2: [1],
    3: [1, 2, 3, 4, 5],
    4: [1, 2, 3, 4, 5],
    5: [1, 2, 3, 4, 5],
    6: [1],
    7: [1, 2, 4],
    8: [1],
    9: [1, 2, 3, 4, 5],
    10: [1],
    11: [1, 2],
    12: [1],
    13: [1, 2],
    14: [1, 2],
    15: [1],
    16: [1, 2],
    17: [1, 2],
    18: [1, 2],
    19: [1, 2],
    20: [1, 2],
    21: [1, 2],
    22: [1],
    23: [1, 2],
    24: [1, 2],
    25: [1, 2],
    26: [1, 2]
  },
  2: {
    1: [1],
    2: [1],
    3: [1],
    4: [1, 3, 4],
    5: [1, 2, 3, 4, 5],
    6: [1],
    7: [1, 4]
  },
  3: {
    1: [1, 2],
    2: [1],
    3: [1, 2],
    4: [1, 2],
    5: [1, 2],
    6: [1],
    7: [1, 2, 4],
    8: [1],
    9: [1, 2, 3, 4, 5],
    10: [1],
    11: [1, 2],
    12: [1, 2],
    13: [1, 2],
    14: [1, 2],
    15: [1],
    16: [1, 2],
    17: [1, 2],
    18: [1, 2],
    19: [1, 2]
  }
}

let dataKey = mockStorge('AccountRoleList', Mock.mock({
  'data|3': [
    {
      'id|+1': 1,
      'name|+1': ["管理员", "教师", "学生"],
      'power|+1':[dic[1], dic[2], dic[3]]
    }
  ],
  page: {
    total: 3,
    current: 1
  }
}))
let dataKeyPL = mockStorge('AccountPowerList', Mock.mock({
  data: dic
}))

let roleListData = global[dataKey]
let powerList = global[dataKeyPL]
// console.log('roleListData', JSON.stringify(roleListData))
// console.log('powerList', JSON.stringify(powerList))
module.exports = {

  'GET /api/role' (req, res) {
    const page = qs.parse(req.query)
    const pageSize = page.pageSize || 10
    const currentPage = page.page || 1

    let data
    let newPage

    let newData = roleListData.data.concat()

    if (page.field) {
      const d = newData.filter(function (item) {
        return item[page.field].indexOf(decodeURI(page.keyword)) > -1
      })

      data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize)

      newPage = {
        current: currentPage * 1,
        total: d.length
      }
    } else {
      data = roleListData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      roleListData.page.current = currentPage * 1
      newPage = roleListData.page
    }
    res.json({success: true, list: data, page: {...newPage, pageSize: Number(pageSize)}})
  },

  'POST /api/role' (req, res) {
    const curItem = getBody(req)

    if(curItem.id) {
      roleListData.data = roleListData.data.map(function (item) {
        if (item.id === curItem.id) {
          return {...curItem, power: JSON.parse(curItem.power)}
        }
        return item
      })

      global[dataKeyPL].data[curItem.id] = JSON.parse(curItem.power)
    } else {
      curItem.id = roleListData.data.length + 1
      roleListData.data.push({...curItem, power: JSON.parse(curItem.power)})

      roleListData.page.total = roleListData.data.length
      roleListData.page.current = 1
    }

    global[dataKey] = roleListData

    res.json({success: true, data: roleListData.data, page: roleListData.page})
  },

  'DELETE /api/role' (req, res) {
    const deleteItem = getBody(req)

    roleListData.data = roleListData.data.filter(function (item) {
      return item.id !== deleteItem.id
    })

    roleListData.page.total = roleListData.data.length

    global[dataKey] = roleListData

    res.json({success: true, data: roleListData.data, page: roleListData.page})
  },

}

@@ -136,16 +136,111 @@
 /utils'%0A
+//power = %7B 1: %22%E6%9F%A5%E7%9C%8B%E8%8F%9C%E5%8D%95%22, 2: %22%E6%9F%A5%E7%9C%8B%E9%A1%B5%E9%9D%A2%22, 3: %22%E6%96%B0%E5%A2%9E%22, 4: %22%E4%BF%AE%E6%94%B9%22, 5: %22%E5%88%A0%E9%99%A4%22, 6%EF%BC%9A%22%E6%9F%A5%E7%9C%8B%E8%AF%A6%E6%83%85%22 7: %22%E5%AE%A1%E6%A0%B8%22, 8: %22%E4%B8%8A%E4%BC%A0%22%EF%BC%8C9:%22%E7%8A%B6%E6%80%81%22 %7D
 %0Aconst d
@@ -297,24 +297,36 @@
 , 2, 3, 4, 5
+, 6, 7, 8, 9
 %5D,%0A    4: %5B1

update role mock