<template>
  <div>
    <!-- 负面清单管理 -->
    <!-- 表单查询项 -->
    <div class="headerbox">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200">
        <el-row>
          <el-col :span="5">
            <el-form-item label="项目名称" prop="postName" style="width: 100%;">
              <el-input v-model="queryParams.postName" placeholder="请输入项目名称" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="建设起止时间">
              <el-date-picker v-model="queryParams.begainTime" type="date" placeholder="开始日期"
                style="width: 11rem;"></el-date-picker>
              ~
              <el-date-picker v-model="queryParams.endTime" type="date" placeholder="结束日期"
                style="width: 10.5rem;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="现状分类">
              <el-select v-model="queryParams.postCode" placeholder="现状分类" clearable>
                <el-option v-for="dict in dict.type.xzfl" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="项目法人单位">
              <el-input v-model="queryParams.xmfrdwxz" placeholder="请输入项目法人单位" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 表格内容区 -->
    <div class="tablebox">
      <!-- 标签行 -->
      <div class="tablehead">
        <!-- <div class="headtitle"><span>项目清单</span></div> -->
      </div>
      <!-- 导入弹窗 -->
      <el-dialog title="导入" :visible.sync="upload.open" width="400px" append-to-body>
        <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip text-center" slot="tip">
            <span>仅允许导入xls、xlsx格式文件。</span>
          </div>
        </el-upload>
        <span slot="footer" class="dialog-footer">
          <el-button @click="upload.open = false">取消</el-button>
          <el-button type="primary" @click="submitFileForm" :loading="upload.isUploading">确定</el-button>
        </span>
      </el-dialog>
      <!-- 导出行 -->
      <div class="tablebtntwo">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <!-- <el-button type="primary" icon="el-icon-upload2" size="medium" @click="handleExport">导出</el-button> -->
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange" stripe>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="项目名称" align="center" prop="name" width="200" />
        <el-table-column label="现状分类" align="center" prop="xzfl">
          <template slot-scope="scope">
            <span>{{ xzflMap[scope.row.xzfl] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目法人单位" align="center" prop="xmfrdwxz" width="200" />
        <el-table-column label="项目建设起止时间" align="center" width="200">
          <template slot-scope="scope">
            {{ scope.row.begainTime }} 至 {{ scope.row.endTime }}
          </template>
        </el-table-column>
        <el-table-column label="总投资额（万元）" align="center" width="130" prop="ztze" />
        <el-table-column label="总用地面积（平方米）" align="center" prop="zydmj" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="getInfo(scope.row)">详情</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)" style="color: #F25353;">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { getBasicInformationPage } from "@/api/ManageApi/index";
import { getToken } from "@/utils/auth";
import { checkPermi, checkRole } from "@/utils/permission";
export default {
  name: "Post",
  dicts: ["xzfl"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      postList: [],
      // 状态颜色映射
      statusColors: {
        '审核通过': '#6EDABE',
        '待填报': '#FFBF6B',
        '待审核': '#7693D8'
      },
      //状态文本映射
      statusMap: {
        1: '待填报',
        2: '待审核',
        3: '审核通过'
      },
      xzflMap: {
        1: '已建',
        2: '在建',
        3: '拟建'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        xmfrdwxz: undefined,
        dateRange: [],
        status: undefined
      },
      // 导入相关数据
      upload: {
        open: false, // 是否显示导入弹窗
        isUploading: false, // 是否正在上传
        updateSupport: 0, // 是否更新已存在数据
        headers: { Authorization: "Bearer " + getToken() }, // 上传请求头
        url: process.env.VUE_APP_BASE_API + "/gysl/basicInformation/import", // 上传地址
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    checkRole,
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      getBasicInformationPage(this.queryParams).then((response) => {
        this.postList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/gysl/basicInformation/exportInfo', {
        ...this.queryParams,
        idList: this.ids[0]
      }, `post_${new Date().getTime()}.xlsx`)
    },

    /** 详情按钮操作 */
    getInfo(row, type) {
      this.$store.commit("SET_CRUMBS", this.$route.meta.title + "详情");
      const id = row.id || this.ids[0];
      this.$router.push({ path: `/manage-info/${id}`, query: { action: type } });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
};
</script>

<style scoped>
.headerbox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablebox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablehead {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.headbtn {
  display: flex;
}

.tablebtntwo {
  margin-top: 1rem;
  margin-bottom: 1rem;
}
</style>