<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索工作栏 -->
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
      >
        <div class="filter-box">
          <div class="filter-lf">
            <el-form-item label="移交单号" prop="applyNo">
              <el-input
                v-model="queryParams.applyNo"
                placeholder="请输入移交单号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="移交日期" prop="turnDate">
              <el-date-picker
                v-model="queryParams.turnDate"
                style="width: 240px"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
            <el-form-item label="移交人" prop="outputName">
              <el-input
                v-model="queryParams.outputName"
                placeholder="请输入移交人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="签收人" prop="inputName">
              <el-input
                v-model="queryParams.inputName"
                placeholder="请输入签收人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="业务实体" prop="company">
              <el-input
                v-model="queryParams.company"
                placeholder="请输入业务实体"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="案卷数量" prop="amount">
              <el-input
                v-model="queryParams.amount"
                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:turn-over: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:turn-over: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="applyNo" />
        <el-table-column
          label="移交日期"
          align="center"
          prop="turnDate"
          width="180"
        >
          <template v-slot="scope">
            <span>{{ parseTime2(scope.row.turnDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="移交说明" align="center" prop="turnExplain" />
        <el-table-column label="移交人" align="center" prop="outputName" />
        <el-table-column label="签收人" align="center" prop="inputName" />
        <el-table-column label="业务实体" align="center" prop="company" />
        <el-table-column label="案卷数量" align="center" prop="amount" />
        <el-table-column label="文件名称" align="center" prop="fileName" />
        <el-table-column label="文件大小" align="center" prop="fileSize" />
        <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"
          prop="processInstanceId"
        />
        <el-table-column label="状态" align="center" prop="status">
          <template v-slot="scope">
            <dict-tag
              :type="DICT_TYPE.BS_EXPENSE_APPLY_STATUS"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="180px"
        >
          <template v-slot="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['archives:turn-over:update']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['archives:turn-over: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="1100px"
      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="filesNum">
              <el-input
                v-model="form.filesNum"
                placeholder="请选择档案"
                readonly
              >
                <el-button
                  slot="append"
                  @click="handleSelectFile"
                  icon="el-icon-search"
                  style="
                    border-color: #46a6ff;
                    background-color: #46a6ff;
                    color: white;
                  "
                  :disabled="optType == 'edit'"
                ></el-button>
              </el-input>
              <FileSelect ref="FileSelect" @onSelected="onFileSelected" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务实体" prop="company">
              <el-input
                v-model="form.company"
                placeholder="选择档案后显示"
                readonly
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="移交单号" prop="applyNo">
              <el-input
                v-model="form.applyNo"
                placeholder="请输入移交单号"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="移交日期" prop="turnDate">
              <el-date-picker
                clearable
                v-model="form.turnDate"
                type="date"
                value-format="timestamp"
                placeholder="选择移交日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="移交说明" prop="turnExplain">
              <el-input
                v-model="form.turnExplain"
                placeholder="请输入移交说明"
              />
            </el-form-item> </el-col
        ></el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="移交人" prop="outputName">
              <el-input
                v-model="form.outputName"
                placeholder="请选择移交人"
                readonly
              >
                <el-button
                  slot="append"
                  @click="handleSelectUser1"
                  icon="el-icon-search"
                  style="
                    border-color: #46a6ff;
                    background-color: #46a6ff;
                    color: white;
                  "
                ></el-button>
              </el-input>
              <UserSelect ref="UserSelect1" @onSelected="onUserSelected1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="签收人" prop="inputName">
              <el-input
                v-model="form.inputName"
                placeholder="请选择签收人"
                readonly
              >
                <el-button
                  slot="append"
                  @click="handleSelectUser2"
                  icon="el-icon-search"
                  style="
                    border-color: #46a6ff;
                    background-color: #46a6ff;
                    color: white;
                  "
                ></el-button>
              </el-input>
              <UserSelect ref="UserSelect2" @onSelected="onUserSelected2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="旧档案位置" prop="position">
              <!-- <el-cascader
                v-model="oldSiteArray"
                :options="siteOptions"
                :props="propName"
                placeholder="请选择档案位置"
                clearable
                @change="handleOldSiteChanged"
              /> -->
              <el-input
                v-model="form.position"
                placeholder="选择档案后显示"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="新档案位置">
              <el-cascader
                v-model="newSiteArray"
                :options="siteOptions"
                :props="propName"
                placeholder="请选择档案位置"
                clearable
                @change="handleNewSiteChanged"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="案卷数量" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入案卷数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="流程实例的编号" prop="processInstanceId">
              <el-input v-model="form.processInstanceId" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申请状态" prop="status">
              <el-input v-model="form.status" readonly />
            </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 {
  createTurnOver,
  updateTurnOver,
  deleteTurnOver,
  getTurnOver,
  getTurnOverPage,
  exportTurnOverExcel,
} from "@/api/archives/turnOver";
import { createWS } from "@/api/archives/record";
import { getSiteTree } from "@/api/setting/organization";
import FileSelect from "@/components/FileSelect/single.vue";
import UserSelect from "@/components/UserSelect/single.vue";

export default {
  name: "TurnOver",
  components: { FileSelect, UserSelect },
  data() {
    return {
      propName: {
        label: "pName",
        value: "pId",
        multiple: false,
      },
      optType: undefined,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 移交列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        applyNo: null,
        turnDate: [],
        turnExplain: null,
        outputName: null,
        outputNameId: null,
        inputName: null,
        inputNameId: null,
        userId: null,
        deptId: null,
        deptName: null,
        companyId: null,
        company: null,
        remark: null,
        createBy: null,
        fromOrganizationId: null,
        fromDepotId: null,
        fromCabinetId: null,
        toOrganizationId: null,
        toDepotId: null,
        toCabinetId: null,
        createById: null,
        amount: null,
        createTime: [],
        processInstanceId: null,
        billType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      organizationList: [], //业务实体
      userOptions: [], //用户数据
      siteOptions: [], //位置数据
      oldSiteArray: [], //旧位置
      newSiteArray: [], //新位置
      company: null,
      outputName: null,
      inputName: null,
      id: null, //档案历史记录ID
      authRecordId: null, //档案历史记录ID
    };
  },
  created() {
    this.getList();
    this.getUserList();
    this.getSiteList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getTurnOverPage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 生成移交单号
    createWS(val) {
      const data = {
        id: val,
      };
      createWS(data).then((response) => {
        this.form.applyNo = response.data;
      });
    },
    //获取归档位置
    getSiteList() {
      getSiteTree().then((response) => {
        this.siteOptions = response.data;
        this.siteOptions.map((w) => {
          w.children.map((l) => {
            let lstr = JSON.stringify(l.children)
              .replace(/depotId/g, "lId")
              .replace(/cabinetId/g, "pId")
              .replace(/cabinetName/g, "pName");
            l.children = JSON.parse(lstr);
          });

          let wstr = JSON.stringify(w.children)
            .replace(/organizationId/g, "wId")
            .replace(/depotId/g, "pId")
            .replace(/depotName/g, "pName");
          w.children = JSON.parse(wstr);
        });
        let ostr = JSON.stringify(this.siteOptions)
          .replace(/organizationId/g, "pId")
          .replace(/organizationName/g, "pName");
        this.siteOptions = JSON.parse(ostr);
        console.log(this.siteOptions);
      });
    },
    // //选择旧位置
    // handleOldSiteChanged(obj) {
    //   if (obj != null) {
    //     this.form.fromOrganizationId = obj[0];
    //     this.form.fromDepotId = obj[1];
    //     this.form.fromCabinetId = obj[2];
    //   }
    // },
    //选择新位置
    handleNewSiteChanged(obj) {
      if (obj != null) {
        this.form.toOrganizationId = obj[0];
        this.form.toDepotId = obj[1];
        this.form.toCabinetId = obj[2];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        applyNo: undefined,
        turnDate: undefined,
        turnExplain: undefined,
        outputName: undefined,
        outputNameId: undefined,
        inputName: undefined,
        inputNameId: undefined,
        userId: undefined,
        deptId: undefined,
        deptName: undefined,
        companyId: undefined,
        company: undefined,
        remark: undefined,
        createBy: undefined,
        fromOrganizationId: undefined,
        fromDepotId: undefined,
        fromCabinetId: undefined,
        toOrganizationId: undefined,
        toDepotId: undefined,
        toCabinetId: undefined,
        createById: undefined,
        amount: undefined,
        processInstanceId: undefined,
        billType: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.optType = "add";
      this.title = "添加移交";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.optType = "edit";
      const id = row.id;
      getTurnOver(id).then((response) => {
        this.form = response.data;
        this.company = response.data.company;
        this.outputName = response.data.outputName;
        this.inputName = response.data.inputName;
        this.open = true;
        this.title = "修改移交";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateTurnOver(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createTurnOver(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除移交编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteTurnOver(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 exportTurnOverExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "移交.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    //选择档案
    handleSelectFile() {
      this.$refs.FileSelect.showFlag = true;
    },
    onFileSelected(obj) {
      console.log(obj);
      if (obj != undefined && obj != null) {
        this.form.company = obj.company;
        this.form.position = obj.position;
        this.form.filesNum = obj.filesNum;
        this.form.authRecordId = obj.id;
        this.createWS(obj.id);
      }
    },
    // 人员选择弹窗
    handleSelectUser1() {
      this.$refs.UserSelect1.showFlag = true;
    },
    // 移交人选择
    onUserSelected1(obj) {
      console.log(obj);
      if (obj != undefined && obj != null) {
        this.form.outputName = obj.username;
      }
    },
    // 人员选择弹窗
    handleSelectUser2() {
      this.$refs.UserSelect2.showFlag = true;
    },
    // 签收人选择
    onUserSelected2(obj) {
      console.log(obj);
      if (obj != undefined && obj != null) {
        this.form.inputName = obj.username;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/header/index.scss";
@import "@/assets/styles/common/index.scss";
</style>
