<template>
  <el-dialog
    title="编辑"
    :visible.sync="dialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :before-close="handleClose"
    class="homeEditIconDiolog"
  >
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="上传默认图标:" prop="title">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleIconSuccess"
          :before-upload="beforeUpload"
          :multiple="false"
          :limit="1"
        >
          <img v-if="form.iconSrc" :src="form.iconSrc" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="上传选中图标:" prop="src" v-if="form.iconSelectedSrc">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleSelectedSuccess"
          :before-upload="beforeUpload"
          :multiple="false"
          :limit="1"
        >
          <img
            v-if="form.iconSelectedSrc"
            :src="form.iconSelectedSrc"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "homeEditIconDiolog",
  props: {
    icon: {
      type: String,
    },
    iconSelected: {
      type: String,
    },
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        iconSrc: "",
        iconSelectedSrc: "",
      },
    };
  },
  methods: {
    handleClose() {
      this.form.iconSrc = this.form.iconSelectedSrc = ''
      this.dialogVisible = false;
    },
    handleConfirm() {
      this.$emit("handleIconSuccess", this.form);
      this.handleClose();
    },
    // 图片大图上传成功
    handleIconSuccess(response, file, fileList) {
      this.form.iconSrc = URL.createObjectURL(file.raw);
    },
    // 图片大图上传成功
    handleSelectedSuccess(response, file, fileList) {
      this.form.iconSelectedSrc = URL.createObjectURL(file.raw);
    },
    // 图片大图上传前
    beforeUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  watch: {
    dialogVisible(newVal) {
      if (newVal) {
        this.form.iconSrc = this.icon;
        if (this.iconSelected) this.form.iconSelectedSrc = this.iconSelected;
      }
    },
  },
};
</script>

<style lang='scss'>
.homeEditIconDiolog {
  .el-form-item {
    text-align: center;

    .el-form-item__label {
      margin-top: 30px;
    }
  }
  .avatar-uploader {
    .el-upload {
      border: 1px dashed #000;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      width: 100px;
      height: 100px;
    }

    .el-upload:hover {
      border-color: #409eff;
    }
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style>