<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索工作栏 -->
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="108px"
      >
        <div class="filter-box">
          <div class="filter-lf">
            <el-form-item label="借阅申请编码" prop="borrowCode">
              <el-input
                v-model="queryParams.borrowCode"
                placeholder="请输入借阅申请编码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="借阅人" prop="userName">
              <el-input
                v-model="queryParams.userName"
                placeholder="请输入借阅人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </div>
        </div>
      </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"
            v-hasPermi="['archives:borrowAapply:create']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['archives:borrowAapply:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <el-table v-loading="loading" :data="list" stripe border>
        <el-table-column
          label="借阅申请编码"
          align="center"
          prop="borrowCode"
        />
        <el-table-column label="借阅人名称" align="center" prop="userName" />
        <el-table-column label="借阅部门名称" align="center" prop="deptName" />
        <el-table-column label="借阅事由" align="center" prop="reason" />
        <el-table-column
          label="预计归还日期"
          align="center"
          prop="returnTime"
          width="180"
        >
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.returnTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="借阅方式" align="center" prop="way">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.ARCHIVES_APPLY" :value="scope.row.way" />
          </template>
        </el-table-column>
        <el-table-column label="档案介质" align="center" prop="media">
          <template v-slot="scope">
            <dict-tag
              :type="DICT_TYPE.ARCHIVES_MEDIA"
              :value="scope.row.media"
            />
          </template>
        </el-table-column>
        <el-table-column label="借阅描述" align="center" prop="describes" />
        <el-table-column
          label="创建日期"
          align="center"
          prop="createTime"
          width="180"
        >
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template v-slot="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handlePass(scope.row)"
              v-hasPermi="['archives:borrowAapply:update']"
              >通过</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleUnpass(scope.row)"
              v-hasPermi="['archives:borrowAapply:update']"
              >驳回</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.borrowState == null"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['archives:borrowAapply:update']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['archives:borrowAapply:delete']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="借阅申请编码" prop="borrowCode">
              <el-input
                v-model="form.borrowCode"
                placeholder="请输入借阅申请编码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="借阅人" prop="userName">
              <el-input
                v-model="form.userName"
                placeholder="请选择借阅人"
                readonly
              >
                <el-button
                  slot="append"
                  @click="handleSelectUser"
                  icon="el-icon-search"
                  style="
                    border-color: #46a6ff;
                    background-color: #46a6ff;
                    color: white;
                  "
                ></el-button>
              </el-input>
              <UserSelect ref="UserSelect" @onSelected="onUserSelected" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="借阅部门名称" prop="deptName">
              <el-input
                v-model="form.deptName"
                placeholder="请输入借阅部门名称"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="借阅事由" prop="reason">
              <el-input
                v-model="form.reason"
                placeholder="请输入借阅事由"
              /> </el-form-item
          ></el-col>
          <el-col :span="8">
            <el-form-item label="预计归还日期" prop="returnTime">
              <el-date-picker
                clearable
                v-model="form.returnTime"
                type="date"
                value-format="timestamp"
                placeholder="选择预计归还日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="借阅方式" prop="way">
              <el-select v-model="form.way" placeholder="请选择借阅方式">
                <el-option
                  v-for="item in wayOptions"
                  :key="item.id"
                  :value="item.value"
                  :label="item.label"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="档案介质" prop="media">
              <el-select v-model="form.media" placeholder="请选择档案介质">
                <el-option
                  v-for="item in mediaOptions"
                  :key="item.id"
                  :value="item.value"
                  :label="item.label"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="借阅档案案卷号" prop="recordId">
              <el-select v-model="form.recordId" placeholder="请选择借阅档案">
                <el-option
                  v-for="item in filesNumOptions"
                  :key="item.id"
                  :value="item.id"
                  :label="item.filesNum"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="optType == 'edit'">
            <el-form-item label="档案类型" prop="recordId">
              <el-select v-model="form.fileType" placeholder="" clearable>
                <el-option
                  v-for="item in fileTypeOptions"
                  :key="item.id"
                  :value="item.value"
                  :label="item.label"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" v-if="optType == 'edit'">
            <el-form-item label="位置" prop="site">
              <el-input v-model="form.site" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="借阅描述" prop="describes">
              <el-input v-model="form.describes" placeholder="请输入借阅描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </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 {
  createBorrowApply,
  updateBorrowApply,
  deleteBorrowApply,
  getBorrowApply,
  getBorrowApplyPage,
  exportBorrowApplyExcel,
} from "@/api/borrow/apply";
import { listData } from "@/api/system/dict/data";
import { getRecordPage } from "@/api/archives/record";
import UserSelect from "@/components/UserSelect/single.vue";

export default {
  name: "BorrowApply",
  components: { UserSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案借阅申请列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        borrowCode: null,
        borrowState: null,
        reason: null,
        describe: null,
        userId: null,
        userName: null,
        deptId: null,
        deptName: null,
        returnTime: [],
        way: null,
        media: null,
        companyId: null,
        company: null,
        remark: null,
        createTime: [],
        historyOrList: "List",
      },
      optType: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      borrowStateOptions: [],
      wayOptions: [],
      mediaOptions: [],
      filesNumOptions: [], //借阅档案数据
      fileTypeOptions: [], //档案类型
    };
  },
  created() {
    this.getList();
    this.handleInitList();
    this.getFilesNum();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getBorrowApplyPage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 获取借阅档案
    getFilesNum() {
      getRecordPage().then((response) => {
        this.filesNumOptions = response.data.list;
      });
    },
    //获取字典数据
    handleInitList() {
      // 获取借阅申请状态
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "borrow_state",
      }).then((res) => {
        this.borrowStateOptions = res.data.list || [];
      });
      // 获取借阅申请方式
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "way_type",
      }).then((res) => {
        this.wayOptions = res.data.list || [];
      });
      // 获取借阅申请介质类型
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "media_type",
      }).then((res) => {
        this.mediaOptions = res.data.list || [];
      });
      // 获取档案类型
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "archives_file_type",
      }).then((res) => {
        this.fileTypeOptions = res.data.list || [];
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        borrowCode: undefined,
        borrowState: undefined,
        reason: undefined,
        describes: undefined,
        userId: undefined,
        userName: undefined,
        deptId: undefined,
        deptName: undefined,
        returnTime: undefined,
        way: undefined,
        media: undefined,
        companyId: undefined,
        company: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.optType = "add";
      this.open = true;
      this.title = "添加档案借阅申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.optType = "edit";
      const id = row.id;
      getBorrowApply(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案借阅申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateBorrowApply(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createBorrowApply(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    // 通过操作事件
    handlePass(row) {
      const data = { id: row.id, borrowState: "0" };
      updateBorrowApply(data).then((response) => {
        this.getList();
        this.$modal.msgSuccess("已通过");
      });
    },
    // 不通过操作事件
    handleUnpass(row) {
      const data = { id: row.id, borrowState: "1" };
      updateBorrowApply(data).then((response) => {
        this.getList();
        this.$modal.msgError("已驳回");
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除档案借阅申请编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteBorrowApply(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有档案借阅申请数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportBorrowApplyExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "档案借阅申请.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    // 人员选择弹窗
    handleSelectUser() {
      this.$refs.UserSelect.showFlag = true;
    },
    // 借阅人选择
    onUserSelected(obj) {
      console.log(obj);
      if (obj != undefined && obj != null) {
        this.form.userName = obj.username;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/header/index.scss";
@import "@/assets/styles/common/index.scss";
</style>
