File size: 7,729 Bytes
89ec743
 
 
aff1ffd
89ec743
 
 
 
 
 
 
 
aff1ffd
89ec743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253ca85
89ec743
 
 
 
 
253ca85
89ec743
 
 
 
 
 
 
 
 
aff1ffd
 
 
89ec743
 
 
 
 
 
 
 
 
253ca85
89ec743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253ca85
 
 
89ec743
 
aff1ffd
89ec743
 
 
 
 
aff1ffd
 
 
 
 
 
 
 
 
 
 
89ec743
 
 
 
 
253ca85
 
 
 
 
 
 
 
89ec743
 
253ca85
89ec743
 
 
 
 
aff1ffd
 
 
 
 
89ec743
 
 
 
 
 
 
 
 
aff1ffd
 
 
 
 
89ec743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aff1ffd
89ec743
 
 
 
 
 
 
 
aff1ffd
89ec743
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const { Scenes, Markup } = require('telegraf');
const Product = require('../../models/Product');
const Category = require('../../models/Category');
const userController = require('../../controllers/userController');

// Helper to show confirmation
const askConfirmation = (ctx, field, value, product) => {
    let displayValue = value;
    if (field === 'media') displayValue = `${value.length} ta yangi fayl`;

    ctx.reply(`⚠️ <b>Tasdiqlaysizmi?</b>\n\nMaydon: ${field}\nEski: ${field === 'media' ? 'Media fayllar' : (product[field] || 'Yo\'q')}\nYangi: ${displayValue}`, {
        parse_mode: 'HTML',
        ...Markup.keyboard([['βœ… Ha', '❌ Bekor qilish']]).resize().oneTime()
    });
};

