const Case = require('../module/Case');
const Router = require('koa-router');
const permission = require('../intercept/permission');
const router = new Router();

async function getCase(id) {
  let _case = new Case();
  _case.id = id;

  let [result = {}] = await _case.query()

  return result
}

/**
 * 获取案例数据
 */
router.get('/', async ctx => {
  let { page = 1, keyword = '', limit = 15, label, language } = ctx.request.query;
  let _case = new Case();
  
  _case.hide = 0;
  let result = await _case.paging(page, Number(limit), keyword, label, language)

  ctx.body = {
    msg: '成功获取数据',
    content: result
  }
});



/**
 * 获取具体案例
 */
router.get('/:id', async ctx => {
  let language = Number(ctx.query.language)
  let id = ctx.params.id;

  (!id || !Number.isFinite(Number(id))) &&
    ctx.error('该案例不存在，请刷新后重试！');

  let result = await getCase(id)

  if (language && result.language !== language) {
    let reResult = null
    let reId = language === 1 ? result.chinese_id:
      language === 2 ? result.english_id : null

    reId && (reResult = await getCase(reId))
    reResult && (result = reResult)
  }

  ctx.body = {
    msg: '成功获取!',
    content: result
  };
});

/**
 * 添加案例
 */
router.post('/', permission, async ctx => {
  let body = ctx.request.body;
  let _case = new Case();

  _case.pro_type = body.proType;
  _case.title = body.title;
  _case.content = body.content;
  _case.cover = body.cover;
  _case.logo = body.logo;
  _case.label = body.label;
  _case.date = body.date;
  _case.sort = Number(body.sort) || 0;
  _case.link = body.link;
  _case.address = body.address;
  _case.type = body.type;
  _case.user_id = ctx.session.user.id
  _case.language = body.language;
  _case.chinese_id = Number(body.chinese_id) || 0;
  _case.english_id = Number(body.english_id) || 0;

  let { insertId } = await _case.insert()

  ctx.body = { 
    msg: '成功添加!',
    insertId
  };
});



/**
 * 修改案例
 */
router.put('/', permission, async ctx => {
  let body = ctx.request.body;
  let _case = new Case();
  _case.id = body.id;

  if (!await _case.exists()) {
    ctx.error('该案例不存在!')
  }

  _case.pro_type = body.proType;
  _case.title = body.title;
  _case.content = body.content;
  _case.cover = body.cover;
  _case.label = body.label;
  _case.logo = body.logo;
  _case.date = body.date;
  _case.sort = Number(body.sort) || 0;
  _case.link = body.link;
  _case.type = body.type;
  _case.address = body.address;
  _case.language = body.language;
  _case.chinese_id = Number(body.chinese_id) || 0;
  _case.english_id = Number(body.english_id) || 0;

  await _case.update()

  ctx.body = {
    msg: '成功修改!'
  };
});

/**
 * 删除案例
 */
router.delete('/:id', permission, async ctx => {
  let id = ctx.params.id;

  (!id || !Number.isFinite(Number(id))) && 
    ctx.error('该案例不存在，请刷新后重试！');

  let _case = new Case();
  _case.id = id;
  _case.hide = 0;

  (await _case.exists()) ||
    ctx.error('该案例不存在，请刷新后重试！');

  _case.hide = 1;
  await _case.update();

  ctx.body = { msg: '成功删除！' };
});

module.exports = exports = { path: '/case', router };