const {
    Markup
} = require('telegraf')
const {
    UserModel,
    BusinessModel
} = global.config
const {
    spaces,
    escape
} = global.utils

module.exports = async (ctx) => {
    let user = await UserModel.findByPk(ctx.from.id)
    if (user.business.id == 0) return await ctx.reply(`Вы не состоите в организации.`)
    let business = await BusinessModel.findOne({
      where: {
        owner: ctx.from.id.toString()
      }
    })
    if(business === null){
      business = await BusinessModel.findOne({
        where: {
        owner: user.business.id
      }
    })
        return await ctx.replyWithMarkdownV2(`🏭 Организация\n_${business.name}_\n💹 Баланс: ₽${escape(spaces(business.balance))}\n🧰 Сырье: ${business.materials}\n👥 Рабочих: ${business.users.length}\n\nВаши отработки: ${user.business.checks}`, Markup.inlineKeyboard([
            [{
                text: `📶 Отработка`,
                callback_data: "workoff"
            }, {
                text: `⬅️ Покинуть`,
                callback_data: "покинуть"
            }]
        ])
        .oneTime()
        .resize())
    }
    if (business.owner == ctx.from.id){
        let text = ``
        for (n = 0; n < business.users.length; n++) { // Процент выплаты участникам
            users = await UserModel.findByPk(business.users[n])
            text += `${users.username} - ${users.business.checks} [${users.business.percent}%]\n`
        }
        return await ctx.replyWithMarkdownV2(`🏭 Ваша организация\n_${business.name}_\n💹 Баланс: ₽${escape(spaces(business.balance))}\n🧰 Сырье: ${business.materials}\n👥 Рабочих: ${business.users.length}\n🔨 Отработок: ${business.checks}\n${escape(text)}\n\nВаши отработки: ${user.business.checks}`, Markup.inlineKeyboard([
            [{
                text: `📶 Отработка`,
                callback_data: "workoff"
            }, {
                text: `💸 Payday`,
                callback_data: "payday"
            }]
        ])
        .oneTime()
        .resize())
    }
}