<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属分类" prop="fileType">
        <el-select v-model="queryParams.fileType" placeholder="请选择所属分类" clearable>
          <el-option
            v-for="dict in dict.type.file_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入图片名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd('product')"
          v-hasPermi="['base:pics:add']"
        >上传产品图</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd('news')"
          v-hasPermi="['base:pics:add']"
        >上传新闻图</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd('other')"
          v-hasPermi="['base:pics:add']"
        >上传其他图</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:pics:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--    选择图片的组件-->
<!--    <div class="photo-wall">-->
<!--      <div class="photo-box" v-for="(photo, index) in picsList"-->
<!--           :key="index" :title="photo.name">-->
<!--        <el-image-->
<!--          :src="photo.url"-->
<!--          style="width: 160px; height: 160px"-->
<!--          alt="photo"-->
<!--          fit="fill"-->
<!--          class="photo-image"-->
<!--          lazy-->
<!--          :preview-src-list="picsList.map(p => p.url)"-->
<!--        >-->
<!--          <div slot="error" class="image-slot">-->
<!--            <i class="el-icon-picture-outline"></i>-->
<!--          </div>-->
<!--        </el-image>-->
<!--        <div class="photo-name" >{{ photo.name }}</div>-->
<!--        <div class="photo-checkbox"><el-checkbox size="medium" v-model="photo.checked"></el-checkbox></div>-->
<!--      </div>-->
<!--      <div style="clear: both"></div>-->
<!--    </div>-->

    <el-table v-loading="loading" :data="picsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图片名称" align="center" prop="name" />
      <el-table-column label="所属分类" align="center" prop="fileType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.file_type" :value="scope.row.fileType"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="url" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.url" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="图片大小" align="center" prop="size" >
        <template slot-scope="scope">
          <span>{{scope.row.size | filterSize}}</span>
        </template>
        </el-table-column>
      <el-table-column label="图片宽" align="center" prop="width" />
      <el-table-column label="图片高" align="center" prop="height" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:pics:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图片库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属分类" prop="fileType">
          <el-select v-model="form.fileType" placeholder="请选择所属分类">
            <el-option
              v-for="dict in dict.type.file_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="url">
          <image-upload @uploadedImg="uploadedImg" v-model="form.url" :limit="9"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPics, getPics, delPics, addPics, updatePics } from "@/api/base/pics";

export default {
  name: "Pics",
  dicts: ['file_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图片库表格数据
      picsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileType: null,
        name: null,
        url: null,
        size: null,
        width: null,
        height: null,
        remark: null,
        tenantId: null,

      },
      // 表单参数
      form: {
        fileList:[]
      },
      // 表单校验
      rules: {
      }
    };
  },
  filters:{
    filterSize:function(size){
      if (!size){
        return "";
      }
      var num = 1024.00
      if (size < num){
        return size + "K";
      }else if(size < Math.pow(num, 2)){
        return (size / num).toFixed(2) + "M"; //M
      }else if(size < Math.pow(num, 3)){
        return (size / Math.pow(num, 2)).toFixed(2) + "G"; //G
      }else {
        return (size / Math.pow(num, 3)).toFixed(2) + "T"; //t
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    formatData (row, column, cellValue) {
      var size = row.store;

    },
    /** 查询图片库列表 */
    getList() {
      this.loading = true;
      listPics(this.queryParams).then(response => {
        this.picsList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    uploadedImg(fileList){
      this.form.fileList = this.form.fileList.concat(fileList)
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileType: null,
        name: null,
        url: null,
        size: null,
        width: null,
        height: null,
        remark: null,
        tenantId: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: null,
        fileList:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(code) {
      this.reset();
      this.open = true;
      this.form.fileType=code
      this.title = "添加图片";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPics(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图片库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form)
          if (this.form.id != null) {
            updatePics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除图片库编号为"' + ids + '"的数据项？').then(function() {
        return delPics(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/pics/export', {
        ...this.queryParams
      }, `pics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.photo-wall {
  margin: auto;
}
.photo-box{
  float: left;
  margin: 20px 10px;
  position: relative;
}
.photo-name{
  padding: 4px 5px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  position: absolute;
  bottom: 0;
  width: 100%;
  text-align: center;
  height: 32px;
  line-height: 28px;
  background-color: rgba(194, 194, 194, 0.5); /* 灰色，半透明 */
}
.photo-checkbox{
  position: absolute;
  left: 0;
  top: 0;
}

</style>
