<template>
  <div class="app-container" id="app-container">
    <el-form :model="queryParams" id="I-form" ref="queryForm" size="small" :inline="true">
      <el-form-item label="模版名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模版名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产业领域" prop="productArea">
        <el-select v-model="queryParams.productArea" placeholder="请选择产业领域" clearable>
          <el-option
            v-for="dict in dict.type.bms_product_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申报类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择申报类型" clearable>
          <el-option
            v-for="dict in dict.type.bms_declaration_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="责任单位" prop="responsibilityUnit">
        <el-select v-model="queryParams.responsibilityUnit" placeholder="请选择责任单位" clearable>
          <el-option
            v-for="dict in dict.type.bms_responsibility_unit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="search_btn">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="btn">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="InfoList"
      :height="tabHeader"
    >
      <el-table-column label="序号" align="center" width="50" fixed>
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="templateName" label="模版名称" min-width="240" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="级别" min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bms_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="产业领域" :show-overflow-tooltip="true" min-width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bms_product_area" :value="scope.row.productArea"/>
        </template>
      </el-table-column>
      <el-table-column label="申报类型" :show-overflow-tooltip="true" min-width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bms_declaration_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column prop="notes" :show-overflow-tooltip="true" label="说明" min-width="200"></el-table-column>
      <el-table-column label="责任单位" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bms_responsibility_unit" :value="scope.row.responsibilityUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleExport(scope.row)"
          >下载表单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      id="L-pagination"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listInfo, exportData } from "@/api/onlineDeclartion/declarationManagement"

export default {
  name: "Template",
  dicts: ['bms_product_area', 'bms_declaration_type', 'bms_responsibility_unit', 'bms_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 模版列表数据
      InfoList: [],
      // 重新渲染表格状态
      refreshTable: true,
      total: 5,
      // 查询参数
      queryParams: {
        templateName: undefined,
        productArea: undefined,
        type: undefined,
        responsibilityUnit: undefined,
        pageNum: 1,
        pageSize: 20,
      },
      tabHeader: undefined,
    };
  },
  mounted() {
    this.cancalDebounce();
    window.addEventListener('resize', this.cancalDebounce);
  },
  destroyed() {
    window.removeEventListener('resize', this.cancalDebounce);
  },
  created() {
    this.getList();
  },
  methods: {
    // 屏幕尺寸变化
    cancalDebounce(){
      const element = document.getElementById('app-container'); // 通过元素的 ID 获取元素
      const header = document.getElementById('I-form'); // 通过元素的 ID 获取元素
      const pagination = document.getElementById('L-pagination'); // 通过元素的 ID 获取元素
      const elementHeight = element.offsetHeight;
      const headerHeight = header.offsetHeight;
      const paginationtHeight = pagination.offsetHeight;
      this.tabHeader = elementHeight - headerHeight - paginationtHeight - 140;
    },
    /** 查询模版列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.InfoList = response.rows;
        this.total = response.total
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
     /** 查看详情操作 */
    handleDetail(row) {
      this.$router.push({name: 'templateDetail',query: {
        templateId: row.id,
        pageType: "template",
      }})
    },
     /** 导出按钮操作 */
     handleExport(row) {
      exportData({id: row.id}).then(response => {
        let url = window.URL.createObjectURL(new Blob([response]))
        let link = document.createElement('a')
        link.style.display = "none"
        link.href = url
        link.setAttribute("download", `${row.templateName}.xlsx`)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(url)
      })
    },
  }
};
</script>
<style lang="scss" scoped>
// .search_btn {
//   position: absolute;
//   top: 20px;
//   right: 18px;
//   .btn {
//     background-color: #f1d78c;
//     border: none;
//     color: #000;
//   }
// }
// .btn {
//     background-color: #f1d78c;
//     border: none;
//     color: #000;
//   }
/* 悬浮 */
// ::v-deep .el-button:hover {
//   /* background: linear-gradient(#0165eb, #018ff2, #00b7f9) !important; */
//   background: linear-gradient(100deg, #f1d78c, #f1c19d) !important;
//   font-weight: bold;
//   color: #606266;
// }
// /*按钮点击*/
// ::v-deep .el-button:focus {
//   background: linear-gradient(100deg, #f1d78c, #f1c19d) !important;
//   /* background: linear-gradient(#0165eb, #018ff2, #00b7f9) !important; */
//   font-weight: bold;
//   color: #606266;
// }
</style>
