1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import promptAction from '@ohos.promptAction';
16import router from '@ohos.router';
17import UploadController from '../../controller/UploadController';
18import User from '../data/User';
19import Logger from '../../utils/Logger';
20import { BusinessError } from '@ohos.base';
21
22const TAG: string = '[PublishPage]';
23
24class Item{
25  res?: Resource
26  name?: Resource
27
28  // constructor(res: Resource, name: Resource) {
29  //   this.res = res;
30  //   this.name = name
31  // }
32}
33
34@Entry
35@Component
36struct PublishPage {
37  private itemList: Array<Item> = [{ res: $r('app.media.app_icon'), name: $r('app.string.Where_are_you') },
38    { res: $r('app.media.app_icon'), name: $r('app.string.Add_applet') },
39    { res: $r('app.media.app_icon'), name: $r('app.string.Publicly_visible') },
40    { res: $r('app.media.app_icon'), name: $r('app.string.Advanced_setup') }];
41  private uploadController: UploadController = new UploadController();
42  private uploadFile: string = (router.getParams() as Record<string, Object>).uploadFile as string; // 需要上传的文件名
43  private currentUser: User | null = null; // 当前用户信息
44  @State uploadState: boolean = false;
45
46  pageTransition() {
47    // 登录页面从底部滑入滑出
48    PageTransitionEnter({ type: RouteType.Push, duration: 10 })
49      .slide(SlideEffect.Right);
50    PageTransitionExit({ type: RouteType.Pop, duration: 10 })
51      .slide(SlideEffect.Right);
52  }
53
54  aboutToAppear() {
55    if (AppStorage.get("currentUser")) {
56      this.currentUser = AppStorage.get("currentUser")!;
57    }
58  }
59
60  build() {
61    Column() {
62      Row({ space: 12 }) {
63        Image($r('app.media.app_icon'))
64          .width(24)
65          .height(24)
66          .objectFit(ImageFit.Fill)
67        Text($r('app.string.Return_edit'))
68          .textAlign(TextAlign.Center)
69          .fontColor($r('app.color.COLOR_CCFFFFFF'))
70          .fontSize(16)
71          .fontFamily($r('app.string.Font_family_regular'))
72          .borderRadius(14)
73      }
74      .width('100%')
75      .height('8%')
76      .padding({ left: 12 })
77      .onClick(e => {
78        router.back();
79      })
80
81      Row() {
82        Column() {
83          TextArea({ placeholder: $r('app.string.Add_work_description') })
84            .id('textArea')
85            .width('100%')
86            .height('50%')
87            .fontColor($r('app.color.COLOR_FFFFFF'))
88            .placeholderColor($r('app.color.COLOR_CCFFFFFF'))
89            .fontSize(18)
90            .fontFamily($r('app.string.Font_family_regular'))
91          Blank()
92          Row({ space: 6 }) {
93            Text($r('app.string.Topic'))
94              .width(88)
95              .height(42)
96              .fontColor($r('app.color.COLOR_FFFFFF'))
97              .backgroundColor($r('app.color.COLOR_669F9B9B'))
98              .textAlign(TextAlign.Center)
99              .fontFamily($r('app.string.Font_family_regular'))
100              .borderRadius(12)
101            Text($r('app.string.Well_Number_Friend'))
102              .width(88)
103              .height(42)
104              .fontColor($r('app.color.COLOR_FFFFFF'))
105              .textAlign(TextAlign.Center)
106              .backgroundColor($r('app.color.COLOR_669F9B9B'))
107              .fontFamily($r('app.string.Font_family_regular'))
108              .borderRadius(12)
109          }
110          .width('100%')
111          .height(48)
112        }
113        .width('70%')
114        .height('100%')
115        .padding({ left: 12 })
116
117        Blank()
118        Stack() {
119          Image($r('app.media.app_icon'))
120            .width('80%')
121            .height('90%')
122            .objectFit(ImageFit.Fill)
123            .borderRadius(12)
124          Text($r('app.string.Select_cover'))
125            .width('80%')
126            .height(32)
127            .fontColor($r('app.color.COLOR_FFFFFF'))
128            .textAlign(TextAlign.Center)
129            .backgroundColor($r('app.color.COLOR_669F9B9B'))
130            .borderRadius(12)
131            .fontFamily($r('app.string.Font_family_regular'))
132        }
133        .width('30%')
134        .height('100%')
135        .borderRadius(12)
136        .alignContent(Alignment.Bottom)
137      }
138      .width('100%')
139      .height('22%')
140
141      Divider()
142        .vertical(false)
143        .height(1)
144        .width('92%')
145        .color($r('app.color.COLOR_5A5B63'))
146        .margin({ top: 12 })
147
148      Column({ space: 2 }) {
149        ForEach(this.itemList, (item: Item) => {
150          Row({ space: 12 }) {
151            Image(item.res)
152              .width(24)
153              .height(24)
154              .objectFit(ImageFit.Fill)
155              .borderRadius(4)
156            Text(item.name)
157              .fontColor($r('app.color.COLOR_FFFFFF'))
158              .textAlign(TextAlign.Center)
159              .fontFamily($r('app.string.Font_family_regular'))
160
161            Blank()
162
163            Image($r('app.media.app_icon'))
164              .width(18)
165              .height(18)
166              .objectFit(ImageFit.Fill)
167          }
168          .width('100%')
169          .height(64)
170          .padding({ left: 12, right: 12 })
171        })
172      }
173      .width('100%')
174      .height('40%')
175
176      Blank()
177      // 发布成功的提示
178      if (this.uploadState) {
179        Row({ space: 8 }) {
180          if (this.currentUser) {
181            Image($r('app.media.app_icon'))
182              .width(36)
183              .height(36)
184              .objectFit(ImageFit.Contain)
185              .borderRadius(18)
186          }
187          Text($r('app.string.Publish_Success'))
188            .fontColor($r('app.color.COLOR_000000'))
189            .textAlign(TextAlign.Center)
190            .fontFamily($r('app.string.Font_family_regular'))
191            .fontSize(18)
192        }
193        .width('96%')
194        .height(64)
195        .backgroundColor($r('app.color.COLOR_FFFFFF'))
196        .margin({ left: 8, right: 8, bottom: 12 })
197        .padding({ left: 12, right: 12 })
198        .borderRadius(12)
199      }
200      Row({ space: 8 }) {
201        Row({ space: 8 }) {
202          Image($r('app.media.app_icon'))
203            .width(20)
204            .height(20)
205            .objectFit(ImageFit.Contain)
206            .borderRadius(14)
207          Text($r('app.string.Save_Draft'))
208            .textAlign(TextAlign.Center)
209            .fontColor($r('app.color.COLOR_FFFFFF'))
210            .fontSize(16)
211            .fontFamily($r('app.string.Font_family_regular'))
212            .borderRadius(14)
213        }
214        .layoutWeight(1)
215        .height('80%')
216        .justifyContent(FlexAlign.Center)
217        .backgroundColor($r('app.color.COLOR_393939'))
218        .borderRadius(12)
219
220        Row({ space: 8 }) {
221          Image($r('app.media.app_icon'))
222            .width(20)
223            .height(20)
224            .objectFit(ImageFit.Contain)
225            .borderRadius(14)
226          Text($r('app.string.Publish'))
227            .textAlign(TextAlign.Center)
228            .fontColor($r('app.color.COLOR_FFFFFF'))
229            .fontSize(16)
230            .fontFamily($r('app.string.Font_family_regular'))
231            .borderRadius(14)
232        }
233        .id('upload')
234        .layoutWeight(1)
235        .height('80%')
236        .justifyContent(FlexAlign.Center)
237        .backgroundColor($r('app.color.COLOR_FC2B55'))
238        .borderRadius(12)
239        .onClick(e => {
240          Logger.info(TAG, `uploadFile = ${this.uploadFile}`);
241          this.uploadController.uploadFile(this.uploadFile).then(res => {
242            Logger.info(TAG, `uploadFile success= ${JSON.stringify(res)}`);
243            this.uploadState = true;
244            setTimeout(() => {
245              this.uploadState = false;
246              router.pushUrl({ url: 'pages/Index' })
247            }, 500)
248          }).catch((err: BusinessError) => {
249            Logger.info(TAG, `uploadFile faild= ${JSON.stringify(err)}`);
250            router.pushUrl({ url: 'pages/Index' })
251          })
252        })
253      }
254      .width('100%')
255      .height('9%')
256      .padding({ left: 8, right: 8 })
257    }
258    .width('100%')
259    .height('100%')
260    .backgroundColor($r('app.color.COLOR_151724'))
261  }
262}