<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-upload
      :action="uploadUrl"
      class="up-pictures"
      :data="params"
      :file-list="pictureList"
      list-type="picture-card"
      :on-success="handleAvatarSuccess"
    >
      <i slot="default" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{ file }">
        <img
          alt=""
          class="el-upload-list__item-thumbnail"
          :src="file.media_url"
        />
        <span class="el-upload-list__item-actions">
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <!--      <el-button @click="close">{{ __('取消') }}</el-button>-->
      <el-button type="primary" @click="close">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { URL } from '@/config'
  import { batchSaveMedia } from '@/api/store/media'
  import { getToken } from '@/utils/token'
  import { mapGetters } from 'vuex'

  export default {
    name: 'UploadPictures',
    components: {},
    data() {
      return {
        params: {
          authorization: token,
          gallery_id: 0,
        },
        pictureList: [],
        disabled: false,
        uploadUrl: URL.action,
        gallery_id: 0,
        title: this.__('上传图片'),
        dialogFormVisible: false,
      }
    },
    computed: {
      ...mapGetters({
        token: 'user/token',
      }),
    },
    created() {
      this.params.authorization = getToken()
    },
    methods: {
      __,
      showEdit(gallery_id) {
        if (gallery_id) {
          this.params.gallery_id = gallery_id
        }
        this.dialogFormVisible = true
      },
      close() {
        this.pictureList = []
        this.params.gallery_id = 0
        this.$emit('fetch-data')
        this.dialogFormVisible = false
      },
      async save() {
        let pictureList = this.pictureList
        if (pictureList.length > 0) {
          const { msg, status } = await batchSaveMedia({
            strMedias: JSON.stringify(pictureList),
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage('请先上传图片！', 'success')
        }
      },
      handleRemove(file) {
        let name = file.name
        let pictureList = this.pictureList
        pictureList.splice(
          pictureList.findIndex((item) => item.media_name === name),
          1
        )
      },
      handleAvatarSuccess(res, file) {
        if (res.status === 200) {
          let obj = {
            media_url: res.data,
            media_name: file.name,
            media_alt: file.name,
            media_mime_type: 'image',
            media_order: 1,
            gallery_id: this.gallery_id,
            media_desc: '',
          }
          this.pictureList.push(obj)
        }
      },
    },
  }
</script>
<style>
  .up-pictures .el-upload-list__item {
    transition: none !important;
  }

  .up-pictures .el-upload--picture-card {
    transition: none !important;
    width: 76px;
    height: 76px;
    line-height: 84px;
  }

  .up-pictures .el-upload-list--picture-card .el-upload-list__item {
    width: 76px;
    height: 76px;
  }
</style>
