<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="登记部门" prop="deptId" v-show="deptShow">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择部门" clearable>
          <el-option
            v-for="dict in allDeptList"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公文名称" prop="docName">
        <el-input
          v-model="queryParams.docName"
          placeholder="请输入公文名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公文类型" prop="docType">
        <el-select
          v-model="queryParams.docType"
          placeholder="请选择公文类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.doc_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <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-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit-outline"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehs:ehsDocument:add']"
          >公文登记
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehs:ehsDocument:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehs:ehsDocument:remove']"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehs:ehsDocument:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ehsDocumentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ Number(scope.$index) + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登记部门" align="center" prop="deptName"  v-if="this.$store.state.user.parentId==0"/>
      <el-table-column label="公文名称" align="center" prop="docName" >
        <template slot-scope="scope">
          <span
            @click="handleUpdate(scope.row)"
            style="color: blue;cursor: pointer"
            >{{ scope.row.docName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公文字号" align="center" prop="docFont" />
      <el-table-column label="拟稿部门" align="center" prop="draftDept" />
      <el-table-column label="公文类型" align="center" prop="docType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.doc_type" :value="scope.row.docType" />
        </template>
      </el-table-column>
      <el-table-column label="公文编号" align="center" prop="docNumber" />
      <el-table-column
        label="收文日期"
        align="center"
        prop="receiveTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收存情况" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['ehs:ehsDocument:edit']"
                    >修改</el-button>-->
          <el-button
            v-show="scope.row.status == 1"
            type="primary"
            plain
            size="mini"
            @click="handleLend(scope.row)"
            >借出
          </el-button>
          <el-button
            v-show="scope.row.status == 0"
            type="primary"
            plain
            size="mini"
            @click="returnDoc(scope.row)"
            >归还
          </el-button>
          <el-button
            size="mini"
            type="primary"
            @click="transferRecord(scope.row)"
            >流转记录
          </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"
    />

    <!-- 添加或修改公文管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公文名称" prop="docName">
          <el-input v-model="form.docName" placeholder="请输入公文名称" />
        </el-form-item>
        <el-form-item label="公文字号" prop="docFont">
          <el-input v-model="form.docFont" placeholder="请输入公文字号" />
        </el-form-item>
        <!--<el-form-item label="拟稿部门" prop="draftDept">
          <el-input v-model="form.draftDept" placeholder="请输入拟稿部门" />
        </el-form-item>-->
        <el-form-item label="拟稿部门" prop="draftDept">
          <el-select
            v-model="form.draftDept"
            placeholder="请选择拟稿部门"
            clearable
            style="width: 180px"
          >
            <el-option
              v-for="(item, index) in ngbmInfo"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公文类型" prop="docType">
          <el-select v-model="form.docType" placeholder="请选择公文类型">
            <el-option
              v-for="dict in dict.type.doc_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公文编号" prop="docNumber">
          <el-input v-model="form.docNumber" placeholder="请输入公文编号" />
        </el-form-item>
        <el-form-item label="收文日期" prop="receiveTime">
          <el-date-picker
            clearable
            v-model="form.receiveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择收文日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收存情况" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </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="title1"
      :visible.sync="open1"
      width="600px"
      append-to-body
    >
      <el-table v-loading="loading" :data="ehsDocTransferList">
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            <span>{{ Number(scope.$index) + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="流转人" align="center" prop="transferName" />

        <el-table-column
          label="流转时间"
          align="center"
          prop="transferTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.transferTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="流转状态"
          align="center"
          prop="docTransferStatus"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.docTransferStatus == '0'">借出</span>
            <span v-else>归还</span>
          </template>
        </el-table-column>
        <el-table-column label="流转情况" align="center" prop="transferDept">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.transfer_dept" :value="scope.row.transferDept"/>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalDetail>0"
        :total="totalDetail"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="getTransferRecord"
      />
    </el-dialog>

    <!--公文借出会话框-->
    <el-dialog
      :title="title2"
      :visible.sync="open2"
      width="600px"
      append-to-body
    >
      <el-form
        :disabled="true"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="公文名称" prop="documentName">
          <el-input v-model="form.documentName" placeholder="请输入公文名称" />
        </el-form-item>

        <el-form label-width="80px" :model="form">
          <el-form-item label="流转人" prop="transferName">
            <el-input v-model="form.transferName" placeholder="请输入流转人" />
          </el-form-item>

          <el-form-item label="流转时间" prop="transferTime">
            <el-date-picker
              clearable
              v-model="form.transferTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择流转时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="流转情况" prop="transferDept">
            <el-select v-model="form.transferDept" multiple collapse-tags placeholder="请选择流转情况"  >
              <el-option
                v-for="dict in dict.type.transfer_dept"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLendForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEhsDocument,
  getEhsDocument,
  delEhsDocument,
  addEhsDocument,
  updateEhsDocument,
  getAllDraftDept,
  getDocTransferById,
  getEhsDocumentById,
} from "@/api/ehs/ehsDocument";
import {
  addEhsDocTransfer,
  listEhsDocTransfer,
  listTransferRecord,
  lendEhsDoc,
  returnEhsDoc,
} from "@/api/ehs/ehsDocTransfer";
import { parseTime } from "../../../utils/ruoyi";
import {listAllDept } from "@/api/system/dept";
import dict from "@/utils/dict";
export default {
  name: "EhsDocument",
  dicts: ["doc_type", "doc_transfer_status","transfer_dept"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 拟稿部门下拉框
      ngbmInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //是否显示部门列表查询条件
      deptShow: false,
      //所有部门列表字典
      allDeptList: [],
      // 公文管理表格数据
      ehsDocumentList: [],
      // 弹出层标题
      title: "",
      title2: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 弹框的数据
      documentId: null,
      open1: false,
      title1: "",
      ehsDocTransferList: [],// 公文流转记录数据
      totalDetail: 0,// 弹框的总条数
      pageNum: 1,// 弹框当前页
      pageSize: 10,// 弹框的每页显示条数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        docName: null,
        docType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        docName: [
          { required: true, message: "公文名称不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.deptShow = this.$store.state.user.parentId==0 ?true :false;
    this.getList();
    // 获取拟稿部门下拉列表
    this.getAllDraftDept();
    this.getListAllDept();
  },
  methods: {
    dict,
    parseTime,
    /** 查询公文管理列表 */
    getList() {
      this.loading = true;
      listEhsDocument(this.queryParams).then((response) => {
        this.ehsDocumentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 单位信息列表 */
    getListAllDept() {
      console.log(this.$store.state.user.deptId);
      this.loading = true;
      listAllDept().then(response => {
        this.allDeptList = response.data;
        this.loading = false;
      });
    },
    /** 获取拟稿部门下拉列表 */
    getAllDraftDept() {
      getAllDraftDept().then((res) => {
        console.log(res);
        this.ngbmInfo = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        docName: null,
        docFont: null,
        draftDept: null,
        docType: null,
        docNumber: null,
        receiveTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        createUserId: null,
        updateUserId: null,
        transferDept: null,
      };
      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 id = row.id || this.ids;
      getEhsDocument(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公文";
      });
    },
    /** 借出按钮操作 */
    handleLend(row) {
      this.reset();
      const id = row.id || this.ids;
      getEhsDocumentById(id).then((response) => {
        this.form = response.data;
        this.open2 = true;
        this.title2 = "借出信息";
      });
    },
    /** 归还操作 */
    returnDoc(row) {
      const id = row.id || this.ids;
      returnEhsDoc(id).then((response) => {
        this.$modal.msgSuccess("归还成功");
        this.getList();
      });
    },
    /** 流转记录按钮操作 */
    transferRecord(row) {
      this.open1 = true;
      this.documentId = row.id;
      this.getTransferRecord();
    },
    getTransferRecord() {
      this.loading = true;
      this.title1 = "流转记录";
      listTransferRecord({"pageNum" : this.pageNum, "pageSize": this.pageSize,"documentId":this.documentId}).then(response => {
          this.ehsDocTransferList = response.rows;
          this.totalDetail = response.total;
          this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEhsDocument(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEhsDocument(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交借出信息 */
    submitLendForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form)
          if(this.form.transferDept!=null){
            this.form.transferDept = this.form.transferDept.join();
          }

          lendEhsDoc(this.form).then((response) => {
            this.$modal.msgSuccess("借出成功");
            this.open2 = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除公文管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEhsDocument(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ehs/ehsDocument/export",
        {
          ...this.queryParams,
        },
        `公文台账_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
