<template>
  <Modal
    :options="getModalOptions"
    :on-positive-click="handleConfirm"
    :on-negative-click="handleClose"
    :on-close="handleClose"
  >
    <template #Context>
      <n-form
        ref="formRef"
        :model="questionForm"
        :rules="questionRules"
        :label-width="120"
        label-placement="left"
        require-mark-placement="left"
        :disabled="disabled"
      >
        <template v-for="(item,index) in getFormOptions" :key="index">
          <n-form-item :label="item.label" :path="item.key">
            <n-input v-if="item.type === 'input'" v-model:value="questionForm[item.key]" v-bind="item.props" />
            <n-select v-if="item.type === 'select'" v-model:value="questionForm[item.key]" v-bind="item.props" />
            <n-image v-if="item.type === 'image'" v-model:src="questionForm[item.key]" v-bind="item.props" />
            <UploadOss v-if="item.type === 'oss'" :ref="el=>{ossRefs[item.refIndex] = el}" :limit="5" :default-list="questionForm[item.file]" @upload-status="handleUploadStatus" />
          </n-form-item>
        </template>
      </n-form>
    </template>
  </Modal>
</template>

<script>
import { form } from '../tools/quesForm.js'
import { defineComponent, ref, reactive, computed, toRefs } from 'vue'
import Modal from '@/components/Modal/index.vue'
import UploadOss from '@/components/UploadOss/index.vue'
import { handleOrder } from '@/api/order/index.js'
export default defineComponent({
  name: 'UserModal',
  components: { Modal, UploadOss },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'preview'
    },
    orderId: {
      type: Number,
      default: null
    },
    data: {
      type: Object,
      default: () => null
    }
  },
  emits: {
    'update:visible': null,
    'reload': null
  },
  setup(props, { emit }) {
    const MODAL_TYPE = {
      'preview': '问题详情',
      'update': '处理问题'
    }
    const { questionForm, questionRules } = form
    const formRef = ref()
    const data = reactive({
      questionForm: {
        ...questionForm,
        imageStatus: '',
        ...props.data,
        position: `${props.data.lng},${props.data.lat}`
      },
      questionRules: {
        ...questionRules
      },
      disabled: props.type === 'preview'
    })
    const getModalOptions = computed(() => {
      return {
        show: props.visible,
        title: MODAL_TYPE[props.type],
        negativeText: '取消',
        positiveText: '确认'
      }
    })

    const getFormOptions = computed(() => {
      if (props.type === 'preview' && props.data.status === 0) {
        const list = form.formItem.filter((item) => !item.mode)
        return {
          ...list
        }
      } else if (props.data.status === 0) {
        const list = form.formItem.filter((item) => !item.handle)
        return {
          ...list
        }
      } else {
        return {
          ...form.formItem
        }
      }
    })

    const ossRefs = ref([])
    function handleUploadStatus(status) {
      switch (status) {
        case 'no-file':
          data.questionForm.imageStatus = ''
          break
        default:
          data.questionForm.imageStatus = status
      }
    }

    function handleConfirm() {
      if (props.type === 'preview') {
        handleClose()
        return
      }
      formRef.value?.validate((errors) => {
        if (!errors) {
          const uploads = ossRefs.value.map((item, index) => {
            return item.startUpload()
          })
          Promise.all(uploads)
            .then(response => {
              const isError = response.map((item) => {
                return item.includes('error')
              })
              if (!isError.includes(true)) {
                const imageStr = response.join()
                const params = {
                  workOrderId: props.orderId,
                  ...data.questionForm,
                  handlerImage: imageStr
                }
                handleOrder(params)
                  .then(res => {
                    if (res.code === 0) {
                      emit('reload')
                      handleClose()
                    }
                  })
              }
            })
        } else {
          $message.error('请完善必填信息')
        }
      })
    }

    /* 关闭弹窗 */
    const handleClose = () => {
      emit('update:visible', false)
    }
    return {
      ...toRefs(data),
      formRef,
      getModalOptions,
      getFormOptions,
      ossRefs,
      handleUploadStatus,
      handleConfirm,
      handleClose
    }
  }
})
</script>
<style scoped lang='scss'>
</style>
