<template>
  <div class="app-container iot-project">
    <component :is="componectVal" :sourceId="sourceId"></component>

    <div v-show="componectVal === ''">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="queryParams.projectName"
            placeholder="请输入项目名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目类型" prop="industry">
          <el-select v-model="queryParams.industry" placeholder="请选择项目类型" clearable size="small">
            <el-option
              v-for="dict in projectTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <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-form>

      <el-table
        v-loading="loading"
        :data="projectList"
        :default-sort="{prop: 'createTime', order: 'descending'}"
        @sort-change="sortChange"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
          :index="indexFormatter"
          width="80px"
        ></el-table-column>
        <el-table-column label="项目名称" width="200px" align="left" prop="projectName" />
        <el-table-column
          label="项目类型"
          align="center"
          width="150"
          :formatter="statusFormat"
          prop="industry"
        />
        <el-table-column label="行政区划" align="left" width="250" prop="regionalismFullName" />
        <el-table-column label="项目地址" align="left" prop="projectAddress" />
        <el-table-column
          label="创建时间"
          sortable="custom"
          align="center"
          width="160"
          prop="createTime"
        />
        <el-table-column
          label="操作"
          align="center"
          width="200"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="handleDetails(scope.row)"
            >详情</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 class="to-home-wrap2" @click="toTableClick" v-show="componectVal !== ''">
      <el-button icon="el-icon-d-arrow-left" title="返回列表" circle>返回列表</el-button>
    </div>
  </div>
</template>

<script>
import {
  listProject,
  exportProject
} from "@/api/tenant/project";
import DetailsWrap from "./profile/details";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Project",
  components: {
    Treeselect,
    DetailsWrap
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        industry: null,
        regionalismId: null,
        projectAddress: null,
        projectStatus: null,
        orderByColumn: "createTime",
        isAsc: "desc"
      },
      projectTypeOptions: [],
      // 行政划分树选项
      regionalismOption: [],
      componectVal: "",
      sourceId: ""
    };
  },
  created() {
    if (this.$route.query['projectId']) {
      this.handleDetails(this.$route.query)
    }
    this.getDicts("project_industry").then(response => {
      this.projectTypeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    handleDetails(row) {
      this.sourceId = row.projectId;
      this.componectVal = "DetailsWrap";
    },
    // 跳转详情页
    toTableClick() {
      if (this.$route.query['projectId']) {
        this.$router.push('/project_tenant/project_tenant')
      }
      this.componectVal = "";
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.projectTypeOptions, row.industry);
    },
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有项目数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportProject(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
<style lang="scss">
.iot-project {
  .eldialog-wrap {
    .el-dialog__header {
      border-bottom: 1px solid #747373;
    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;
      border-top: 1px solid #747373;
      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
  }
  .to-home-wrap2 {
    width: 100px;
    height: 20px;
    position: absolute;
    right: 30px;
    top: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    color: #656363;
    font-size: 20px;
    cursor: default;
    .el-button--medium.is-circle {
      width: 25px;
      height: 20px;
      padding: 0;
      background: #f26a6a;
      color: #fff;
      font-size: 16px;
      border-radius: 5px;
      height: 30px;
      width: 100%;
      font-size: 14px;
    }
  }
  .to-home-wrap2:hover {
    color: #1890ff;
    font-size: 30px;
  }
}
</style>
