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

module.exports = class extends Base {
  async indexAction() {
    this.assign('currentPage', 'sys-sms');
    this.assign('pageTitle', '短信记录');
    this.assign('breadcrumb', [
      { name: '系统管理', url: '/admin/system/user.html' },
      { name: '短信记录' }
    ]);
    return this.display();
  }

  async listAction() {
    const keyword = this.get('keyword') || '';
    const status = this.get('status');
    const bizType = this.get('bizType') || '';
    const startDate = this.get('startDate') || '';
    const endDate = this.get('endDate') || '';
    const page = this.get('page') || 1;
    const pageSize = this.get('pageSize') || 20;

    const model = this.model('sms_log');
    const where = {};

    if (keyword) {
      where.mobile = ['like', `%${keyword}%`];
    }
    if (status !== '' && status !== undefined) {
      where.status = parseInt(status);
    }
    if (bizType) {
      where.biz_type = bizType;
    }
    if (startDate && endDate) {
      where.create_time = ['between', [startDate + ' 00:00:00', endDate + ' 23:59:59']];
    } else if (startDate) {
      where.create_time = ['>=', startDate + ' 00:00:00'];
    } else if (endDate) {
      where.create_time = ['<=', endDate + ' 23:59:59'];
    }

    const list = await model.where(where)
      .order('id DESC')
      .page(page, pageSize)
      .countSelect();

    return this.json({ code: 0, data: list });
  }
};
