<template>
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-title">
        日期：{{year}}年{{month}}月{{day}}日
      </view>
    </view>
    <picker-view class="picker-view" :value="value" @change="bindChange" :indicator-style="indicatorStyle"
      :indicator-class="indicatorClass" :mask-style="maskStyle" :mask-class="maskClass" :mask-top-style="maskTopStyle"
      :mask-bottom-style="maskBottomStyle">
      <picker-view-column class="picker-view-column">
        <view class="item" v-for="(item,index) in years" :key="index"><text class="text">{{item}}年</text></view>
      </picker-view-column>
      <picker-view-column class="picker-view-column">
        <view class="item" v-for="(item,index) in months" :key="index"><text class="text">{{item}}月</text>
        </view>
      </picker-view-column>
      <picker-view-column class="picker-view-column">
        <view class="item" v-for="(item,index) in days" :key="index"><text class="text">{{item}}日</text></view>
      </picker-view-column>
    </picker-view>
    <boolean-data :defaultValue="false" title="设置选择器中间选中框的样式" @change="setIndicatorStyle"></boolean-data>
    <!-- #ifdef WEB || MP-WEIXIN -->
    <boolean-data :defaultValue="false" title="设置选择器中间选中框的类名" @change="setIndicatorClass"></boolean-data>
    <boolean-data :defaultValue="false" title="设置蒙层的样式" @change="setMaskStyle"></boolean-data>
    <boolean-data :defaultValue="false" title="设置蒙层的类名" @change="setMaskClass"></boolean-data>
    <!-- #endif -->
    <!-- #ifdef APP -->
    <boolean-data :defaultValue="false" title="设置蒙层上半部分的样式" @change="setMaskTopStyle"></boolean-data>
    <boolean-data :defaultValue="false" title="设置蒙层下半部分的样式" @change="setMaskBottomStyle"></boolean-data>
    <!-- #endif -->
  </view>
</template>

<script lang="uts">
  import { state, setEventCallbackNum } from '@/store/index.uts'
  export default {
    data() {
      // 20180112 HBuilderX内测开始 :)
      const _years : number[] = []
      const _year = 2018
      const _months : number[] = []
      const _month : number = 1
      const _days : number[] = []
      const _day = 12
      for (let i = 2000; i <= _year; i++) {
        _years.push(i)
      }
      for (let i = 1; i <= 12; i++) {
        _months.push(i)
      }
      for (let i = 1; i <= 31; i++) {
        _days.push(i)
      }
      return {
        title: 'picker-view',
        years: _years as number[],
        year: _year as number,
        months: _months as number[],
        month: _month as number,
        days: _days as number[],
        day: _day as number,
        value: [_year - 2000, _month - 1, _day - 1] as number[],
        result: [] as number[],
        indicatorStyle: 'height: 50px;',
        indicatorClass: '',
        maskStyle: '',
        maskClass: '',
        maskTopStyle: '',
        maskBottomStyle: ''
      }
    },
    methods: {
      setIndicatorStyle(checked : boolean) {
        const extraStyle = 'height: 50px;border:#ff5500 solid 1px;background:rgba(182, 179, 255, 0.4);';
        // #ifdef APP-HARMONY
        this.indicatorStyle = checked ? extraStyle : "height: 50px;border:none;background:transparent;";
        // #endif
        // #ifndef APP-HARMONY
        this.indicatorStyle = checked ? extraStyle : "height: 50px;";
        // #endif
      },
      setIndicatorClass(checked : boolean) {
        this.indicatorClass = checked ? "indicator-test" : ""
      },
      setMaskStyle(checked : boolean) {
        const extraMaskStyle = "background-image: linear-gradient(to bottom, #d8e5ff, rgba(216, 229, 255, 0));"
        this.maskStyle = checked ? extraMaskStyle : ""
      },
      setMaskClass(checked : boolean) {
        this.maskClass = checked ? "mask-test" : ""
      },
      setMaskTopStyle(checked : boolean) {
        const linearToTop = "background-image: linear-gradient(to bottom, #f4ff73, rgba(216, 229, 255, 0));"
        // #ifdef APP-HARMONY
        this.maskTopStyle = checked ? linearToTop : "background-image: linear-gradient(to bottom, transparent, transparent);"
        // #endif
        // #ifndef APP-HARMONY
        this.maskTopStyle = checked ? linearToTop : ""
        // #endif
      },
      setMaskBottomStyle(checked : boolean) {
        const linearToBottom = "background-image: linear-gradient(to top, #f4ff73, rgba(216, 229, 255, 0));"
        // #ifdef APP-HARMONY
        this.maskBottomStyle = checked ? linearToBottom : "background-image: linear-gradient(to bottom, transparent, transparent);"
        // #endif
        // #ifndef APP-HARMONY
        this.maskBottomStyle = checked ? linearToBottom : ""
        // #endif
      },
      // 自动化测试
      getEventCallbackNum() : number {
        return state.eventCallbackNum
      },
      // 自动化测试
      setEventCallbackNum(num : number) {
        setEventCallbackNum(num)
      },
      bindChange(e : UniPickerViewChangeEvent) {
        // 自动化测试 触发事件元素、type 类型
        // console.log(e.target?.tagName, e.type);
        if ((e.target?.tagName ?? '').includes('PICKER-VIEW')) {
          this.setEventCallbackNum(state.eventCallbackNum + 1)
        }
        if (e.type === 'change') {
          this.setEventCallbackNum(state.eventCallbackNum + 2)
        }
        const val = e.detail.value
        this.result = val
        this.year = this.years[val[0]]
        this.month = this.months[val[1]]
        this.day = this.days[val[2]]
      },
      setValue() {
        this.value = [0, 1, 30] as number[]
      },
      setValue1() {
        this.value = [10, 10, 10] as number[]
      },
    }
  }
</script>

<style>
  .picker-view {
    width: 100%;
    height: 320px;
    margin-top: 10px;
    margin-bottom: 20px;
  }

  .item {
    height: 50px;
  }

  .text {
    line-height: 50px;
    text-align: center;
  }

  /* 自动化测试 */
  .indicator-test {
    height: 50px;
    border: #ff5500 solid 1px;
    background:rgba(182, 179, 255, 0.4);
  }

  .mask-test {
    background-image: linear-gradient(to bottom, #d8e5ff, rgba(216, 229, 255, 0));
  }
</style>
