<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <!-- <el-form-item label="会计年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入会计年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会计月份" prop="period">
        <el-input
          v-model="queryParams.period"
          placeholder="请输入会计月份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="起止凭证号" prop="vouNum">
        <el-input
          v-model="queryParams.vouNum"
          placeholder="请输入起止凭证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          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>
        <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>
    </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:record: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:record:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-s-claim"
          size="mini"
          :disabled="multiple"
          @click="handleArchive"
          v-hasPermi="['setting:organization:create']"
          >归档</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      @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="frequency" />
      <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="year" />
      <el-table-column label="会计月份" align="center" prop="period" />
      <el-table-column label="起止凭证号" align="center" prop="vouNum" />
      <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="site" />
      <el-table-column label="关联号" align="center" prop="flowId" />
      <el-table-column
        label="创建日期"
        align="center"
        prop="createTime"
        width="160"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="归档详情" align="center" prop="note" />
      <el-table-column label="归档状态" align="center" prop="fileStatus">
        <template v-slot="scope">
          <dict-tag
            :type="DICT_TYPE.ARCHIVES_FILE_STATUS"
            :value="scope.row.fileStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="借阅状态" align="center" prop="borrowStatus" />
      <el-table-column label="鉴定状态" align="center" prop="identifyStatus" />
      <el-table-column label="归档时间" align="center" prop="recordTime" />
      <!-- <el-table-column label="所属部门" align="center" prop="deptName" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archives:record:update']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archives:record: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-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="业务实体" prop="company">
              <!-- <el-cascader
                v-model="companyArray"
                :options="companyOptions"
                :props="propName"
                placeholder="请选择业务实体"
                clearable
                @change="handleChange"
              /> -->
              <el-select
                v-model="form.company"
                placeholder="请选择业务实体"
                @change="handleCompanyChange"
              >
                <el-option
                  v-for="item in organizationList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.organizationName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="归档频次" prop="frequency">
              <el-input v-model="form.frequency" placeholder="请输入归档频次" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="档案类型" prop="company">
              <el-select v-model="form.fileType" placeholder="请选择档案类型">
                <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">
            <el-form-item label="会计年份" prop="year">
              <el-date-picker
                format="yyyy年"
                v-model="form.year"
                value-format="yyyy"
                type="year"
                placeholder="请选择会计年份"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="会计月份" prop="period">
              <el-date-picker
                popper-class="due_month"
                v-model="form.period"
                format="M月"
                value-format="M"
                type="month"
                placeholder="请选择会计月份"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="起止凭证号" prop="vouNum">
              <el-input v-model="form.vouNum" placeholder="请输入起止凭证号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="全宗号" prop="caseNum">
              <el-input
                v-model="form.caseNum"
                placeholder="选择业务实体后自动带入"
                readonly
                @change="changeCaseNum"
              />
              <!-- <el-select
                v-model="form.caseNum"
                placeholder="请选择全宗号"
                @change="changeCaseNum"
              >
                <el-option
                  v-for="item in caseNumOptions"
                  :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="catalogNum">
              <el-select
                v-model="form.catalogNum"
                placeholder="请选择目录号"
                @change="changeCatalogNum"
              >
                <el-option
                  v-for="item in catalogNumOptions"
                  :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="filesNum">
              <el-input v-model="form.filesNum" readonly placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="档案位置" prop="site">
              <el-cascader
                v-model="siteArray"
                :options="siteOptions"
                :props="propName1"
                placeholder="请选择档案位置"
                clearable
                @change="handleSiteChanged"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联号" prop="flowId">
              <el-input v-model="form.flowId" placeholder="请输入关联号" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="1">请选择字典生成</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="归档详情" prop="note">
              <el-input v-model="form.note" placeholder="请输入归档详情" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="归档时间" prop="recordTime">
              <el-date-picker
                clearable
                v-model="form.recordTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择归档时间"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="借阅状态" prop="borrowStatus">
              <el-radio-group v-model="form.borrowStatus">
                <el-radio label="1">请选择字典生成</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <!-- <el-col :span="8">
            <el-form-item label="鉴定状态" prop="identifyStatus">
              <el-radio-group v-model="form.identifyStatus">
                <el-radio label="1">请选择字典生成</el-radio>
              </el-radio-group>
            </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 {
  createRecord,
  updateRecord,
  deleteRecord,
  getRecord,
  getRecordPage,
  exportRecordExcel,
  createCodeRule,
  archivesRecord,
} from "@/api/archives/record";
// import { listDept } from "@/api/system/dept";
import { listData } from "@/api/system/dict/data";
import { getSiteTree } from "@/api/setting/organization";
import { getOrganizationPage } from "@/api/setting/organization";

