import React, { useState, useEffect } from 'react'
import Taro, { Current } from '@tarojs/taro'
import { Input, Textarea } from '@tarojs/components'
import request, { apis } from '@/utils/request'
import { useModel } from '@/store'
import '@/assets/css/reset.less'
import '@/assets/css/iconfont.less'
import './index.less'

export default function WuYeXiuGaiBaoXiu () {

  const { user } = useModel('user')
  const [DataLock, setDataLock] = useState(false)
  const [DetailInfo, setDetailInfo] = useState(null)
  const [CurrnetId] = useState(Current.router.params.id)

  useEffect(() => {
    Init()
  }, [CurrnetId])

  const Init = () => {
    request({ ...apis.getGongDanDetail, args: { orgId: user.orgId }, params: { ticketId: CurrnetId } }).then((res) => {
      setDetailInfo(res)
    })
  }

  const TitleChange = (e) => {
    setDetailInfo({ ...DetailInfo, ticketTitle: e.detail.value })
  }

  const DescChange = (e) => {
    setDetailInfo({ ...DetailInfo, ticketContent: e.detail.value })
  }

  const CheckForm = () => { // 校验报修单
    if (DetailInfo.ticketTitle === '') {
      Taro.showToast({ title: `报修标题不能为空`, icon: 'none' })
      return false
    }
    if (DetailInfo.ticketContent === '') {
      Taro.showToast({ title: `报修描述不能为空`, icon: 'none' })
      return false
    }
    return true
  }

  const Send = () => { // 提交报修
    if (DataLock || !CheckForm()) return
    setDataLock(true)
    request({
      ...apis.editGongDan,
      args: { id: CurrnetId },
      data: {
        ticketTitle: DetailInfo.ticketTitle,
        ticketContent: DetailInfo.ticketContent,
        type: 2
      }
    }).then((res) => {
      Taro.showToast({ title: '报修修改成功', icon: 'none' })
      Taro.redirectTo({ url: `/pages/WuYe/BaoXiuDetail/index?id=${res.id}` })
      setDataLock(false)
    }).catch((res) => {
      Taro.showToast({ title: res, icon: 'none' })
      setDataLock(false)
    })
  }

  return (
    <view className='WuYeXiuGaiBaoXiu'>
      <view className='Form'>
        <Input placeholder='简述你的想法' onInput={TitleChange} value={DetailInfo === null ? null : DetailInfo.ticketTitle}></Input>
        <Textarea placeholder='描述问题详情，以便我们更好的相处' onInput={DescChange} value={DetailInfo === null ? null : DetailInfo.ticketContent}></Textarea>
      </view>
      <view className='Photo'>
        <view className='Add'>
          <view className='centerLabel'>
            <text className='iconfont iconxiangji'></text>
            <text>添加图片</text>
          </view>
        </view>
        <view className='PhotoItem'>
          <text className='iconfont iconshanchu'></text>
        </view>
      </view>
      <view className='Btn'>
        <text onClick={Send}>确认修改</text>
      </view>
    </view>
  )
}
