<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
      <uni-forms-item name="infoName" label="资料名称" required>
        <uni-easyinput placeholder="资料名称" v-model="formData.infoName"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="infomation" label="资料内容">
        <uni-easyinput placeholder="资料内容" v-model="formData.infomation" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="infoSouce" label="资料来源">
        <uni-easyinput placeholder="资料来源" v-model="formData.infoSouce" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="infoType" label="资料类型">
        <uni-easyinput placeholder="资料类型" v-model="formData.infoType" trim="both"></uni-easyinput>
      </uni-forms-item>
     <!-- <uni-forms-item name="createBy" label="创建人">
       <uni-easyinput placeholder="创建人" v-model="formData.createBy" trim="both"></uni-easyinput>
     </uni-forms-item>
     <uni-forms-item name="updateBy" label="更新人">
       <uni-easyinput placeholder="更新人" v-model="formData.updateBy" trim="both"></uni-easyinput>
     </uni-forms-item>
     <uni-forms-item name="createTime" label="创建时间">
       <uni-datetime-picker  v-model="formData.createTime"></uni-datetime-picker>
     </uni-forms-item>
     <uni-forms-item name="updateTime" label="更新时间">
       <uni-datetime-picker  v-model="formData.updateTime"></uni-datetime-picker>
     </uni-forms-item> -->
      <view class="uni-button-group">
        <button type="primary" class="uni-button" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/ngtools_information.js';

  const db = uniCloud.database();
  const dbCollectionName = 'ngtools_information';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.indexOf(key) > -1) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "infoName": "",
        "infomation": "",
        "infoSouce": "",
        "infoType": "",
        "createTime": null,
        "createBy": "",
        "updateTime": null,
        "updateBy": ""
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>

<style>
  .uni-container {
    padding: 15px;
  }

  .uni-input-border,
  .uni-textarea-border {
    width: 100%;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    box-sizing: border-box;
  }

  .uni-input-border {
    padding: 0 10px;
    height: 35px;

  }

  .uni-textarea-border {
    padding: 10px;
    height: 80px;
  }

  .uni-button-group {
    margin-top: 50px;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    justify-content: center;
  }

  .uni-button {
    width: 184px;
  }
</style>
