import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
import { upload } from "../../../../lib/request/upload";

// module/chats/pages/analysis/analysis.ts

Component({
  behaviors: [PageContainerBehavior],
  options: {},
  properties: {},
  data: {
    uploadList: [
      { target: 'tongueImgUrl', required: true, label: '舌面图', src: '../../assets/tongue-1.png' },
      { target: 'tongueBackImgUrl', required: true, label: '舌下图', src: '../../assets/tongue-2.png' },
      { target: 'faceImgUrl', required: false, label: '面部图', src: '../../assets/face-1.png' },
    ],
    thumbnail: [] as string[],
    original: [
      // 'http://121.43.162.141:9300/statics/2024/07/13/1_20240713152314A029.JPG',
      // 'http://121.43.162.141:9300/statics/2024/07/13/2_20240713152711A030.JPG',
      // 'http://121.43.162.141:9300/statics/2024/07/13/3_20240713152819A031.png',
    ] as string[],
    status: [false, false, false],
    _queue: {} as AnyObject,
  },
  methods: {
    handle(event: WechatMiniprogram.TouchEvent) {
      const { handle, index } = event.mark as AnyObject;
      console.log(handle, index, handle === 'upload:delete', event.mark);
      switch (handle) {
        case 'preview':
          break;
        case 'upload':
          this._chooseMedia(index).then(src => this._uploadMedia(index, src))
          break;
        case 'upload:delete':
          this._deleteMedia(index);
          break;
      }
    },
    _chooseMedia(index: number) {
      return wx.chooseMedia({ count: 1, mediaType: ['image'], camera: 'front' })
        .then(res => {
          const src = res.tempFiles[0].tempFilePath;
          this.setData({ [`thumbnail.${index}`]: src });
          return src;
        })
        .catch(({ errMsg }) => {
          const message = (<AnyObject>{ 'chooseMedia:fail cancel': '取消上传' })[errMsg] ?? errMsg;
          wx.showToast({ title: message, icon: 'none' });
          throw { errMsg };
        })
    },
    _deleteMedia(index: number) {
      this.setData({
        [`thumbnail.${index}`]: '',
        [`original.${index}`]: '',
      })
    },
    _uploadMedia(index: number, src?: string) {
      src ??= this.data.thumbnail[index];
      upload({
        params: { name: 'file', file: src! },
        transform({ data }) { return (<any>data).url; }
      }).then(src => {
        this.setData({ [`original.${index}`]: src })
      }, () => {
        wx.showToast({ title: '上传失败', icon: 'error' })
        this.setData({
          [`thumbnail.${index}`]: '',
          [`original.${index}`]: '',
        })
      })
    },
    onSubmit() {
      const data = [];
      for (let index = 0; index < this.data.uploadList.length; index++) {
        const item = this.data.uploadList[index];
        if (item.required) {
          if (this.data._queue[index]) {
            wx.showToast({ title: `请等待${item.label}上传完成`, icon: 'loading' });
            return;
          } else if (!this.data.original[index]) {
            wx.showToast({ title: `请上传${item.label}`, icon: 'none' });
            return;
          }
        }
        if (this.data.original[index]) data.push({ target: item.target, src: this.data.original[index], })
      }
      this.getOpenerEventChannel().emit('update', data)
      wx.navigateBack()
    }
  }
})