<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--机构数据-->
      <el-col :span="4" :xs="24">
        <org-tree v-model="queryParams.belongOrgId" @defaultKey="getDefaultKey" @defaultOrg="getDefaultOrg"  @checkChange="checkChange"
                  @click="clickTreeNode" hangsheTree :defaultCheckSub="false"></org-tree>
      </el-col>
      <!--搜索栏-->
      <el-col :span="20" :xs="24">
        <div class="main-right-box">
          <div class="main-search-box">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            >
              <el-form-item label="登记簿类型" prop="registerBookType">
                <el-select v-model="queryParams.registerBookType" placeholder="请选择登记簿类型" clearable>
                  <el-option
                    v-for="dict in dict.type.register_book_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="日期" prop="date">
                <el-date-picker
                  v-model="queryParams.date"
                  type="month"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择保存日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item>

              </el-form-item>
            </el-form>

            <el-row :gutter="10">

              <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" @click="exportAll()" :disabled="this.registerBookList.length<=0">导出全部
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" @click="exportSelected()" :disabled="this.ids.length<=0">导出勾选
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" @click="getDownList()">下载列表</el-button>
              </el-col>
              <!--              <el-col :span="1.5">
                              <el-button
                                type="primary"
                                plain
                                icon="el-icon-plus"
                                size="mini"
                                @click="handleAdd"
                                v-hasPermi="['core:registerBook: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="['core:registerBook: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="['core:registerBook: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="['core:registerBook:export']"
                              >导出
                              </el-button>
                            </el-col>-->
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </div>
          <el-table
            border
            height="646"
            v-loading="loading" :data="registerBookList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="登记簿类型" align="center" prop="registerBookType">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.register_book_type" :value="scope.row.registerBookType"/>
              </template>
            </el-table-column>
            <el-table-column label="所属机构" align="center" prop="orgName"/>
            <el-table-column label="统计日期" align="center" prop="date" width="180">
              <!--              <template slot-scope="scope">
                              <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
                            </template>-->
            </el-table-column>

            <el-table-column label="文件名称" align="center" prop="fileName">
              <template slot-scope="r">
                <a :href="imageUrl(r.row.fileUrl) " style="color:#45b97c"
                   target="_blank">{{ r.row.fileName }}</a>
              </template>
            </el-table-column>
            <!--            <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="['core:registerBook:edit']"
                            >修改
                            </el-button>
                            <el-button
                              size="mini"
                              type="text"
                              icon="el-icon-delete"
                              @click="handleDelete(scope.row)"
                              v-hasPermi="['core:registerBook:remove']"
                            >删除
                            </el-button>
                          </template>
                        </el-table-column>-->
          </el-table>

          <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>
    <down-list-dialog ref="downListDialog" @success="refresh(true)"></down-list-dialog>
  </div>
</template>

<script>
import {
  listRegisterBook,
  selectExport,
} from "@/api/registerbook/registerBook.js";
import tableList from "@/mixins/tableList";
import DownListDialog from "./dialog.list";
import OrgTree from "@/components/orgTree";
import {guid} from "@/utils";
import dayjs from "dayjs";

export default {
  name: "RegisterBook",
  dicts: ['register_book_type'],
  components: {OrgTree, DownListDialog},
  mixins: [tableList],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // registerBook表格数据
      registerBookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 导出全部:0,导出勾选:1
        exportType: 1,
        registerBookType: null,
        date: new Date(),
        orgId: null,
      },
      selectedOrgName: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        registerBookType: [
          {required: true, message: "登记簿类型不能为空", trigger: "change"}
        ],
        date: [
          {required: true, message: "保存日期不能为空", trigger: "blur"}
        ],
        orgId: [
          {required: true, message: "机构id不能为空", trigger: "blur"}
        ],
        orgName: [
          {required: true, message: "机构名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询registerBook列表 */
    getList() {
      this.loading = true;
      listRegisterBook(this.queryParams).then(response => {
        this.registerBookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    refresh(isCurrent) {
      this.$refs.st[isCurrent ? "refresh" : "search"]();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        registerBookType: null,
        date: null,
        orgId: null,
        orgName: null,
        orgPath: null,
        fileUrl: null
      };
      this.resetForm("form");
    },
    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    getDefaultOrg(org) {
      this.queryParams.orgId = org.id;
      this.selectedOrgName = org.shortName;
      this.getList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.handleQuery();
    },
    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      this.selectedOrgName = data.shortName;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    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
    },
    async exportAll() {
      //this.search.exportType = 0;
      //await cutExport(this.search);
      this.$message.success("导出成功,请稍后查看导出结果!");
    },
    async exportSelected() {
      if (!this.ids || this.ids.length == 0) {
        this.$message.error('请先选择需要导出的文件！');
        return;
      }
      let pdfIdList = this.ids;
      this.queryParams.exportType = 1;
      let fileName = `登记簿-${dayjs(new Date()).format("YYYYMMDD")}.zip`;
      await selectExport({...this.queryParams, pdfIdList},
        `${this.selectedOrgName}_${this.$tab.getCurrentTabName()}_${dayjs().format("YYYYMMDD")}.zip`);

    },
    getDownList() {
      this.$refs.downListDialog.show(this.search);
    },
  }
};
</script>
