<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类" prop="secrecyType">
        <el-select v-model="queryParams.secrecyType" placeholder="请选择分类" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种类" prop="kind">
        <el-input
          v-model="queryParams.kind"
          placeholder="请输入种类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密级" prop="confidential">
        <el-input
          v-model="queryParams.confidential"
          placeholder="请输入密级"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人id" prop="peopleId">
        <el-input
          v-model="queryParams.peopleId"
          placeholder="请输入责任人id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用人id" prop="peopleId1">
        <el-input
          v-model="queryParams.peopleId1"
          placeholder="请输入使用人id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批准人id" prop="peopleId2">
        <el-input
          v-model="queryParams.peopleId2"
          placeholder="请输入批准人id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起用时间" prop="startTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择起用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="停用时间" prop="endTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择停用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="移交时间" prop="hindOverTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.hindOverTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择移交时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单位" prop="unitId">
        <el-input
          v-model="queryParams.unitId"
          placeholder="请输入单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人姓名" prop="peopleName">
        <el-select v-model="queryParams.peopleName" placeholder="请选择责任人姓名" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="使用人姓名" prop="people1Name">
        <el-select v-model="queryParams.people1Name" placeholder="请选择使用人姓名" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="批准人姓名" prop="people2Name">
        <el-select v-model="queryParams.people2Name" placeholder="请选择批准人姓名" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-select v-model="queryParams.deptName" placeholder="请选择部门名称" clearable size="small">
          <el-option label="请选择字典生成" 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-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bdglSecret:secret: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="['bdglSecret:secret: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="['bdglSecret:secret:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['bdglSecret:secret:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="secretList" @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" prop="secrecyType" />
      <el-table-column label="编号" align="center" prop="number" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="种类" align="center" prop="kind">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.electronic " :value="scope.row.kind"/>
        </template>
      </el-table-column>
      <el-table-column label="型号" align="center" prop="version" />
      <el-table-column label="密级" align="center" prop="confidential" />
      <el-table-column label="责任人id" align="center" prop="peopleId" />
      <el-table-column label="使用人id" align="center" prop="peopleId1" />
      <el-table-column label="批准人id" align="center" prop="peopleId2" />
      <el-table-column label="起用时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停用时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="移交时间" align="center" prop="hindOverTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hindOverTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unitId" />
      <el-table-column label="责任人姓名" align="center" prop="peopleName" />
      <el-table-column label="使用人姓名" align="center" prop="people1Name" />
      <el-table-column label="批准人姓名" align="center" prop="people2Name" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <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="['bdglSecret:secret:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bdglSecret:secret: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"
    />

    <!-- 添加或修改保密设备外借记录对话框 -->
    <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="secrecyType">
          <el-select v-model="form.secrecyType" placeholder="请选择分类">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="种类" prop="kind">
          <el-input v-model="form.kind" placeholder="请输入种类" />
        </el-form-item>
        <el-form-item label="型号" prop="version">
          <el-input v-model="form.version" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="密级" prop="confidential">
          <el-input v-model="form.confidential" placeholder="请输入密级" />
        </el-form-item>
        <el-form-item label="责任人id" prop="peopleId">
          <el-input v-model="form.peopleId" placeholder="请输入责任人id" />
        </el-form-item>
        <el-form-item label="使用人id" prop="peopleId1">
          <el-input v-model="form.peopleId1" placeholder="请输入使用人id" />
        </el-form-item>
        <el-form-item label="批准人id" prop="peopleId2">
          <el-input v-model="form.peopleId2" placeholder="请输入批准人id" />
        </el-form-item>
        <el-form-item label="起用时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择起用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停用时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择停用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="移交时间" prop="hindOverTime">
          <el-date-picker clearable size="small"
            v-model="form.hindOverTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择移交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单位" prop="unitId">
          <el-input v-model="form.unitId" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="责任人姓名" prop="peopleName">
          <el-select v-model="form.peopleName" placeholder="请选择责任人姓名">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用人姓名" prop="people1Name">
          <el-select v-model="form.people1Name" placeholder="请选择使用人姓名">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="批准人姓名" prop="people2Name">
          <el-select v-model="form.people2Name" placeholder="请选择批准人姓名">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-select v-model="form.deptName" placeholder="请选择部门名称">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </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>
  </div>
</template>

<script>
import { listSecret, getSecret, delSecret, addSecret, updateSecret, exportSecret } from "@/api/bdglSecret/secret";

export default {
  name: "Secret",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保密设备外借记录表格数据
      secretList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        secrecyType: null,
        number: null,
        name: null,
        kind: null,
        version: null,
        confidential: null,
        peopleId: null,
        peopleId1: null,
        peopleId2: null,
        startTime: null,
        endTime: null,
        hindOverTime: null,
        unitId: null,
        peopleName: null,
        people1Name: null,
        people2Name: null,
        deptName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保密设备外借记录列表 */
    getList() {
      this.loading = true;
      listSecret(this.queryParams).then(response => {
        this.secretList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        secrecyType: null,
        number: null,
        name: null,
        kind: null,
        version: null,
        confidential: null,
        peopleId: null,
        peopleId1: null,
        peopleId2: null,
        startTime: null,
        endTime: null,
        hindOverTime: null,
        unitId: null,
        peopleName: null,
        people1Name: null,
        people2Name: null,
        deptName: 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
      getSecret(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保密设备外借记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSecret(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSecret(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除保密设备外借记录编号为"' + ids + '"的数据项？').then(function() {
        return delSecret(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有保密设备外借记录数据项？').then(() => {
        this.exportLoading = true;
        return exportSecret(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
