<template>
  <Modal
    :options="getModalOptions"
    :on-positive-click="handleConfirm"
    :on-negative-click="handleIgnore"
    :on-close="handleClose"
  >
    <template #Context>
      <div class="carousel__flex">
        <n-icon size="26" color="#8A8A8A">
          <LeftOutlined @click="handleCarousel('prev')" />
        </n-icon>
        <div class="carousel__container">
          <label>问题描述</label>
          <n-carousel ref="carouselRef" :default-index="selectIndex" :show-dots="false">
            <img
              v-for="item in getCarouselInfo"
              :key="item.id"
              class="carousel-img"
              :src="item.fileMarkerUrl"
            >
          </n-carousel>
        </div>
        <n-icon size="26" color="#8A8A8A">
          <RightOutlined @click="handleCarousel('next')" />
        </n-icon>
      </div>
      <div class="modal-form">
        <label>问题类型</label>
        <n-select
          v-model:value="selectType"
          :options="QUESTION_TYPE"
        />
      </div>
    </template>
  </Modal>
</template>

<script>
import { QUESTION_TYPE } from '@/utils/dictionary.js'
import { changeQuestionStatus } from '@/api/task/index.js'
import { LeftOutlined, RightOutlined } from '@vicons/antd'
import Modal from '@/components/Modal/index.vue'
import { defineComponent, computed, ref, reactive, toRefs } from 'vue'
export default defineComponent({
  name: 'UserModal',
  components: { Modal, LeftOutlined, RightOutlined },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Array,
      default: () => null
    },
    selectRow: {
      type: Object,
      default: () => {}
    }
  },
  emits: {
    'update:visible': null,
    'update-data': null,
    'done': null
  },
  setup(props, { emit }) {
    const formRef = ref()
    const data = reactive({
      selectRow: props.selectRow,
      selectIndex: props.data.findIndex((item) => { return item.id === props.selectRow.id }),
      selectType: props.selectRow.type,
      QUESTION_TYPE
    })
    /* 获取弹窗的属性 */
    const getModalOptions = computed(() => {
      return {
        show: props.visible,
        title: '问题确认',
        width: 700,
        negativeText: data.selectRow.status !== 2 ? '忽略' : '',
        positiveText: data.selectRow.status !== 1 ? '确认' : ''
      }
    })

    const getCarouselInfo = computed(() => {
      return props.data
    })

    const carouselRef = ref(null)

    function handleCarousel(type) {
      const currentIndex = carouselRef.value.getCurrentIndex()

      switch (type) {
        case 'prev':
          if (currentIndex !== 0) {
            carouselRef.value.prev()
            data.selectRow = props.data[currentIndex - 1]
            data.selectType = props.data[currentIndex - 1].type
          }
          break
        case 'next':
          if (currentIndex !== props.data.length - 1) {
            carouselRef.value.next()
            data.selectRow = props.data[currentIndex + 1]
            data.selectType = props.data[currentIndex + 1].type
          }
          break
      }
    }

    /**
     * @description: 保存
     * @return {*}
     */
    const handleConfirm = () => {
      const params = {
        id: [data.selectRow.id],
        type: data.selectType,
        status: 1
      }
      changeQuestionStatus(params)
        .then(res => {
          if (res.code === 0) {
            data.selectRow.status = 1
            handleCarousel('next')
            emit('update-data')
          }
        })
    }
    const handleIgnore = () => {
      const params = {
        id: [data.selectRow.id],
        type: data.selectType,
        status: 2
      }
      changeQuestionStatus(params)
        .then(res => {
          if (res.code === 0) {
            data.selectRow.status = 2
            handleCarousel('next')
            emit('update-data')
          }
        })
    }

    /* 关闭弹窗 */
    const handleClose = () => {
      emit('update:visible', false)
    }

    return {
      ...toRefs(data),
      formRef,
      getModalOptions,
      getCarouselInfo,
      carouselRef,
      handleCarousel,
      handleConfirm,
      handleIgnore,
      handleClose
    }
  }

})
</script>
<style scoped lang='scss'>
.carousel__flex{
  display: flex;
  align-items: center;
  justify-content: space-between;
  .carousel__container{
    width: calc(100% - 100px);
    display: flex;
    margin-right: 40px;
    label{
      width: 70px;
      flex-shrink: 0;
    }
  }
  .n-icon{
    cursor: pointer;
  }
  .carousel-img{
    width: 100%;
    height: 280px;
    object-fit: cover
  }
}

.modal-form{
  width: calc(100% - 60px);
  display: flex;
  align-items: center;
  margin: 20px auto 0;
  label{
    width: 70px;
    flex-shrink: 0;
  }
  .n-select{
    width: 300px;
  }
}
</style>
