<template>
  <view class="ss-p-x-30 ss-p-y-30">
		<view class="ss-m-b-30">
      <uni-easyinput v-model="date" type="text" disabled />
    </view>
		<uni-easyinput type="textarea" v-model="journal" clearable maxlength="500" autoHeight placeholder="请输入要记录的内容" />
    <view style="text-align: end;" class="ss-m-t-10 color-999">{{ journal?.length || 0 }}/500</view>
		<view class="f-horizon-center">
      <button
				v-if="isEdit"
				size="default"
				class="delete-button commonBtnStyle" 
				:disabled="deleteDisabled"
				@click="handleDelete"
			>删 除</button>
      <button 
        size="default" 
        :class="{'save-button': isEdit, 'commonBtnStyle': isEdit, 'send-button': !isEdit}" 
        @click="handleSubmit"
        :disabled="disabled"
       >保 存</button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { getCalendarRecord, saveCalendarRecord } from '@/api/drawLots.js'

const isEdit = ref(false)
const date = ref(null)
const journal = ref(null)
const disabled = ref(false)
const deleteDisabled = ref(false)
const userInfo = ref(uni.getStorageSync('wechat_user'))

// 获取手账记录
const calendarRecord = ref([])
const getCalendarData = async (month_key) => {
	try {
		const { result } = await getCalendarRecord({ month_key, openid: userInfo.value.openid })
		calendarRecord.value = result?.calendar_content || []
	} catch {}
}

onLoad((options) => {
  date.value = options?.date || null
  journal.value = options?.notes || null
  isEdit.value = options?.notes ? true : false

  getCalendarData(options?.date.slice(0, 7))
})

// 删除
const handleDelete = async () => {
  const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  if (index === -1) return uni.showToast({ title: '删除失败，当前数据不存在', icon: 'none', duration: 2000 })
  calendarRecord.value.splice(index, 1)
  deleteDisabled.value = true
  disabled.value = true

  try {
    await saveCalendarRecord({ openid: userInfo.value.openid, month_key: date.value.slice(0, 7), calendar_content: calendarRecord.value })
    uni.showToast({ title: '删除成功', icon: 'none', duration: 2000 })
    setTimeout(() => {
			uni.navigateBack({
				delta: 1
			})
			disabled.value = false
      deleteDisabled.value = false
		}, 1000)
  } catch {
    deleteDisabled.value = false
    disabled.value = false
  }
}

// 保存
const handleSubmit = async () => {
  if (!journal.value) return uni.showToast({ title: '请输入要记录的内容', icon: 'none', duration: 2000 })
  disabled.value = true

  let list = []
  if (!calendarRecord.value.length) {
    list = [{ date: date.value, events: [], notes: journal.value }]
  } else {
    const index = calendarRecord.value.findIndex((item) => item.date === date.value)
    if (index > -1) {
      calendarRecord.value[index].notes = journal.value
    } else {
      calendarRecord.value.push({ date: date.value, events: [], notes: journal.value })
    }
    list = calendarRecord.value
  }
  const params = {
    openid: userInfo.value.openid,
    month_key: date.value.slice(0, 7),
    calendar_content: list
  }

  console.log(params, 'params')
  try {
    await saveCalendarRecord(params)
    uni.showToast({ title: '保存成功', icon: 'none', duration: 2000 })
    setTimeout(() => {
			uni.navigateBack({
				delta: 1
			})
			disabled.value = false
		}, 1000)
  } catch {
    disabled.value = false
  }
}
</script>

<style lang="scss" scoped>
</style>