const Base = require('../base');

module.exports = class extends Base {
  // 内容管理页面
  async indexAction() {
    this.assign('currentPage', 'content');
    this.assign('pageTitle', '内容管理');
    this.assign('breadcrumb', [{ name: '内容管理' }]);
    this.assign('adminUser', this.adminUser || {});
    this.assign('canAdd', this.isSuperAdmin || (this.userPermissions || []).includes('content:add'));
    this.assign('canEdit', this.isSuperAdmin || (this.userPermissions || []).includes('content:edit'));
    this.assign('canDelete', this.isSuperAdmin || (this.userPermissions || []).includes('content:delete'));
    return this.display();
  }

  // 列表接口
  async listAction() {
    const { keyword, status, page = 1, pageSize = 10 } = this.get();
    const list = await this.model('content').getList({ keyword, status, page, pageSize });
    return this.success(list);
  }

  // 详情接口
  async detailAction() {
    const { id } = this.get();
    if (!id) return this.fail('参数错误');
    const item = await this.model('content').where({ id, is_deleted: 0 }).find();
    if (think.isEmpty(item)) return this.fail('内容不存在');
    return this.success(item);
  }

  // 新增
  async addAction() {
    const { content_key, title, remark, content, status = 1, sort = 0 } = this.post();
    if (!content_key || !title) return this.fail('内容标识和标题不能为空');

    // 检查 key 唯一
    const exist = await this.model('content')
      .where({ content_key, is_deleted: 0 })
      .find();
    if (!think.isEmpty(exist)) return this.fail('内容标识已存在');

    const id = await this.model('content').add({
      content_key, title,
      remark: remark || '',
      content: content || '',
      status: status ? 1 : 0,
      sort: sort || 0,
      create_by: this.adminUser?.id || 0
    });

    await this.log('add', '内容管理', `新增内容「${title}」(key:${content_key})`);
    return this.success({ id });
  }

  // 编辑（content_key 不可修改）
  async editAction() {
    const { id, title, remark, content, status, sort } = this.post();
    if (!id) return this.fail('参数错误');
    if (!title) return this.fail('标题不能为空');

    const item = await this.model('content').where({ id, is_deleted: 0 }).find();
    if (think.isEmpty(item)) return this.fail('内容不存在');

    await this.model('content').where({ id }).update({
      title,
      remark: remark !== undefined ? remark : item.remark,
      content: content !== undefined ? content : item.content,
      status: status !== undefined ? (status ? 1 : 0) : item.status,
      sort: sort !== undefined ? sort : item.sort,
      update_by: this.adminUser?.id || 0
    });

    await this.log('edit', '内容管理', `编辑内容「${title}」(ID:${id})`);
    return this.success();
  }

  // 删除
  async deleteAction() {
    const { id } = this.post();
    if (!id) return this.fail('参数错误');

    const item = await this.model('content').where({ id, is_deleted: 0 }).find();
    if (think.isEmpty(item)) return this.fail('内容不存在');

    await this.model('content').where({ id }).update({
      is_deleted: 1,
      update_by: this.adminUser?.id || 0
    });

    await this.log('delete', '内容管理', `删除内容「${item.title}」(ID:${id})`);
    return this.success();
  }
};
