<template>
  <view class="uni-app">
    <view class="status-bar" />
    <view class="main-container">
      <wk-nav-bar :title="navTitle">
        <!-- #ifndef MP-WEIXIN -->
        <button
          class="button white-btn"
          @click="handleSave">
          保存
        </button>
        <!-- #endif -->
      </wk-nav-bar>
			
      <view class="container">
        <wk-form ref="form" :fields="fieldArr" />
      </view>
			
      <!-- #ifdef MP-WEIXIN -->
      <view class="footer-btn-group">
        <button class="button" @click="handleSave">
          保存
        </button>
      </view>
      <!-- #endif -->
    </view>
  </view>
</template>

<script>
  import {
    SaveWorkTask,
    UpdateWorkChildTask,
    AddWorkChildTask
  } from 'API/oa/task'
	
  import Fields from '@/utils/fields.js'
  import { guid } from '@/utils/lib.js'
  import { mapGetters } from 'vuex'
  import moment from 'moment'
	
  export default {
    name: 'AddTask',
    data() {
      return {
        id: null,
        pid: null,
        fieldArr: [],
        loading: false
      }
    },
    computed: {
			...mapGetters({
				userInfo: 'user/userInfo'
			}),
			navTitle() {
				if (!this.pid) return '新建任务'
				return this.id ? '编辑子任务' : '添加子任务'
			}
    },
    onLoad(options) {
      this.id = options.id || null
      this.pid = options.pid || null
      this.initForm()
    },
    onUnload() {
      getApp().globalData.formBridge = {}
    },
    methods: {
      initForm() {
        this.fieldArr = [
          new Fields({
            name: '任务名称',
            fieldName: 'name',
            formType: 'text',
            isNull: 1
          }),
          new Fields({
            name: '负责人',
            fieldName: 'mainUserId',
            formType: 'single_user',
            isNull: 1,
            value: [this.userInfo]
          }),
          new Fields({
            name: '结束时间',
            fieldName: 'stopTime',
            formType: 'date'
          })
        ]
        if (this.pid) {
          const bridge = getApp().globalData.formBridge
          if (!bridge.default) return
          this.$nextTick(() => {
            this.$refs.form.setForm(bridge.default || {})
          })
        }
      },
			
      handleSave() {
        if (this.loading) return
        this.loading = true
        this.$refs.form.getForm().then(form => {
          form = form.entity
          // if (form.stopTime && moment().isAfter(form.stopTime)) {
          // 	this.$toast('结束时间必须大于当前时间')
          // 	this.loading = false
          // 	return
          // }
					
          if (this.id) form.taskId = this.id
          if (this.pid) form.pid = this.pid
					
          let request = null
          if (this.pid) {
            request = this.id ? UpdateWorkChildTask : AddWorkChildTask
          } else {
            request = SaveWorkTask
          }
					
          console.log('save: ', form)
          request(form).then(() => {
            this.$toast(this.id ? '编辑成功' : '添加成功')
            this.loading = false
            this.$refreshAndToPrev(this)
          }).catch()
        }).catch(() => {
          this.loading = false
        })
      }
    }
  }
</script>

<style scoped lang="scss">
	.main-container {
		background-color: white;
		.container {
			flex: 1;
		}
	}
</style>
