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 */
15
16import router from '@ohos.router';
17import BusinessController from '../../controller/BusinessController';
18import CommodityController from '../../controller/CommodityController';
19import { BusinessInfo, CommodityInfo } from '../../data/Server';
20import Logger from '../../utils/Logger';
21import { getStringData } from '../../utils/ResourceDataHandle';
22import { BusinessError } from '@ohos.base';
23
24const TAG: string = '[Buy]';
25
26@Entry
27@Component
28struct Buy {
29  private businessController: BusinessController = new BusinessController();
30  private commodityController: CommodityController = new CommodityController();
31  @State businessList: Array<BusinessInfo> = []; // 商家列表
32  @State commodityList: CommodityInfo[][] = []; // 商品列表
33  @State longitude: string = getStringData($r('app.string.buy_longitude')); // 经度
34  @State latitude: string = getStringData($r('app.string.buy_latitude')); // 纬度
35
36  aboutToAppear() {
37    Logger.info(TAG, 'Buy aboutToAppear begin');
38    // 商家列表
39    this.businessController.getBusinessList(this.longitude, this.latitude).then((res: BusinessInfo[]) => {
40      Logger.info(TAG, `aboutToAppear then res= ${JSON.stringify(res)}`);
41      this.businessList = res;
42      this.businessList.forEach((business, index) => {
43        // 商品列表
44        let businessId = business['id'];
45        Logger.info(TAG, `commodityList then business= ${JSON.stringify(businessId)},index = ${index}`);
46        this.commodityController.getCommodityList(businessId).then((res: CommodityInfo[]) => {
47          this.commodityList[index] = res;
48          Logger.info(TAG, `commodityList then commodityList = ${JSON.stringify(this.commodityList)}`);
49        }).catch((err: BusinessError) => {
50          Logger.info(TAG, `commodityList catch err= ${err}`);
51        })
52      })
53    }).catch((err: BusinessError) => {
54      Logger.info(TAG, `aboutToAppear catch err= ${JSON.stringify(err)}`);
55    })
56  }
57
58  build() {
59    Column() {
60      Column() {
61        Row() {
62          Image($r('app.media.icon'))
63            .height(24)
64            .width(24)
65            .id('buyBack')
66            .onClick(() => {
67              router.back();
68            })
69
70          Row() {
71            Image($r('app.media.icon'))
72              .height(18)
73              .width(18)
74              .margin({ left: 12 })
75            Text($r('app.string.business_noodles'))
76              .fontSize(18)
77              .fontColor($r('app.color.business_font'))
78              .fontWeight(FontWeight.Medium)
79              .margin({ left: 12 })
80            Blank()
81          }
82          .height('70%')
83          .margin({ left: 12, top: 12, bottom: 10 })
84          .layoutWeight(1)
85          .backgroundColor(Color.White)
86          .borderRadius(24)
87
88          Image($r('app.media.icon'))
89            .height(36)
90            .width(36)
91            .margin({ left: 24 })
92            .id('selfPickUp')
93            .onClick(() => {
94              router.push({ url: 'pages/buy/SelfPickUp' });
95            })
96        }
97        .justifyContent(FlexAlign.Center)
98        .margin({ top: 4 })
99        .width('90%')
100        .height('8%')
101
102        Row() {
103          Text($r('app.string.buy_all'))
104            .fontSize(18)
105          Text($r('app.string.buy_takeaway'))
106            .fontSize(18)
107          Text($r('app.string.buy_store'))
108            .fontSize(18)
109          Text($r('app.string.buy_hongtuan'))
110            .fontSize(18)
111          Text($r('app.string.buy_delivery'))
112            .fontSize(18)
113        }
114        .justifyContent(FlexAlign.SpaceBetween)
115        .width('90%')
116        .height('8%')
117      }
118      .width('100%')
119      .backgroundColor($r('app.color.index_bg'))
120
121      Row() {
122        Text($r('app.string.buy_shop'))
123          .width('100%')
124          .textAlign(TextAlign.Center)
125          .padding({ top: 6, bottom: 6 })
126      }
127      .margin({ top: 24 })
128      .borderRadius(8)
129      .width('90%')
130      .backgroundColor($r('app.color.index_bg'))
131
132      Scroll() {
133        Column() {
134          ForEach(this.businessList, (business: BusinessInfo, index) => {
135            Row() {
136              Row() {
137                Image($r('app.media.icon'))
138                  .width(100)
139                  .height(100)
140              }
141              .height('100%')
142              .alignItems(VerticalAlign.Top)
143
144              Column() {
145                Row() {
146                  Text(business.name)
147                    .textOverflow({ overflow: TextOverflow.Ellipsis })
148                    .fontSize(24)
149                    .fontWeight(5)
150                }
151                .width('100%')
152
153                Row() {
154                  Text(business.score + getStringData($r('app.string.business_score')))
155                    .fontColor($r('app.color.business_score'))
156                    .fontSize(20)
157                    .fontWeight(5)
158                  Blank()
159                  Text($r('app.string.buy_time'))
160                    .fontSize(16)
161                    .fontWeight(5)
162                    .margin({ left: 6 })
163                }
164                .width('100%')
165                .margin({ top: 4 })
166
167                Row() {
168                  Text(getStringData($r('app.string.buy_address')) + business.address)
169                    .fontSize(16)
170                    .fontWeight(5)
171                  Blank()
172                  Text(business.distance + getStringData($r('app.string.business_m')))
173                    .fontSize(16)
174                    .fontWeight(5)
175                }
176                .width('100%')
177                .margin({ top: 4 })
178
179                Row() {
180                  Row() {
181                    Text($r('app.string.business_good_shop'))
182                      .fontColor($r('app.color.business_good_shop'))
183                      .fontSize(12)
184                      .fontWeight(5)
185                  }
186                  .backgroundColor($r('app.color.business_buy_back_bg'))
187
188                  Row() {
189                    Text($r('app.string.business_buy_back'))
190                      .fontColor($r('app.color.business_good_shop'))
191                      .fontSize(12)
192                      .fontWeight(5)
193                  }
194                  .margin({ left: 6 })
195                  .backgroundColor($r('app.color.business_buy_back_bg'))
196                }
197                .width('100%')
198                .margin({ top: 8 })
199
200                Scroll() {
201                  Row() {
202                    ForEach(this.commodityList[index], (commodity: CommodityInfo) => {
203                      Row() {
204                        Column() {
205                          Image($r('app.media.icon'))
206                            .width(100)
207                            .height(100)
208                          Text(commodity.name.split('').join('\u200B'))
209                            .textAlign(TextAlign.Start)
210                            .textOverflow({ overflow: TextOverflow.Ellipsis })
211                            .maxLines(1)
212                            .margin({ top: 4 })
213                          Text(commodity.salePrice)
214                            .width(100)
215                            .textAlign(TextAlign.Start)
216                            .fontColor($r('app.color.business_score'))
217                            .fontSize(20)
218                            .fontWeight(5)
219                            .margin({ top: 4 })
220                        }
221                        .margin({ top: 4 })
222                        .width(100)
223                      }
224                      .margin({ left: 4, top: 8 })
225                      .justifyContent(FlexAlign.Start)
226                    })
227                  }
228                }
229                .scrollable(ScrollDirection.Horizontal)
230              }
231              .width('70%')
232              .height('100%')
233              .margin({ left: 12, right: 12 })
234              .justifyContent(FlexAlign.Start)
235            }
236            .height('44%')
237            .margin({ top: 24 })
238          })
239        }
240        .margin({ bottom: 120 })
241      }
242      .width('100%')
243      .height('90%')
244    }
245  }
246}