<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索工作栏 -->
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <div class="filter-box">
          <div class="filter-lf">
            <el-form-item label="档案类型" prop="fileType">
              <el-select
                v-model="queryParams.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-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"
            :disabled="multiple"
            @click="handleAuth"
            >鉴定</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <el-table
        v-loading="loading"
        :data="list"
        stripe
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="业务实体" align="center" prop="company" />
        <el-table-column label="档案类型" align="center" prop="fileType">
          <template v-slot="scope">
            <dict-tag
              :type="DICT_TYPE.ARCHIVES_FILE_TYPE"
              :value="scope.row.fileType"
            />
          </template>
        </el-table-column>
        <el-table-column label="档案位置" align="center" prop="site" />
        <el-table-column label="全宗号" align="center" prop="caseNum" />
        <el-table-column label="目录号" align="center" prop="catalogNum" />
        <el-table-column label="案卷号" align="center" prop="filesNum" />
        <el-table-column label="归档时间" align="center" prop="recordTime" />
        <el-table-column label="备注" align="center" prop="remark" />
      </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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-row>
          <el-col :span="14">
            <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-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="鉴定结果" prop="result">
              <el-radio v-model="form.result" label="0">销毁</el-radio>
              <el-radio v-model="form.result" label="1">延长期限</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.result == 1">
          <el-col :span="12">
            <el-form-item label="延长期限" prop="timeLimit">
              <el-select v-model="form.timeLimit" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <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>
  </div>
</template>

<script>
import { listData } from "@/api/system/dict/data";
import { createAuthenticateApply } from "@/api/fileAuth/authenticate";
import { getRecordPage } from "@/api/archives/record";
import UserSelect from "@/components/UserSelect/single.vue";

export default {
  name: "FileAuth",
  components: { UserSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非多个禁用
      multiple: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 归档列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        authenticate: "List",
        fileType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      fileTypeOptions: [], //档案类型
      fileStatusOptions: [], //归档状态
      catalogNumOptions: [], //目录号
      caseNumOptions: [], //全宗号
      userName: null,
      options: [
        {
          value: "10",
          label: "10年",
        },
        {
          value: "20",
          label: "20年",
        },
        {
          value: "30",
          label: "30年",
        },
        {
          value: "永久保存",
          label: "永久保存",
        },
      ],
      selectList: [],
    };
  },
  created() {
    this.getList();
    this.handleInitList();
    this.getUserList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getRecordPage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectList = selection.map((item) => {
        return {
          recordId: item["id"],
        };
      });
      console.log(this.selectList);

      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    //获取字典数据
    handleInitList() {
      // 获取档案类型
      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 = {
        userId: undefined,
        userName: undefined,
        timeLimit: undefined,
        result: undefined,
        remark: undefined,
      };
      this.userName = undefined;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 鉴定操作事件
    handleAuth() {
      this.reset();
      this.open = true;
      this.title = "鉴定";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.selectList.forEach((item) => {
          item.userId = this.form.userId;
          item.userName = this.form.userName;
          item.timeLimit = this.form.timeLimit;
          item.result = this.form.result;
          item.remark = this.form.remark;
        });
        console.log(this.selectList);
        createAuthenticateApply(this.selectList).then((response) => {
          this.$modal.msgSuccess("鉴定完成");
          this.open = false;
          this.getList();
        });
        return;
      });
    },
    // 人员选择弹窗
    handleSelectUser() {
      this.$refs.UserSelect.showFlag = true;
    },
    // 鉴定人选择
    onUserSelected(obj) {
      console.log(obj);
      if (obj != undefined && obj != null) {
        this.form.userName = obj.username;
        this.form.userId = obj.id;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.due_month .el-date-picker__header {
  display: none;
}
@import "@/assets/styles/header/index.scss";
@import "@/assets/styles/common/index.scss";
</style>
