<template>
  <div class="basic-image">
    <template v-for="(image,index) in fileList" :key="index">
      <img-row :file="image"
               :config="config"
               :remove-code="config.removeCode"
               :removable="true"
               @preview="handlePictureCardPreview"
               @after-remove="removeFile"></img-row>
    </template>
    <cip-upload action="uploadUrl"
               listType="picture-card"
               :itemStyle="{width: config.itemWidth, height: config.itemHeight}"
               :accept="accept"
               :before-upload="beforeUpload"
               :uploadFile="uploadFile"
               :show-file-list="false"
               :disabled="uploadDisabled"
               multiple>
    </cip-upload>
    <div class="el-upload__tip" style="width: 100%">{{limitTip}}</div>
    <cip-dialog :title="(config.mediaType === 'video' ?'视频': '图片') +'预览'" v-model="dialogVisible" :show-only="true">
      <Media style="width:100%" :type="config.mediaType" :src="dialogImageUrl" alt=""/>
    </cip-dialog>
  </div>
</template>

<script>
import { computed, ref } from 'vue'
import CipDialog from '@cip/components/cip-dialog'
import Media from './media'
import ImgRow from './img-row'
import { formInputProps, fromInputEmits } from '../../form-input-props'
import { uploadProps } from '../file/upload'
import CipUpload from '@cip/components/cip-upload'

export default {
  props: formInputProps,
  components: { CipDialog, ImgRow, Media, CipUpload },
  emits: [...fromInputEmits],
  setup (props, context) {
    const { fileList, mainMessage, countLimit, typeLimit, sizeLimit, ...uploadUtils } = uploadProps(props, context)
    const dialogImageUrl = ref('')
    const dialogVisible = ref(false)
    // 验证文件是否可以上传
    const beforeUpload = (file) => {
      if (mainMessage(typeLimit(file), '图片上传类型超出限制') ||
        mainMessage(countLimit(file), '图片上传数量超出限制') ||
        mainMessage(sizeLimit(file), '图片上传大小超出限制')) {
        return false
      }
    }
    const handlePictureCardPreview = (file) => {
      dialogImageUrl.value = props.config.imgTemplate ? props.config.imgTemplate(file.url) : file.url
      dialogVisible.value = true
    }
    // 限制提示语
    const limitTip = computed(() => {
      const { fileType, size, limitTip: configLimitTip } = props.config
      if (configLimitTip) return configLimitTip
      if (!!fileType && !!size) return `只能上传${fileType}文件，且不超过${size}MB`
      if (!!fileType && !size) return `只能上传${fileType}文件`
      if (!fileType && !!size) return `只能上传不超过${props.config.size}MB的文件`
      return ''
    })
    const accept = computed(() => {
      const fileType = props.config.fileType
      if (fileType) {
        return '.' + fileType.replace(/,\s?/g, ',.')
      }
      return 'image/*'
    })
    return {
      ...uploadUtils,
      fileList,
      beforeUpload,
      handlePictureCardPreview,
      dialogVisible,
      dialogImageUrl,
      limitTip,
      accept
    }
  }
}
</script>

<style lang="less">
.basic-image{
  display: flex;
  flex-wrap: wrap;
  .cip-upload{
    margin: 0 8px 8px 0;
  }
  .image-plus{
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    box-sizing: border-box;
    width: 148px;
    height: 148px;
    cursor: pointer;
    line-height: 146px;
    vertical-align: top;
    text-align: center;
    i{
      font-size: 28px;
      color: #8c939d;
    }
    &:hover{
      border: 1px dashed #409eff;
    }
  }
}
</style>
