Spaces:
Sleeping

File size: 9,650 Bytes
6b3ca98
 
 
0150abc
0a823c6
ef0fba9
a76f9c4
6b3ca98
33763b6
b38f140
e21c2b1
0a823c6
 
746620f
b38f140
 
 
308d526
b38f140
0a823c6
 
 
33763b6
0150abc
 
b38f140
 
 
 
0150abc
b38f140
 
ef0fba9
0150abc
ef0fba9
33763b6
 
 
 
bf639b8
33763b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b38f140
 
26fb1db
 
a76f9c4
26fb1db
a76f9c4
 
0a823c6
a76f9c4
33763b6
 
 
26fb1db
a76f9c4
 
 
 
 
 
 
bf639b8
a76f9c4
bf639b8
a76f9c4
bf639b8
a76f9c4
 
 
 
8cbbc10
bf639b8
a76f9c4
 
 
 
bf639b8
a76f9c4
 
 
 
 
 
 
 
 
 
bf639b8
a76f9c4
 
 
 
26fb1db
d007137
33763b6
 
 
 
26fb1db
 
0d7dc66
bf639b8
33763b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26fb1db
 
9257bfb
26fb1db
a76f9c4
33763b6
9257bfb
c4feec3
33763b6
a76f9c4
26fb1db
bf639b8
a76f9c4
33763b6
a76f9c4
 
 
 
bf639b8
a76f9c4
 
33763b6
bf639b8
33763b6
 
 
bf639b8
a76f9c4
 
 
 
 
 
 
 
c4feec3
26fb1db
 
a76f9c4
 
 
 
33763b6
 
 
 
a76f9c4
 
8cbbc10
a76f9c4
 
 
 
33763b6
 
a76f9c4
 
8cbbc10
33763b6
a76f9c4
 
8cbbc10
 
 
 
 
a76f9c4
 
 
8cbbc10
33763b6
 
a76f9c4
 
 
 
 
 
 
 
33763b6
d007137
ef0fba9
33763b6
b38f140
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import streamlit as st
import firebase_admin
from firebase_admin import credentials, firestore
from datetime import datetime
import os
import json
import random

# 1. إعداد الواجهة
st.set_page_config(page_title="تطبيق فرصة عمل", layout="centered")

st.markdown("""
    <style>
    [data-testid="stSidebar"] { display: none !important; }
    [data-testid="stHeader"], [data-testid="stToolbar"] { display: none !important; }
    .stApp { direction: rtl; text-align: right; }
    #MainMenu, footer { visibility: hidden; }
    input, select, textarea { text-align: right !important; direction: rtl !important; }
    .stButton>button { width: 100%; border-radius: 10px; height: 3em; background-color: #2E86C1; color: white; }
    </style>
    """, unsafe_allow_html=True)

# 2. تهيئة Firebase
if not firebase_admin._apps:
    try:
        firebase_json = os.environ.get("FIREBASE")
        if firebase_json:
            cred_dict = json.loads(firebase_json)
            cred = credentials.Certificate(cred_dict)
            firebase_admin.initialize_app(cred)
    except Exception as e:
        st.stop()

db = firestore.client()

# session states
for key, default in {
    "account_created": False,
    "otp_sent": False,
    "otp_code": None,
    "contact": "",
    "submitted": False,
    "user_profile": {},
    "category_choice": "خريج"
}.items():
    if key not in st.session_state:
        st.session_state[key] = default


# ==========================================
# دالة جلب ومطابقة الوظائف من Firestore
# ==========================================
def find_matching_jobs(profile):
    try:
        all_ads = db.collection("vacancies").stream()
    except Exception:
        return []

    matches = []
    user_category = profile.get("job_status", "")
    user_spec     = profile.get("major") or profile.get("work_type", "")
    user_gender   = profile.get("gender", "")

    for ad in all_ads:
        data = ad.to_dict()

        # 1. مطابقة الفئة (إلزامية)
        if data.get("category", "") != user_category:
            continue

        # 2. مطابقة التخصص/المهنة (أو "أخرى" تقبل الجميع)
        ad_spec = data.get("spec", "")
        if ad_spec not in (user_spec, "أخرى"):
            continue

        # 3. مطابقة الجنس ("كلاهما" يقبل الجميع)
        ad_gender = data.get("gender", "كلاهما")
        if ad_gender != "كلاهما" and ad_gender != user_gender:
            continue

        matches.append(data)

    return matches


def main():
    if not st.session_state.account_created:
        show_signup_page()
    elif not st.session_state.submitted:
        show_questions_page()
    else:
        show_jobs_page()