const editProductScene = new Scenes.WizardScene(
    'EDIT_PRODUCT_SCENE',
    // Step 0: Ask what to edit (Index 0)
    async (ctx) => {
        const prodId = ctx.wizard.state.prodId;
        if (!prodId) return ctx.scene.leave();

        const product = await Product.findOne({ id: prodId });
        if (!product) {
            ctx.reply("Mahsulot topilmadi.");
            return ctx.scene.leave();
        }
        ctx.wizard.state.product = product;

        ctx.reply(`✏️ <b>${product.name || 'Nomsiz'}</b> ni tahrirlash.\nMavjud: ${product.quantity} ta\nHolati: ${product.condition === 'used' ? '♻️ B/U' : 'πŸ†• Yangi'}\n\nQaysi ma'lumotni o'zgartiramiz?`, {
            parse_mode: 'HTML',
            ...Markup.keyboard([
                ['πŸ“ Nomini', 'πŸ’° Narxini'],
                ['πŸ”’ Sonini', 'πŸ“„ Tavsifni'],
                ['πŸ“‚ Kategoriyani', 'πŸ“Έ Rasmni'],
                ['πŸ†•/♻️ Holatini'], // New Button
                ['❌ Bekor qilish']
            ]).oneTime().resize()
        });
        return ctx.wizard.next();
    },
    // Step 1: Handle selection (Index 1)
    async (ctx) => {
        const text = ctx.message.text;
        if (text === '❌ Bekor qilish' || text === '/start') {
            ctx.scene.leave();
            userController.start(ctx, "Tahrirlash bekor qilindi.");
            return;
        }

        let field = '';
        if (text === 'πŸ“ Nomini') field = 'name';
        else if (text === 'πŸ’° Narxini') field = 'price';
        else if (text === 'πŸ”’ Sonini') field = 'quantity';
        else if (text === 'πŸ“„ Tavsifni') field = 'description';
        else if (text === 'πŸ“‚ Kategoriyani') field = 'category';
        else if (text === 'πŸ“Έ Rasmni') field = 'media';
        else if (text === 'πŸ†•/♻️ Holatini') field = 'condition'; // Handler
        else {
            ctx.reply("Iltimos, tugmalardan birini tanlang.");
            return;
        }

        ctx.wizard.state.field = field;

        if (field === 'category') {
            const categories = await Category.find();
            const buttons = categories.map(c => c.name);
            buttons.push('❌ Bekor qilish');
            ctx.reply("Yangi kategoriyani tanlang:", Markup.keyboard(buttons).resize());
            return ctx.wizard.selectStep(3); // Go to Category Handler
        } else if (field === 'media') {
            ctx.wizard.state.newMedia = [];
            ctx.reply("πŸ“Έ Yangi rasmlarni yuboring (Maksimal 4 ta).\nEski rasmlar o'chib ketadi.\n\nBarchasini yuborib bo'lgach, 'βœ… Tayyor' tugmasini bosing.", Markup.keyboard(['βœ… Tayyor', '❌ Bekor qilish']).resize());
            return ctx.wizard.selectStep(4); // Go to Media Handler
        } else if (field === 'condition') {
            ctx.reply("Yangi holatni tanlang:", Markup.keyboard([['πŸ†• Yangi', '♻️ B/U'], ['❌ Bekor qilish']]).resize());
            return ctx.wizard.selectStep(2); // Go to Value Input (handled as text)
        }

        ctx.reply(`Yangi ${text.toLowerCase()} kiriting:`, Markup.keyboard([['❌ Bekor qilish']]).resize());
        return ctx.wizard.next();
    },
    // Step 2: Handle Text/Number Input (Index 2)
    async (ctx) => {
        const newValue = ctx.message.text;
        if (!newValue || newValue === '❌ Bekor qilish') {
            ctx.scene.leave();
            userController.start(ctx, "Bekor qilindi.");
            return;
        }

        if (newValue.startsWith('/')) {
            ctx.scene.leave();
            userController.start(ctx);
            return;
        }

        if (ctx.wizard.state.field === 'price' || ctx.wizard.state.field === 'quantity') {
            if (isNaN(newValue)) return ctx.reply("Iltimos, raqam kiriting.");
        }

        // Map Condition Text to Value
        if (ctx.wizard.state.field === 'condition') {
            if (newValue === 'πŸ†• Yangi') ctx.wizard.state.newValue = 'new';
            else if (newValue === '♻️ B/U') ctx.wizard.state.newValue = 'used';
            else return ctx.reply("Iltimos, tugmani tanlang: Yangi yoki B/U");
        } else {
            ctx.wizard.state.newValue = newValue;
        }

        // Ask Confirmation HERE
        askConfirmation(ctx, ctx.wizard.state.field, ctx.wizard.state.newValue, ctx.wizard.state.product);
        return ctx.wizard.selectStep(5); // Go to Confirmation Handler
    },
    // Step 3: Handle Category Input (Index 3)
    async (ctx) => {
        const tex = ctx.message.text;
        if (!tex || tex === '❌ Bekor qilish' || tex.startsWith('/')) {
            ctx.scene.leave();
            userController.start(ctx, "Bekor qilindi.");
            return;
        }

        ctx.wizard.state.newValue = tex;

        askConfirmation(ctx, 'category', tex, ctx.wizard.state.product);
        return ctx.wizard.selectStep(5);
    },
    // Step 4: Handle Media Input (Index 4)
    async (ctx) => {
        const msg = ctx.message;
        if (msg.text === '❌ Bekor qilish' || (msg.text && msg.text.startsWith('/'))) {
            ctx.scene.leave();
            userController.start(ctx, "Bekor qilindi.");
            return;
        }

        if (msg.text === 'βœ… Tayyor') {
            if (ctx.wizard.state.newMedia.length === 0) return ctx.reply("Kamida bitta rasm yuboring.");
            ctx.wizard.state.newValue = ctx.wizard.state.newMedia;

            askConfirmation(ctx, 'media', ctx.wizard.state.newMedia, ctx.wizard.state.product);
            return ctx.wizard.selectStep(5);
        }

        if (msg.photo) {
            ctx.wizard.state.newMedia.push({ type: 'photo', file_id: msg.photo[msg.photo.length - 1].file_id });
            ctx.reply(`Rasm qabul qilindi (${ctx.wizard.state.newMedia.length}/4)`);
        } else if (msg.video) {
            ctx.wizard.state.newMedia.push({ type: 'video', file_id: msg.video.file_id });
            ctx.reply(`Video qabul qilindi (${ctx.wizard.state.newMedia.length}/4)`);
        }

        if (ctx.wizard.state.newMedia.length >= 4) {
            ctx.wizard.state.newValue = ctx.wizard.state.newMedia;

            askConfirmation(ctx, 'media', ctx.wizard.state.newMedia, ctx.wizard.state.product);
            return ctx.wizard.selectStep(5);
        }
    },
    // Step 5: Handle Yes/No (Hander ONLY) (Index 5)
    async (ctx) => {
        if (ctx.message.text === 'βœ… Ha') {
            try {
                const update = {};
                update[ctx.wizard.state.field] = ctx.wizard.state.newValue;
                await Product.updateOne({ id: ctx.wizard.state.product.id }, update);
                ctx.reply("βœ… Muvaffaqiyatli saqlandi!", Markup.removeKeyboard());

                // Redirect to Main Menu
                userController.start(ctx);
            } catch (e) {
                console.error(e);
                ctx.reply("Xatolik bo'ldi.");
            }
        } else {
            userController.start(ctx, "Bekor qilindi.");
        }
        return ctx.scene.leave();
    }
);

module.exports = editProductScene;