<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >新增</el-button
      >
    </el-row>
    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column
        label="创建时间"
        align="center"
        width="150"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="图片"
        align="center"
        width="150"
        prop="imageUrl"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <img style="width: 100px;height: 100px;" :src="scope.row.imageUrl" alt="">
        </template>
      </el-table-column>
      <el-table-column
        label="工具名称"
        align="center"
        width="100"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工具链接"
        align="center"
        width="200"
        prop="toolUrl"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工具类型"
        align="center"
        width="100"
        prop="typeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="状态"
        align="center"
        width="100"
        prop="status"
        :show-overflow-tooltip="true"
      >
       <template slot-scope="scope">
        {{scope.row.status === 1 ? '启用' : "禁用"}}
        </template>
    </el-table-column>
   <el-table-column
        label="描述"
        align="center"
        width="200"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="showEdit(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-dialog title="添加邮箱" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="邮箱" required>
              <el-input
                v-model="form.recipientEmail"
                type="input"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" required>
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    <!-- 新增 -->
    <el-dialog title="新增工具" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="24">
            <img v-if="form.imageUrl" style="width: 100px;height: 100px;" :src="form.imageUrl" alt="">
            <div class="upload-container" style="display: flex;margin:10px 0;margin-left:30px" v-loading="uploadLoading" element-loading-text="上传中...">
                <el-upload
                :action="uploadFileUrl"
                :before-upload="handleBeforeUpload"
                :limit="1"
                :on-error="handleUploadError"
                :on-exceed="handleExceed"
                :on-success="handleUploadSuccess"
                :show-file-list="false"
                :headers="headers"
                class="upload-file-uploader"
                ref="fileUpload"
                >
                <!-- 上传按钮 -->
                <el-button class="upload-button" size="mini" type="primary">选取图片</el-button>
                <!-- 上传提示 -->
                </el-upload>
            </div>
          </el-col>
           <el-form-item label="状态">
              <el-select style="width: 100%;" v-model="form.status" placeholder="状态">
                <el-option v-for="dict in statusList" :key="dict.dictValue" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工具名称">
              <el-input style="width: 100%;" v-model="form.name" placeholder="请输入工具名称"></el-input>
            </el-form-item>
            <el-form-item label="工具链接">
              <el-input style="width: 100%;" v-model="form.toolUrl" placeholder="请输入工具链接"></el-input>
            </el-form-item>

             <el-form-item label="工具类型">
              <el-select style="width: 100%;" v-model="form.type" placeholder="工具类型">
                <el-option v-for="dict in toolTypeList" :key="dict.dictValue" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="描述">
              <el-input type="textarea" style="width: 100%;" v-model="form.remark" placeholder="请输入描述"></el-input>
            </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
     <el-dialog title="编辑工具" :visible.sync="editOpen" width="600px" append-to-body>
      <el-form ref="editForm" :model="editForm" label-width="80px">
        <el-row>
          <el-col :span="24">
            <img v-if="editForm.imageUrl" style="width: 100px;height: 100px;margin-left:30px" :src="editForm.imageUrl" alt="">
            <div class="upload-container" style="display: flex;margin:10px 0;margin-left:30px" v-loading="uploadLoading" element-loading-text="上传中...">
                <el-upload
                :action="uploadFileUrl"
                :before-upload="handleBeforeUpload"
                :limit="1"
                :on-error="handleUploadError"
                :on-exceed="handleExceed"
                :on-success="handleUploadSuccess"
                :show-file-list="false"
                :headers="headers"
                class="upload-file-uploader"
                ref="fileUpload"
                >
                <!-- 上传按钮 -->
                <el-button class="upload-button" size="mini" type="primary">重新选取图片</el-button>
                <!-- 上传提示 -->
                </el-upload>
            </div>
            <el-form-item label="状态">
              <el-select style="width: 100%;" v-model="editForm.status" placeholder="状态">
                <el-option v-for="dict in statusList" :key="dict.dictValue" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
             <el-form-item label="工具名称">
              <el-input style="width: 100%;" v-model="editForm.name" placeholder="请输入工具名称"></el-input>
            </el-form-item>
            <el-form-item label="工具链接">
              <el-input style="width: 100%;" v-model="editForm.toolUrl" placeholder="请输入工具链接"></el-input>
            </el-form-item>

             <el-form-item label="工具类型">
              <el-select style="width: 100%;" v-model="editForm.type" placeholder="工具类型">
                <el-option v-for="dict in toolTypeList" :key="dict.dictValue" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>

            <!-- //描述 -->
            <el-form-item label="描述">
              <el-input type="textarea" style="width: 100%;" v-model="editForm.remark" placeholder="请输入描述"></el-input>
            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
       <el-button type="primary" @click="handleEdit">确定修改</el-button>
        <el-button @click="cancelEdit">取 消</el-button>
      </div>
    </el-dialog>

    <!-- <el-dialog title="编辑邮箱" :visible.sync="editOpen" width="600px" append-to-body>
      <el-form ref="form" :model="editForm" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="邮箱" required>
              <el-input
                v-model="editForm.recipientEmail"
                type="input"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" required>
              <el-input
                v-model="editForm.remark"
                type="textarea"
                placeholder="请输入备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit">确定修改</el-button>
        <el-button @click="cancelEdit">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