export default {
  name: "Record",
  components: {},
  data() {
    return {
      propName: {
        checkStrictly: true,
        label: "name",
        value: "id",
        multiple: false,
      },
      propName1: {
        label: "pName",
        value: "pId",
        multiple: false,
      },
      companyOptions: [], //业务实体数据
      companyArray: [], //业务实体选中值
      siteOptions: [], //归档位置数据
      siteArray: [], //归档位置选中值
      // 遮罩层
      loading: true,
      // 非多个禁用
      multiple: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 归档列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        companyId: null,
        company: null,
        frequency: null,
        fileType: null,
        year: null,
        period: null,
        vouNum: null,
        caseNum: null,
        catalogNum: null,
        filesNum: null,
        site: null,
        flowId: null,
        createTime: [],
        status: null,
        note: null,
        fileStatus: null,
        borrowStatus: null,
        identifyStatus: null,
        remark: null,
        recordTime: [],
        deptId: null,
        deptName: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        userId: null,
        organizationId: null, //立档单位
        depotId: null, //库房
        cabinetId: null, //档案柜
      },
      // 表单参数
      form: {},
      Fileform: {},
      // 选中数组
      ids: [],
      // 表单校验
      rules: {},
      fileTypeOptions: [], //档案类型
      fileStatusOptions: [], //归档状态
      catalogNumOptions: [], //目录号
      caseNumOptions: [], //全宗号
      organizationList: [],
    };
  },
  created() {
    this.getList();
    this.getOrganizationList();
    this.getSiteList();
    this.handleInitList();
  },
  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.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    // //获取业务实体
    // getCompanyList() {
    //   listDept().then((response) => {
    //     this.companyOptions = this.handleTree(response.data, "id", "parentId");
    //     console.log(this.companyOptions);
    //   });
    // },
    // handleChange(value) {
    //   console.log(value, value[value.length - 1]);
    //   this.form.companyId = value[value.length - 1]; //id处理
    // },
    // 获取立档单位
    getOrganizationList() {
      getOrganizationPage().then((response) => {
        this.organizationList = response.data.list;
      });
    },
    handleCompanyChange(val) {
      var data = this.organizationList.find((item) => item.id == val);
      console.log(data);
      this.form.caseNum = data.caseNum;
      this.Fileform.caseNum = data.caseNum;
    },

    //获取字典数据
    handleInitList() {
      // 获取档案类型
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "archives_file_type",
      }).then((res) => {
        this.fileTypeOptions = res.data.list || [];
      });
      // 获取全宗号
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "archives_case_num",
      }).then((res) => {
        this.caseNumOptions = res.data.list || [];
      });
      // 获取目录号
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "archives_catalog_num",
      }).then((res) => {
        this.catalogNumOptions = res.data.list || [];
      });
      // 获取归档状态
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "archives_file_status",
      }).then((res) => {
        this.fileStatusOptions = res.data.list || [];
      });
    },
    // 全宗号修改事件
    changeCaseNum() {
      if (this.Fileform.catalogNum != null && this.Fileform.caseNum != null) {
        this.creatFilesNum();
      }
    },
    // 目录号修改事件
    changeCatalogNum(val) {
      this.Fileform.catalogNum = val;
      console.log(this.Fileform);
      if (this.Fileform.catalogNum != null && this.Fileform.caseNum != null) {
        this.creatFilesNum();
      }
    },
    // 获取案卷号
    creatFilesNum() {
      createCodeRule(this.Fileform).then((response) => {
        this.form.filesNum = 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);
      });
    },
    //选择默认的归档位置
    handleSiteChanged(obj) {
      if (obj != null) {
        this.form.organizationId = obj[0];
        this.form.depotId = obj[1];
        this.form.cabinetId = obj[2];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        companyId: undefined,
        company: undefined,
        frequency: undefined,
        fileType: undefined,
        year: undefined,
        period: undefined,
        vouNum: undefined,
        caseNum: undefined,
        catalogNum: undefined,
        filesNum: undefined,
        site: undefined,
        flowId: undefined,
        status: undefined,
        note: undefined,
        fileStatus: undefined,
        borrowStatus: undefined,
        identifyStatus: undefined,
        remark: undefined,
        recordTime: undefined,
        deptId: undefined,
        deptName: undefined,
        attr1: undefined,
        attr2: undefined,
        attr3: undefined,
        attr4: undefined,
        userId: undefined,
      };
      this.Fileform = {
        catalogNum: null,
        caseNum: null,
      };
      this.companyArray = [];
      this.siteArray = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加归档";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getRecord(id).then((response) => {
        this.form = response.data;
        this.siteArray[0] = response.data.organizationId;
        this.siteArray[1] = response.data.depotId;
        this.siteArray[2] = response.data.cabinetId;
        console.log(this.siteArray);
        this.open = true;
        this.title = "修改归档";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateRecord(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createRecord(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 归档按钮操作 */
    handleArchive() {
      const data = this.ids;
      console.log(data);
      this.$modal
        .confirm("是否确认归档")
        .then(function () {
          return archivesRecord(data);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("归档成功");
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除归档编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteRecord(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 exportRecordExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "归档.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss">
.due_month .el-date-picker__header {
  display: none;
}
</style>
