<template>
  <div>
    <!-- 表单查询项 -->
    <div class="headerbox">
      <el-form ref="queryForm" :model="queryParams" size="small" label-width="100px">
        <el-row>
          <el-col :span="5">
            <el-form-item label="文件名称" prop="name" style="width: 100%;">
              <el-input v-model="queryParams.name" placeholder="请输入文件名称" :fetch-suggestions="querySearchAsync"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="文件类型" prop="lx">
              <el-select v-model="queryParams.lx" placeholder="请选择文件类型">
                <el-option v-for="dict in dict.type.wjlx" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <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-col>
        </el-row>
      </el-form>
    </div>
    <!-- 表格内容区 -->
    <div class="tablebox">
      <!-- 导出行 -->
      <div class="tablebtntwo">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
        <el-table-column label="文件名称" align="center" prop="name" />
        <el-table-column label="类型" align="center">
          <template slot-scope="scope">
            {{ fileTypeMap[scope.row.lx] || scope.row.lx }}
          </template>
        </el-table-column>
        <el-table-column label="附件" align="center" prop="fj" />
        <el-table-column label="上传用户" align="center" prop="scyh" />
        <el-table-column label="上传时间" align="center" prop="scsj" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text"  @click="getInfo(scope.row)"
              v-hasPermi="['system:post:detail']">详情</el-button>
            <el-button size="mini" type="text"  @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']">编辑</el-button>
            <el-button size="mini" type="text"  @click="handleDelete(scope.row)" style="color: red;"
              v-hasPermi="['system:post: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" />
    </div>

    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40rem" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="7rem">
        <el-form-item label="文件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="类型" prop="lx">
          <el-select v-model="form.lx" placeholder="请选择文件类型" style="width: 100%;">
            <el-option v-for="dict in dict.type.wjlx" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传用户" prop="scyh">
          <el-input v-model="form.scyh" placeholder="请输入上传用户" />
        </el-form-item>
        <el-form-item label="上传时间" prop="scsj">
          <el-date-picker v-model="form.scsj" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
            style="width: 100%;" />
        </el-form-item>
        <el-form-item label="附件" prop="fj">
          <fileload v-model="form.fj"></fileload>
        </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>

    <!-- 详情项目对话框 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="40rem" append-to-body>
      <el-form ref="detailForm" :model="detailForm" label-width="7rem" :disabled="true">
        <el-form-item label="文件名称" prop="name">
          <el-input v-model="detailForm.name" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="类型" prop="lx">
          <el-select v-model="detailForm.lx" placeholder="请选择文件类型" style="width: 100%;">
            <el-option v-for="dict in dict.type.wjlx" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传用户" prop="scyh">
          <el-input v-model="detailForm.scyh" placeholder="请输入上传用户" />
        </el-form-item>
        <el-form-item label="上传时间" prop="scsj">
          <el-date-picker v-model="detailForm.scsj" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
            style="width: 100%;" />
        </el-form-item>
        <el-form-item label="附件" prop="fj">
          <el-input v-model="detailForm.fj" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">取消</el-button>
        <el-button @click="detailOpen = false">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getXmzskPage, addXmzsk, updateXmzsk, deleteXmzsk, getXmzskById } from "@/api/ManageApi/index";
import fileload from "@/components/FileUpload/index.vue"

export default {
  dicts: ['wjlx'],
  name: "Xmzsk",
  components: {
    fileload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 目录表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示添加或修改弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        lx: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        name: undefined,
        lx: undefined,
        fj: undefined,
        scsj: undefined,
        scyh: undefined,
        createBy: undefined,
        createId: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateId: undefined,
        updateTime: undefined
      },
      // 详情表单参数
      detailForm: {
        id: undefined,
        name: undefined,
        lx: undefined,
        fj: undefined,
        scsj: undefined,
        scyh: undefined,
        createBy: undefined,
        createId: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateId: undefined,
        updateTime: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "文件名称不能为空", trigger: "blur" }
        ],
        lx: [
          { required: true, message: "文件类型不能为空", trigger: "change" }
        ],
        scyh: [
          { required: true, message: "上传用户不能为空", trigger: "blur" }
        ],
        scsj: [
          { required: true, message: "上传时间不能为空", trigger: "change" }
        ]
      },
      // 文件类型映射
      fileTypeMap: {
        1: "政策文件",
        2: "研究报告",
        3: "网络信息",
        4: "其他",
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    querySearchAsync(queryString, cb) {
      let results = [];
      // 假设 getXmzskPage 是用于文件名称模糊查询的 API
      getXmzskPage({ name: queryString }).then(res => {
        res.data.records.forEach(item => {
          results.push({
            value: item.name,
            id: item.id,
          });
        });
      });
      cb(results);
    },
    // 查询目录列表
    getList() {
      this.loading = true;
      getXmzskPage(this.queryParams).then(response => {
        this.postList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      }).catch(error => {
        console.error('API Request Error:', error);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        lx: undefined,
        fj: undefined,
        scsj: undefined,
        scyh: undefined,
        createBy: undefined,
        createId: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateId: undefined,
        updateTime: undefined
      };
      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() {
      this.reset();
      this.open = true;
      this.title = "新增项目知识库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const catalogId = row.id;
      this.form = { ...row };
      this.open = true;
      this.title = "修改项目知识库";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateXmzsk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              console.error('API Request Error:', error);
            });
          } else {
            addXmzsk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              console.error('API Request Error:', error);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const catalogIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目知识库编号为"' + catalogIds + '"的数据项？').then(() => {
        return deleteXmzsk([catalogIds]);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 详情按钮操作 */
    getInfo(row) {
      const catalogId = row.id;
      getXmzskById(catalogId).then(response => {
        this.detailForm = { ...response.data };
        this.detailOpen = true;
        this.title = "项目知识库详情";
      }).catch(error => {
        console.error('API Request Error:', error);
      });
    }
  }
};
</script>

<style scoped>
.headerbox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablebox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablehead {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.headbtn {
  display: flex;
}

.tablebtntwo {
  margin-top: 1rem;
  margin-bottom: 1rem;
}
</style>