import { emailList, add, deleteList, updateList } from "@/api/useTool/index";
export default {
  name: "useTool",
  dicts: [],
  data() {
    return {
        statusList: [
            {
                label: '启用',
                value: 1
            },
            {
                label: '禁用',
                value: 0
            }
        ],
        toolTypeList: [
            {
                label: '文本',
                value: 0
            },
            {
                label: '图片',
                value: 1
            },
            {
                label: '视频',
                value: 2
            }
        ],
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/file/fileUpload',
      uploadLoading: false,
      open: false,
      editOpen: false,
      form: {},
      // 遮罩层
      loading: true,
    headers: {
        Authorization: 'Bearer ' + getToken(),
      },
      list: [],
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
      },
      editForm: {},
    };
  },
  created() {
    this.getList();
    // this.onGetDict()
  },
  methods: {
    // 编辑
    cancelEdit() {
      this.editOpen = false;
      this.editForm = {};
    },

    // 修改
    handleEdit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          updateList(this.addDateRange(this.editForm)).then((response) => {
            this.$modal.msgSuccess("编辑成功");
            this.getList();
            this.cancelEdit();
          });
        }
      });
    },
    showEdit(row) {
      this.editOpen = true;
      this.editForm = JSON.parse(JSON.stringify(row)) ;
    },

    handleBeforeUpload(file) {
      this.uploadLoading = true;  // 设置 loading 状态
      return true;  // 返回 true 允许上传
    },
    handleUploadSuccess(response, file, fileList) {
        this.$nextTick(() => {
            if(this.editOpen){
                this.editForm.imageUrl = response.data.path
            }else{
                this.form.imageUrl = response.data.path
            }
            this.uploadLoading = false;
            this.$message.success('上传成功');
            this.$refs.fileUpload.clearFiles();  // 清空上传组件的文件列表
        })
    },
    handleUploadError(error, file, fileList) {
      console.log('error3',error);
      console.log('file3',file);
      console.log('fileList3',fileList);
      this.uploadLoading = false;
      this.$message.error('上传失败,请重新上传');
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },

    // 新增
    handleAdd() {
      this.open = true;
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    onAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          add(this.addDateRange({ ...this.form })).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.getList();
            this.cancel();
          });
        }
      });
    },

    // 获取数据字典
    // onGetDict() {
    //     getDict(this.addDateRange({type:'memberBenefitType'})).then((response) => {

    //   });
    // },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      emailList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.list = response.data;
        this.loading = false;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return deleteList(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal
        .confirm("是否确认清空所有登录日志数据项？")
        .then(function () {
          return cleanLogininfor();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
  .upload-button{
    margin-bottom: 20px;
  }
</style>