# ==========================================
# الصفحة الأولى: ابحث عن وظيفتك + OTP
# ==========================================
def show_signup_page():
    st.markdown("<h1 style='text-align: center;'>ابحث عن وظيفتك</h1>", unsafe_allow_html=True)

    contact = st.text_input("البريد الإلكتروني / رقم الجوال", value=st.session_state.contact)

    if not st.session_state.otp_sent:
        if st.button("إرسال كود التحقق"):
            if contact:
                st.session_state.contact = contact
                code = str(random.randint(1000, 9999))
                st.session_state.otp_code = code
                st.session_state.otp_sent = True
                st.info(f" تم إرسال كود التحقق (للاختبار: **{code}**)")
                st.rerun()
            else:
                st.warning("رجاءً أدخل البريد الإلكتروني أو رقم الجوال")
    else:
        st.info(f" تم إرسال كود التحقق إلى: {st.session_state.contact}")
        st.caption(f"(للاختبار، الكود هو: {st.session_state.otp_code})")

        otp_input = st.text_input("أدخل كود التحقق")

        if st.button("تأكيد الكود والمتابعة"):
            if otp_input == st.session_state.otp_code:
                try:
                    db.collection("users").add({
                        "contact": st.session_state.contact,
                        "date": datetime.now()
                    })
                except Exception:
                    pass
                st.session_state.account_created = True
                st.rerun()
            else:
                st.error("الكود غير صحيح، يرجى المحاولة مرة أخرى")

        if st.button("تغيير رقم الجوال / البريد"):
            st.session_state.otp_sent = False
            st.session_state.otp_code = None
            st.rerun()


# ==========================================
# الصفحة الثانية: الأسئلة (إصلاح مشكلة الفئة)
# ==========================================
def show_questions_page():
    st.markdown("<h2 style='text-align: center;'>إكمال معلومات التقديم</h2>", unsafe_allow_html=True)

    #  الحل: radio وselectbox خارج الفورم حتى يتغير فوراً عند الاختيار
    st.session_state.category_choice = st.radio(
        "الحالة",
        ["خريج", "عامل"],
        horizontal=True,
        index=0 if st.session_state.category_choice == "خريج" else 1
    )

    if st.session_state.category_choice == "خريج":
        major = st.selectbox("التخصص", ["إدارة وأقتصاد", "هندسة", "تقنية معلومات", "قانون", "لغات", "أخرى"])
        work_type = ""
        edu = ""
    else:
        edu = st.selectbox("التحصيل الدراسي", ["امي", "ابتدائيه", "متوسطه", "اعداديه"])
        work_type = st.selectbox("المهنة", ["عامل", "حلاق", "كاشير", "عامل بناء", "خباز", "نجار", "أخرى"])
        major = ""

    with st.form("info_form"):
        name = st.text_input("الاسم")
        gender = st.radio("الجنس", ["ذكر", "أنثى"], horizontal=True)
        age = st.number_input("العمر", min_value=18, max_value=65)

        if st.session_state.category_choice == "خريج":
            job_title = st.text_input("المسمى الوظيفي")
        else:
            job_title = ""

        location = st.text_input("الموقع (السكن)")
        st.write(" ..سيتم البحث عن فرصة عمل تناسبك")

        if st.form_submit_button("إرسال التقديم"):
            if not name or not location:
                st.warning("رجاءً أكمل جميع الحقول المطلوبة")
            else:
                profile = {
                    "name": name,
                    "gender": gender,
                    "age": int(age),
                    "job_status": st.session_state.category_choice,
                    "location": location,
                    "major": major,
                    "work_type": work_type,
                    "edu": edu,
                    "job_title": job_title,
                }
                try:
                    db.collection("applications").add({**profile, "date": datetime.now()})
                except Exception:
                    pass
                st.session_state.user_profile = profile
                st.session_state.submitted = True
                st.rerun()

    if st.button("رجوع"):
        st.session_state.account_created = False
        st.session_state.otp_sent = False
        st.session_state.otp_code = None
        st.rerun()


# ==========================================
# الصفحة الثالثة: فرص العمل من Firestore
# ==========================================
def show_jobs_page():
    profile = st.session_state.user_profile
    st.success(" تم إرسال معلوماتك بنجاح!")
    st.markdown(f"<h2 style='text-align: center;'>فرص العمل المتاحة لك يا {profile.get('name', '')}</h2>", unsafe_allow_html=True)
    st.markdown(f"**موقعك:** {profile.get('location', '')} | **حالتك:** {profile.get('job_status', '')}")
    st.markdown("---")

    with st.spinner("جاري البحث عن فرص العمل المناسبة..."):
        jobs = find_matching_jobs(profile)

    if jobs:
        st.markdown(f"###  وجدنا {len(jobs)} فرصة عمل تناسب مؤهلاتك!")
        for job in jobs:
            st.markdown(f"""
            <div style="background-color:#EBF5FB; border-right: 5px solid #2E86C1; border-radius:10px; padding:15px; margin-bottom:15px;">
                <h4> {job.get('job', '')} — {job.get('shop', '')}</h4>
                <p> الموقع: {job.get('location', 'غير محدد')}</p>
                <p> الراتب: {job.get('salary', 'غير محدد')}</p>
                <p> للتواصل: <b>{job.get('phone', 'غير متاح')}</b></p>
                <p> {job.get('details', '')}</p>
            </div>
            """, unsafe_allow_html=True)
    else:
        st.info(" لا توجد فرص عمل مطابقة حالياً، سنتواصل معك عند توفر فرصة تناسبك.")

    st.balloons()

    if st.button("تقديم جديد"):
        st.session_state.account_created = False
        st.session_state.otp_sent = False
        st.session_state.otp_code = None
        st.session_state.contact = ""
        st.session_state.submitted = False
        st.session_state.user_profile = {}
        st.session_state.category_choice = "خريج"
        st.rerun()


if __name__ == "__main__":
    main()