<template>
  <div class="pic-manage">
    <new-card cardTitle="图片管理">
      <el-button
        class="mt20"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
        >新增</el-button
      >
      <el-table :data="dataList">
        <el-table-column label="监测项目" align="center" prop="monType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.slope_mon_eq"
              :value="scope.row.monType"
            />
          </template>
        </el-table-column>
        <el-table-column label="主图" align="center" prop="image">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="`${appPicApi}${scope.row.image}`"
              :preview-src-list="[`${appPicApi}${scope.row.image}`]"
              fit="contain"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-upload
              class="upload"
              ref="upload"
              action="#"
              accept=".jpg, .jpeg, .png, .gif"
              :http-request="
                (file) => {
                  return uploadFile(file, scope.row);
                }
              "
              :show-file-list="false"
            >
              <el-button type="text" size="small">上传主图</el-button>
            </el-upload>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            > -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </new-card>
    <!-- 添加或修改图片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="监测项目" prop="monType">
          <el-select
            v-model="form.monType"
            placeholder="请输入监测项目"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.slope_mon_eq"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上传主图" prop="image">
          <el-upload
            class="upload-demo"
            action="#"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
            accept=".jpg, .jpeg, .png, .gif"
            :http-request="
              (scope) => {
                return uploadImage(scope);
              }
            "
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件</div>
          </el-upload>
        </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 { imageList, imageAdd, imageEdit, imageDel } from "@/api/lclApi/lclapi";
import { commonUpload } from "@/api/lclApi/formData";
import cardLcl from "@/components/lclCard/index.vue";
export default {
  name: "pic-manage",
  dicts: ["slope_mon_eq"],
  components: { "new-card": cardLcl },
  props: [],
  data() {
    return {
      dataList: [],
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      fileList: [],
      title: "",
      open: false,
      appPicApi: "",
    };
  },
  watch: {},
  created() {
    this.$nextTick(function () {});
  },
  mounted() {
    this.$nextTick(function () {
      this.getList();
      this.appPicApi = this.$store.getters.appPicApi;
    });
  },
  methods: {
    reset() {
      this.form = {
        id: null,
        monType: null,
        image: null,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.fileList = [];
      this.reset();
      this.open = true;
      this.title = "添加项目图片";
    },
    getList() {
      imageList({ pageNum: 1, pageSize: 999 }).then((res) => {
        if (res && res.code === 200) {
          this.dataList = res.rows;
        }
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      if (this.form.id != undefined) {
        updateUser(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
          this.reset();
        });
      } else {
        imageAdd(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
          this.reset();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return imageDel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleUpdate(row) {
      console.log(row);
    },
    //  文件上传
    uploadFile(file, row) {
      console.log(row, "row");
      this.form = row;
      let that = this;
      const formData = new FormData();
      formData.append("file", file.file);
      commonUpload(formData).then((res) => {
        if (res.code == 200) {
          this.form.image = res.fileName;
          this.$message({
            type: "success",
            message: "上传成功",
          });
          imageEdit(that.form).then((res) => {
            this.getList();
          });
        } else {
          this.$message({
            type: "error",
            message: "上传失败",
          });
        }
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    uploadImage(scope) {
      const formData = new FormData();
      formData.append("file", scope.file);
      commonUpload(formData).then((res) => {
        if (res && res.code === 200) {
          this.form.image = res.fileName;
          this.$modal.msgSuccess("上传成功");
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.pic-manage {
  width: 100%;
}
</style>