• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device 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 dayjs from 'dayjs'
16import router from '@ohos.router'
17import { TitleBar } from '../Component/TitleBar'
18import { AlertShow } from '../Component/AlertShow'
19import { TextShow } from '../Component/TextShow'
20import { TextAreaShow } from '../Component/TextAreaShow'
21
22@Entry
23@Component
24struct DatePage {
25  @State value: string = ''
26  @State beforeValue: string = ''
27  @State afterValue: string = ''
28  @State afterDays: string = ''
29  @State beforeDays: string = ''
30  private pinDialogController: CustomDialogController | undefined = undefined
31  onClickButton = () => {
32    router.back()
33  }
34
35  build() {
36    Column() {
37      TitleBar({ handleButtonClick: this.onClickButton })
38      Scroll() {
39        Column() {
40          TextShow({ text: $r('app.string.second_dayjs'), fontSize: 25 })
41          TextShow({ text: $r('app.string.input_math_date'), fontSize: 20 })
42          TextAreaShow({ placeholder: '2000-01-01', value: $value, inputNumber: 0 })
43          TextShow({ text: $r('app.string.before_day'), fontSize: 20 })
44          TextAreaShow({ placeholder: '0', value: $beforeDays, inputNumber: 1 })
45          TextShow({ text: $r('app.string.after_day'), fontSize: 20 })
46          TextAreaShow({ placeholder: '0', value: $afterDays, inputNumber: 2 })
47          Button($r('app.string.sure'))
48            .fontSize(20)
49            .width(100)
50            .height(50)
51            .type(ButtonType.Normal)
52            .margin({ top: 20 })
53            .key('confirm')
54            .onClick(() => {
55              if (this.value === '') {
56                this.pinDialogController = new CustomDialogController({
57                  builder: AlertShow({ text: $r('app.string.input_math_date') }),
58                })
59                this.pinDialogController.open()
60                return
61              }
62              if (this.beforeDays === '' && this.afterDays === '') {
63                this.pinDialogController = new CustomDialogController({
64                  builder: AlertShow({ text: $r('app.string.input_before_after') }),
65                })
66                this.pinDialogController.open()
67                return
68              }
69
70              if (this.beforeDays) {
71                let day = dayjs(this.value)
72                  .subtract(Number(this.beforeDays), 'day')
73                this.beforeValue = `${day.year()}-${day.month() + 1}-${day.date()}`
74              }
75              if (this.afterDays) {
76                let day = dayjs(this.value)
77                  .add(Number(this.afterDays), 'day')
78                this.afterValue = `${day.year()}-${day.month() + 1}-${day.date()}`
79              }
80            })
81
82
83          if (this.beforeDays) {
84            Text(`${this.beforeDays} 天前日期:${this.beforeValue}`)
85              .fontSize(20)
86              .alignSelf(ItemAlign.Start)
87              .margin({ top: '3%', left: '5%' })
88          }
89
90          if (this.afterDays) {
91            Text(`${this.afterDays} 天后日期:${this.afterValue}`)
92              .fontSize(20)
93              .alignSelf(ItemAlign.Start)
94              .margin({ top: '3%', left: '5%' })
95          }
96
97          Button($r('app.string.next_page'))
98            .fontSize(20)
99            .width(150)
100            .height(50)
101            .type(ButtonType.Normal)
102            .margin({ top: 20 })
103            .key('dateNextPage')
104            .onClick(() => {
105              this.value = ''
106              router.push({
107                url: 'pages/LocalLibrary'
108              })
109            })
110
111        }.width('100%')
112      }
113      .constraintSize({ maxHeight: '85%' })
114    }
115  }
116}