<template>
  <div class="box">
    <div class="condition">
      <div class="header">
        <!-- <i style="float:left;font-size:30px;padding-right:10px;cursor:pointer;" title="返回" @click="goBack" class="el-icon-back"></i> -->
        <el-button style="float:left;" size="small" type="default" icon="el-icon-back" @click="goBack"></el-button>
        <floor-cascader ref="floorCascader" @change="changeFloor"></floor-cascader>
        <my-cascader ref="cascader" @change="changeDevice"></my-cascader>
        <el-input placeholder="请输入安装位置" style="width: 200px;margin-left:20px;" v-model="keycode" clearable @keyup.enter.native="getTableData">
          <i slot="suffix" class="el-input__icon el-icon-search" style="cursor:pointer;" @click="getTableData"></i>
        </el-input>
        <el-button style="float:right;margin-top:1px;" @click="handleClickCreate">创建已选</el-button>
      </div>
      <div class="main" :style="tableData && tableData.length?'height: calc(100% - 96px);':'height: calc(100% - 46px);'">
        <el-table ref="multipleTable" :data="tableData" v-loading='loading' stripe height="100%" @selection-change="handleSelectionChange" :header-cell-style="headerStyle">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="所属建筑楼层" width="200">
            <template slot-scope="scope">
              <div>
                {{scope.row.Building?scope.row.Building.BuildLocalName?scope.row.Building.BuildLocalName:'':''}} -
                {{scope.row.Floor?scope.row.Floor.FloorLocalName?scope.row.Floor.FloorLocalName:'':''}}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="EquipLocalName" label="资产本地名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="EquipLocalID" label="资产本地编码" show-overflow-tooltip width="200"></el-table-column>
          <el-table-column prop="FamilyName.Name" label="设备族"></el-table-column>
          <el-table-column prop="LedgerParam.Siteinstall.InstallLocation" label="安装位置"></el-table-column>
          <template slot="empty">
          <div style="height: 60%;transform: translateY(50%);">
            <i class="icon-wushuju iconfont"></i>
            数据暂无
          </div>
        </template>
        </el-table>
      </div>
      <div class=footer>
        <el-pagination
          class="right"
          v-show="tableData && tableData.length"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNumber"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
//组件
import floorCascader from "@/components/ledger/lib/floorCascader"
import myCascader from "@/components/ledger/lib/assets"
//api
import { queryProperty } from "@/api/scan/request"
import { createAssetsTask } from "@/api/data_admin/buildTaskApi"
export default {
  data() {
    return {
      params: {},
      loading: false,
      keycode: '',
      page: {
        pageSize: 50,
        pageSizes: [10, 20, 50, 100],
        pageNumber: 1,
        total: 0
      },
      tableData: [],
      multipleSelection:[],
      headerStyle: {
        backgroundColor: '#e1e4e5',
        color: '#2b2b2b',
        lineHeight: '30px'
      }, // 列表样式
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId", "userId", "secret"])
  },
  components: {
    floorCascader,
    myCascader
  },
  created() {
    this.params = this.$route.query
  },
  mounted() {
    this.getTableData()
  },
  watch: {
    projectId() {
      this.page.pageNumber = 1
      this.multipleSelection = []
      this.getTableData()
    }
  },
  methods: {
    //返回
    goBack() {
      this.$router.push({ name: "buildTask" })
    },
    //选择设备族
    changeDevice(val) {
      if(val.code) {
        this.params.category = val.code
      } else {
        this.params.category = ''
      }
      this.page.pageNumber = 1
      this.getTableData()
    },
    //选择建筑楼层
    changeFloor(value) {
      if (value[0]) {
        this.params.buildId = value[0]
      }
      if (value[1]) {
        this.params.floorId = value[1]
      } else {
        this.params.floorId = ''
      }
      this.page.pageNumber = 1
      this.getTableData()
    },
    //获取表格数据
    getTableData() {
      this.loading = true
      let param = this.formatFilter()
      queryProperty(param, res => {
        this.tableData = res.Content
        this.page.total = res.Total
        this.loading = false
      })
    },
    //格式化条件
    formatFilter() {
      let params = {
        Cascade: [
          {
            Name: "familyName"
          },
          {
            Name: "building",
            Projection: [ "BuildLocalName", "BuildName", "BuildID" ]
          },
          {
            Name: "floor",
            Projection: [ "FloorLocalName", "FloorName", "FloorID" ]
          }
        ],
        Filters: `ProjectId='${this.projectId}'`,
        Orders: "CreateTime desc, EquipID asc",
        PageNumber: this.page.pageNumber,
        PageSize: this.page.pageSize
      }

      if(this.params.isVerification){
        params.Filters += `;TaskState isNull`
      }

      if(this.keycode){
        params.Filters += `;LedgerParam.Siteinstall.InstallLocation contain '${this.keycode}'`
      }

      if(this.params.category){
        params.Filters += `;Family='${this.params.category}'`
      }

      if (this.params.buildId == "noKnow") {
        params.Filters += `;BuildingId isNull`
      } else if (this.params.buildId && this.params.buildId != "all") {
        params.Filters += `;BuildingId='${this.params.buildId}'`
      }

      if (this.params.floorId == "noKnow") {
        params.Filters += `;FloorId isNull`
      } else if (this.params.floorId && this.params.floorId != "all") {
        params.Filters += `;FloorId='${this.params.floorId}'`
      }

      return params
    },
    //选择的设备或部件
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //切换每页显示多少条数据
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    //切换页数
    handleCurrentChange(val) {
      this.page.pageNumber = 1
      this.page.pageNumber = val
      this.getTableData()
    },
    //创建已选
    handleClickCreate() {
      let list = this.multipleSelection.map((item) => {
        return {EquipId: item.EquipID}
      })
      let param = {
        Content: list
      }
      createAssetsTask(param, res => {
        this.$message.success("创建成功!")
        this.$router.push({ name: "buildTask" })
      })
    }
  }
};
</script>

<style scoped lang='less'>
.box {
  .condition {
    padding: 10px;
    display: flex;
    height: 100%;
    flex-direction: column;
    border: 1px solid #dfe6ec;
    background: #fff;
    margin-bottom: 10px;
    .header{
      padding-bottom: 10px;
      border-bottom: 1px solid #bcbcbc;
    }
    .main{
      margin-top: 10px;
    }
  }
}
</style>

