<!--
 * @Descripttion: 
 * @version: 
 * @Author: ysh
 * @Date: 2022-09-08 17:16:38
 * @LastEditors: wrh
 * @LastEditTime: 2024-05-10 15:14:58
-->
<template>
  <div>
    <!-- 搜索栏 -->
    <el-row>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
        <el-form-item label="存储地址" prop="storageAddress">
          <el-input v-model="queryParams.storageAddress" placeholder="请输入存储地址" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="管理员" prop="administrator">
          <el-input v-model="queryParams.administrator" placeholder="请输入管理员" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 操作栏 -->
    <el-row>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAddAStorage"
        v-hasPermi="['oa:archive:add']">新增存储地址</el-button>
      <el-button type="danger" size="small" icon="el-icon-delete" :disabled="multiple" @click="handleDeleteMany"
        v-hasPermi="['oa:archive:remove']">批量删除
      </el-button>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-table :data="storageTableData" style="width: 100%" stripe v-loading="loading"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50px" />
        <el-table-column type="index" label="序号" width="50px" align="center" />
        <el-table-column prop="id" label="存储编号" align="center" />
        <el-table-column prop="storageAddress" label="存储地址" align="center" />
        <el-table-column prop="user.nickName" label="管理员" align="center" />
        <el-table-column prop="remark" label="备注" align="center" />
        <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button type="text" icon="el-icon-edit-outline" @click="handleUpdate(scope.row)" v-hasPermi="['oa:archive:edit']">
              </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:archive:remove']">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getStorageList" />
    </el-row>
    <!-- 新增和修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%">
      <el-form :model="form" ref="formRef" label-width="105px">
        <el-form-item label="存储地址" prop="storageAddress">
          <el-input v-model="form.storageAddress" placeholder="请输入存储地址" />
        </el-form-item>
        <el-form-item label="管理人" prop="administrator">
          <el-input v-model="form.administrator" placeholder="请输入管理人" />
        </el-form-item>
        <el-form-item label="备注" prop="administrator">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :max="250" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listStorage, addStorage, updateStorage, delStorage } from "@/api/file/storage";
export default {
  nam: "Project",
  data() {
    return {
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: undefined,
        storageAddress: undefined,
        administrator: undefined,
        remark: undefined
      },
      rules: {
        storageAddress: [{ required: true, message: "存储地址不能为空", trigger: "blur" }],
        administrator: [{ required: true, message: "管理员不能为空", trigger: "blur" }],
      },
      storageTableData: [],
      loading: true,
      total: 0,
      title: '',
      open: false,
      multiple: true,
      ids: [],
    }
  },
  created() {
    this.getStorageList();
  },
  /* ----------------------------------------------------------------------------------------------- */
  /* 搜索按钮 */
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getStorageList();
    },
    /* 获取存储列表 */
    getStorageList() {
      listStorage(this.queryParams).then(res => {
        this.storageTableData = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    /* 新增存储 */
    handleAddAStorage() {
      this.open = true;
      this.title = "增加存储地址";
    },
    /* 表单确定按钮 */
    submitForm() {
      this.$refs["formRef"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateStorage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getStorageList();
            });
          } else {
            addStorage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getStorageList();
            });
          }
        }
      })
    },
    /* 表单取消按钮 */
    cancel() {
      this.reset();
      this.open = false;
    },
    /* 重置新增和修改表单 */
    reset() {
      this.form = {
        id: undefined,
        storageAddress: undefined,
        administrator: undefined,
        remark: undefined
      }
    },
    /* 修改按钮 */
    handleUpdate(row) {
      this.form = row;
      this.title = "修改存储地址"
      this.open = true;
    },
    /* 删除按钮 */
    handleDelete(row) {
      const ids = row.id;
      const storageAddress = row.storageAddress
      this.$modal.confirm('是否确认删除用户编号为"' + storageAddress + '"的数据项？').then(() => {
        return delStorage(ids);
      }).then(() => {
        this.getStorageList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /* 批量删除操作 */
    handleDeleteMany() {
      this.$modal.confirm('是否确认删除？').then(() => {
        return delStorage(this.ids);
      }).then(() => {
        this.getStorageList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
  }
}
</script>

<style lang="scss" scoped></style>