<template>
	<view class="f-straight">
    <uni-forms ref="form" :modelValue="formData" :rules="rules" validateTrigger="bind" label-width="90px">
      <uni-forms-item label="培训中心" name="orgName" required>
				<uni-easyinput type="text" v-model="formData.orgName" placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>
      <uni-forms-item label="培训课程" name="course" required>
				<uni-easyinput type="text" v-model="formData.course" placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>
      <uni-forms-item label="开始时间" name="startTime" required>
				<picker mode="date" :value="formData.startTime" fields="month" :end="endDate" @change="e => formData.startTime = e.detail.value">
					<view class="uni-input ss-m-t-20">{{ formData.startTime }}</view>
				</picker>
			</uni-forms-item>
      <uni-forms-item label="结束时间" name="endTime" required>
				<picker mode="date" :value="formData.endTime" fields="month" :end="endDate" @change="e => formData.endTime = e.detail.value">
          <view class="uni-input ss-m-t-20">{{ formData.endTime }}</view>
        </picker>
			</uni-forms-item>
      <uni-forms-item label="培训描述" name="content">
				<uni-easyinput type="textarea" v-model="formData.content" autoHeight  placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>
    </uni-forms>
	</view>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { ref, watch, unref } from 'vue'
import { convertYearMonthToTimestamp, timesTampChange } from '@/utils/date.js'
import { removeEmptyProperties } from "@/utils/index"
const props = defineProps({
  id: {
    type: String,
    default: ''
  },
  text: {
    type: String,
    default: ''
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

let formData = ref({
  startTime: '2014-01',
  endTime: '2018-01'
})
const form = ref()
const date = new Date()
const endDate = date.getFullYear() + '-' + (date.getMonth() + 1) // 不可选时间

const getInfo = (data) => {
  data.startTime = data.startTime ? timesTampChange(data.startTime, 'Y-M') : '2014-01'
  data.endTime = data.endTime ? timesTampChange(data.endTime, 'Y-M') : '2018-01'
  formData.value = cloneDeep(data) || {
    startTime: '2014-01',
    endTime: '2018-01'
  }
}

watch(
  () => props.data,
  (newVal) => {
    if (newVal && Object.keys(newVal)) {
      const data = removeEmptyProperties(newVal)
      getInfo(data)
    }
  },
  { immediate: true },
)

const rules = {
	orgName:{
		rules: [{required: true, errorMessage: '请输入培训中心' }]
	},
	course:{
		rules: [{required: true, errorMessage: '请输入培训课程' }]
	},
	startTime:{
		rules: [{required: true, errorMessage: '请选择培训开始时间' }]
	},
  endTime:{
		rules: [{required: true, errorMessage: '请选择培训结束时间' }]
	}
}

const submit = async () => {
  try {
    const valid = await unref(form).validate()
    // if (!valid) return { id: props.id, data: null}
    const startTime = convertYearMonthToTimestamp(formData.value.startTime)
    const endTime = convertYearMonthToTimestamp(formData.value.endTime)
    if (startTime > endTime) {
      uni.showToast({ icon: 'none', title: '开始时间不能大于结束时间', duration: 2000 })
      return
    }
    return { id: props.id, data: { ...formData.value, startTime, endTime }}
  } catch (error) {
    return { text: props.text }
  }
}

defineExpose({
  id: props.id,
  submit
})


</script>

<style lang="less" scoped>
.upload-img{
  position: relative;
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
}
.upload-file{
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 10rpx;
}
</style>