<template>
  <div class="AttendanceFiexdTime">
    <div class="form-group">
      <div class="form-group__item" hover-class="bg-hover" v-for="(item, index) in config" :key="index">
        <div class="left">
          <switch color="#1088F9" :checked="item.status === '1'" :disabled="item.editEnable === '0'" @change="e => onChange(e, index)" />
        </div>
        <div class="right" @click="toInput(item)" v-if="['6', '7'].indexOf(item.fieldType) === -1">
          <div class="right-left">
            <h2>{{ mapFieldLable(item.type) }}</h2>
            <p>{{ item.defaultValue || '' }}</p>
          </div>
          <u-icon name="arrow-right" color="#E1E2E3" size="#E1E2E3"></u-icon>
        </div>
        <picker mode="date" style="flex: 1;" v-if="item.fieldType === '7'" :value="item.defaultValue" @change="e => onDateChange(e, index)">
          <div class="right">
            <div class="right-left">
              <h2>{{ mapFieldLable(item.type) }}</h2>
              <p>{{ item.defaultValue || '' }}</p>
            </div>
            <u-icon name="arrow-right" color="#E1E2E3" size="#E1E2E3"></u-icon>
          </div>
        </picker>
        <picker mode="time" style="flex: 1;" v-if="item.fieldType === '6'" :value="item.defaultValue" @change="e => onDateChange(e, index)">
          <div class="right">
            <div class="right-left">
              <h2>{{ mapFieldLable(item.type) }}</h2>
              <p>{{ item.defaultValue || '' }}</p>
            </div>
            <u-icon name="arrow-right" color="#E1E2E3" size="#E1E2E3"></u-icon>
          </div>
        </picker>
      </div>
    </div>
    <div class="form-btn" hover-class="text-hover" @click="save">保存</div>
  </div>
</template>
<script>
  import { mapFieldLable } from './config'

  export default {
    data () {
      return {
        config: [],
        mapFieldLable
      }
    },

    onLoad () {
      this.config = uni.getStorageSync('waterConfig')

      uni.$on('change', e => {
        this.config.forEach((v, index) => {
          if (v.type === e.type) {
            this.$set(this.config[index], 'defaultValue', e.defaultValue)
          }
        })
      })
    },

    methods: {
      linkTo (url) {
        uni.navigateTo({
          url
        })
      },

      onDateChange (e, index) {
        this.$set(this.config[index], 'defaultValue', e.detail.value)
      },

      onChange (e, index) {
        this.$set(this.config[index], 'status', e.detail.value ? '1' : '0')
      },

      toInput (e) {
        if (e.editEnable === '0') return

        if (e.fieldType === '2') return

        uni.setStorageSync('formConfig', e)
        this.linkTo('./Form')
      },

      save () {
        for (let i = 0; i < this.config.length; i ++) {
          if ((this.config[i].fieldType === '0' || this.config[i].fieldType === '1') && !this.config[i].defaultValue && this.config[i].status === '1') {
            return this.$u.toast(`请输入${this.mapFieldLable(this.config[i].type)}`)
          }
        }

        uni.$emit('change', this.config)
        uni.navigateBack({
          delta: 1
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .AttendanceFiexdTime {
    padding-bottom: 130px;

    * {
      line-height: 1;
      box-sizing: border-box;
    }

    .form-btn {
      position: fixed;
      bottom: 0;
      left: 0;
      z-index: 1;
      width: 100%;
      height: 112px;
      line-height: 112px;
      text-align: center;
      color: #fff;
      font-size: 32px;
      background: #1365DD;

      &:active {
        opacity: 0.8;
      }
    }
    .form-group {
      margin-bottom: 16px;
      padding: 0 32px;
      background: #fff;

      .form-group__item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 32rpx 0;
        font-size: 32px;
        color: #333333;
        border-bottom: 1px solid #DDDDDD;

        &:last-child {
          border: none;
        }


        .right {
          display: flex;
          align-items: center;
          flex: 1;
          margin-left: 36px;

          .right-left {
            flex: 1;

            h2 {
              margin-bottom: 12px;
              color: #999999;
              font-size: 28px;
            }

            p {
              font-size: 34px;
              color: #333;
            }
          }
        }


        .left {
          display: flex;
          align-items: center;
          margin-right: 10px;
        }
      }
    }
  }
</style>
