<template>
  <div class="app-container">
    <el-tabs type="card" v-model="activeName" @tab-click="tabClick">
      <el-tab-pane label="城建项目计划" name="f"></el-tab-pane>
      <el-tab-pane label="重点项目计划" name="s"></el-tab-pane>
    </el-tabs>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-upload" size="mini" @click="doImport"
          v-hasPermi="['project:projectPlan:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="doExport"
          v-hasPermi="['project:projectPlan:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectPlanList">
      <el-table-column label="No." align="center" prop="id" width="50"><template slot-scope="scope" >{{scope.$index+1}}</template></el-table-column>
      <el-table-column :label="activeName == 'f' ? '项目名称' : '项目名称'" align="left" min-width="200" prop="projectName" :show-overflow-tooltip="true"/>
      <el-table-column :label="activeName == 'f' ? '建设规模及主要工程内容' : '建设规模及主要建设内容'"  min-width="300"  align="left" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column :label="activeName == 'f' ? '建设性质' : '建设性质'" align="center" prop="buildType" />
      <el-table-column :label="activeName == 'f' ? '包抓部门' : '包抓部门'" align="center" prop="dept"  min-width="200"/>
      <el-table-column :label="activeName == 'f' ? '开工时限' : '开工日期'" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="activeName == 'f' ? '建成时限' : '完工日期'" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="activeName == 'f' ? '总投资(万元)' : '总投资(万元)'"  min-width="100" align="center" prop="totalInvestment" />
      <el-table-column :label="activeName == 'f' ? '年度投资(万元)' : '年度计划投资(万元)'"  min-width="150" align="center" prop="yearInvestment" />
    </el-table>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="600px" :close-on-click-modal="false" v-if="upload.open"
      :close-on-press-escape="false" append-to-body :custom-class="'proj-plan-import-dlg file-' + upload.files.length">      
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :multiple="false"
        :on-change="uploadChange" :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading" :on-remove="uploadChange" :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>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
            @click="doExport">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAll} from "@/api/project/projectPlan.js";
import { getToken } from "@/utils/auth";
export default {
  name: "ProjectPlan",
  data() {
    return {
      activeName: 'f',
      showSearch: false,
      // 遮罩层
      loading: true,
      // 项目跟进计划表格数据
      projectPlanList: [],      
      // 用户导入参数
      upload: {
        files: [],
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API ,
      },
    };
  },
  created() {
    
  },
  mounted(){
    this.getList();
  },
  methods: {
    tabClick() {
      this.getList();
    },
    /** 查询项目跟进计划列表 */
    getList() {
      this.loading = true;
      listAll(this.activeName=='f'?0:1).then(data => {        
        this.projectPlanList =data.data||[];        
        this.loading = false;
      });
    },
    uploadChange(a, b, c) {
      this.upload.files = b;
    },
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    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();
    },
    /** 新增按钮操作 */
    doImport() {
      if (this.activeName == 'f') {
        this.upload.url = process.env.VUE_APP_BASE_API +  "/project/projectPlan/importUrbanPlan";
      } else {
        this.upload.url = process.env.VUE_APP_BASE_API +"/project/projectPlan/importMajorPlan";
      }
      let name = this.activeName == 'f' ? '城建项目计划' : '重点项目计划';
      this.upload.title = `${name}_导入`;
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    doExport() {
      let url = `project/projectPlan/export/${this.activeName == 'f' ? 0 : 1}`;
      let name = this.activeName == 'f' ? '城建项目计划' : '重点项目计划';
      this.download(url, {}, `${name}_${this.$dt(new Date()).format("YYYYMMDDHHmmss")}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.proj-plan-import-dlg {
  .el-upload {
    width: 100%;
    .el-upload-dragger {
      width: 100%;
    }
  }
  &.file-1 {
    .el-upload {
      display: none;
    }
    .el-upload__tip {
      display: none;
    }
  }
}
</style>
