<template>
  <div class="form">
    <div class="form-group">
      <div class="form-item" :class="[config.fieldType === '1' ? 'textarea' : '']">
        <span>{{ mapFieldLable(config.type) }}</span>
        <div class="form-item__right" v-if="config.fieldType === '0'">
          <input :placeholder="'请输入' + mapFieldLable(config.type)" :maxlength="10" v-model="config.defaultValue">
          <span></span>
        </div>
        <div class="form-item__right" v-if="config.fieldType === 'date'" @click="isShowDate = true">
          <span :style="{color: config.defaultValue ? '#333' : '#999'}">{{ config.defaultValue || '请选择' + mapFieldLable(config.type) }}</span>
          <u-icon name="arrow-right" color="#E1E2E3" size="#E1E2E3"></u-icon>
        </div>
        <div class="form-item__right" v-if="config.fieldType === '1'">
          <textarea :placeholder="'请输入' + mapFieldLable(config.type)" v-model="config.defaultValue" :maxlength="-1"></textarea>
        </div>
      </div>
    </div>
    <div class="form-btn" hover-class="text-hover" @click="save">保存</div>
    <u-picker mode="time" v-model="isShowDate" :params="params" @confirm="onChange"></u-picker>
  </div>
</template>
<script>
  import { mapFieldLable } from './config'
  export default {
    data () {
      return {
        list: [],
        mapFieldLable,
        isShowDate: false,
        params: {
          year: true,
          month: true,
          day: true
        },
        currIndex: 0,
        config: {
          type: '',
          fieldType: '',
          defaultValue: ''
        }
      }
    },

    onLoad () {
      this.config = {
        ...this.config,
        ...uni.getStorageSync('formConfig')
      }
    },

    methods: {
      onChange (e) {
        this.$set(config, 'value', `${e.year}-${e.month}-${e.day}`)
      },

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

<style lang="scss" scoped>
  .form {
    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-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 120px;
        font-size: 32px;
        color: #333333;
        border-bottom: 1px solid #DDDDDD;

        &:last-child {
          border: none;
        }

        & > span {
          margin-right: 20px;
        }

        &.textarea {
          display: block;
          height: auto;
          padding: 32px 0;

          textarea {
            width: 100%;
            margin-top: 20px;
            text-align: left;
          }
        }

        .form-item__right {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          text-align: right;
          height: 100%;
          flex: 1;

          span {
            font-size: 30px;
            color: #333;
          }

          input {
            width: 100%;
            height: 100%;
            text-align: right;
            font-size: 30px;
            color: #333;
          }
        }
      }
    }
  }
</style>
