import TicketDetail from '@/pages/TicketDetail/TicketDetail';
import { TicketInfo, TicketNote } from '@/pages/TicketDetail/TicketNote';
import Taro from '@tarojs/taro';
import moment from 'moment';
import { getUrl } from '.';

export function getTicketInfo(that: TicketDetail, id: number) {
  Taro.request({
    url: getUrl('/tickets/info'),
    method: 'GET',
    data: {
      id: id,
    },
  })
    .then(res => {
      if (!res.data.success) {
        that.setState({
          rs: {
            loading: false,
            success: false,
          },
        });
      } else {
        const data = res.data.data;
        const ticketDetail: TicketInfo = {
          id: data.id,
          type: data.type,
          device: data.device,
          deviceModel: data.deviceModel,
          description: data.description,
          createdTime: moment(data.createdTime as string),
          status: data.status,
        };
        const notes: Array<TicketNote> = [];
        data.notes.map(item => {
          notes.push({
            id: item.id,
            op: item.op,
            type: item.type,
            content: item.content,
            createdTime: moment(item.createdTime as string),
          });
        });
        that.setState({
          ticketInfo: ticketDetail,
          notes: notes,
          rs: {
            loading: false,
            success: true,
          },
        });
      }
    })
    .catch(reason => {
      that.setState({
        rs: {
          loading: false,
          success: false,
        },
      });
      console.log(reason);
    });
}
