<template>
  <div class="app-container" id="app-container">
    <el-form id="I-form" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <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-row id="I-rowOne" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          class="btn"
        >新增在线申报</el-button>
      </el-col>
    </el-row>

    <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="160" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="级别" min-width="80">
        <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="140">
        <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="125">
        <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" label="说明" min-width="180"></el-table-column>
      <el-table-column label="责任单位" min-width="80">
        <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" min-width="100" prop="createTime">
        <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="handleTemplate(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"
    />
    <!-- 添加或修改菜单对话框 -->
    <el-dialog title="请选择申报表单" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="责任单位" prop="responsibilityUnit">
              <el-select v-model="form.responsibilityUnit" placeholder="请选择责任单位" style="width: 100%;" @change="getTemplateList" clearable>
                <el-option
                  v-for="dict in dict.type.bms_responsibility_unit"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="申报表单模版" prop="templateId">
              <el-select v-model="form.templateId" placeholder="请选择申报表单模版" style="width: 100%;" @change="changeTemplateId" :disabled="!this.form.responsibilityUnit" clearable>
                <el-option
                  v-for="dict in templateDataList"
                  :key="dict.templateId"
                  :label="dict.templateName"
                  :value="dict.templateId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="表单说明" prop="notes">
              <el-input v-model="form.notes" type="textarea" placeholder="请输入说明" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" class="btn">开始填报</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "declarationManagement",
  dicts: ['sys_show_hide', 'sys_normal_disable', 'bms_product_area', 'bms_declaration_type', 'bms_responsibility_unit', 'bms_level', 'bms_template_type', 'bms_declaration_channels'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      InfoList: [],
      // 菜单树选项
      menuOptions: [],
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      total: 5,
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined,
        ageNum: 1,
        pageSize: 20,
      },
      enterpriseId: '',
      templateDataList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        responsibilityUnit: [
          { required: true, message: "请选择责任单位", trigger: "blur" }
        ],
        templateId: [
          { required: true, message: "请选择申报表单模版", trigger: "blur" }
        ]
      },
      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 headerTwo = document.getElementById('I-rowOne'); // 通过元素的 ID 获取元素
      const pagination = document.getElementById('L-pagination'); // 通过元素的 ID 获取元素
      const elementHeight = element.offsetHeight;
      const headerHeight = header.offsetHeight;
      const headerOHeight = headerTwo.offsetHeight;
      const paginationtHeight = pagination.offsetHeight;
      this.tabHeader = elementHeight - headerHeight - headerOHeight - paginationtHeight - 140;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.InfoList = response.rows;
        this.total = response.total
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menuId: undefined,
        parentId: 0,
        menuName: undefined,
        icon: undefined,
        menuType: "M",
        orderNum: undefined,
        isFrame: "1",
        isCache: "0",
        visible: "0",
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    getTemplateList(val) {
      if(val){
        let params = {
          responsibilityUnit: val,
          enterpriseId: this.$store.state.user.enterpriseId,
          pageNum: 1,
          pageSize: 10,
        }
        templateList(params).then(response => {
          console.log(response);
          this.templateDataList = response.rows
        })
      } else {
        this.form.templateId = ''
      }
    },
    /** 获取说明 */
    changeTemplateId(val) {
      this.templateDataList.forEach(item => {
        if(item.templateId == val) {
          this.form.notes = item.notes
          this.form.templateRecordId = item.templateRecordId
        }
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$router.push({name: 'declareAddContent',query: {
            templateId: this.form.templateId,
            templateRecordId: this.form.templateRecordId,
            pageType: "add",
          }})
          // this.$router.push({name: 'declareAddContent',query: {templateId: 3,templateRecordId: 57,pageType: "add",}}) //载体建设
        }
      });
    },
    /** 导出按钮操作 */
    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)
      })
    },
    /** 查看表单模版 */
    handleTemplate (row) {
      this.$router.push({name: 'templateDetail',query: {
        templateId: row.id,
        pageType: "template",
      }})
    }
  }
};
</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>
