<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>
        <die-cascader ref="dieCascader" :Family="params.Family" @change="changeDevice"></die-cascader>
        <floor-cascader ref="floorCascader" @change="changeFloor"></floor-cascader>
        <el-button style="float:right;margin-top:1px;" @click="handleClickCreate">创建已选</el-button>
      </div>
      <div class="main">
        <el-table ref="multipleTable" :data="tableData" v-loading='loading' stripe height="100%"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="设备名称" width="200">
            <template slot-scope="scope">{{
                scope.row.localName ? scope.row.localName : scope.row.name
              }}
            </template>
          </el-table-column>
          <el-table-column label="设备本地编码" show-overflow-tooltip>
            <template slot-scope="scope">{{
                scope.row.localId ? scope.row.localId : scope.row.id
              }}
            </template>
          </el-table-column>
          <el-table-column prop="equipCategory.name" label="设备类型" show-overflow-tooltip
                           width="200"></el-table-column>
          <el-table-column prop="building.localName" label="所属建筑"></el-table-column>
          <el-table-column prop="floor.localName" label="所属楼层"></el-table-column>
          <el-table-column prop="address" label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click.native.prevent="handleOpenDetail(scope.row, tableData)" type="text" size="small">详情
              </el-button>
            </template>
          </el-table-column>
        </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 dieCascader from "@/components/ledger/lib/partsDieList"
//api
import { queryPartsDie, createPropertys } from "@/api/scan/request";

export default {
  data() {
    return {
      params: {},
      loading: false,
      page: {
        pageSize: 50,
        pageSizes: [10, 20, 50, 100],
        pageNumber: 1,
        total: 0
      },
      tableData: [],
      multipleSelection: []
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId", "userId", "secret"])
  },
  components: {
    floorCascader,
    dieCascader
  },
  created() {
    this.params = this.$route.query
  },
  mounted() {
    if (this.params.category) {
      this.$refs.dieCascader.setValue(this.params.category)
    }
    if (this.params.buildId && this.params.floorId) {
      this.$refs.floorCascader.setValue([this.params.buildId, this.params.floorId])
    } else if (this.params.buildId && !this.params.floorId) {
      this.$refs.floorCascader.setValue([this.params.buildId])
    } else {
      this.$refs.floorCascader.setValue(['all'])
    }
  },
  watch: {
    projectId() {
    }
  },
  methods: {
    //返回
    goBack() {
      this.$router.push({
        name: "propertyLedger",
        params: { Family: this.params.Family }
      })
    },
    //选择设备或部件
    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()
      queryPartsDie(param, res => {
        this.tableData = res.content
        this.page.total = res.total
        this.loading = false
      })
    },
    //格式化条件
    formatFilter() {
      let param = {
        cascade: [
          {
            name: "equipCategory",
            projection: ["code", "name"]
          },
          {
            name: "building",
            projection: ["localName", "name", "id"]
          },
          {
            name: "floor",
            projection: ["localName", "name", "id"]
          }
        ],
        filters: `classCode='${this.params.category}';propertyId isNull`,
        orders: "createTime desc, id asc",
        pageNumber: this.page.pageNumber,
        pageSize: this.page.pageSize
      }

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

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

      return param
    },
    //选择的设备或部件
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //切换每页显示多少条数据
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    //切换页数
    handleCurrentChange(val) {
      this.page.pageNumber = val
      this.getTableData()
    },
    //查看详情
    handleOpenDetail(row) {
      // window.open(`http://adm.sagacloud.cn:8058/spread?id=${row.EquipID}&pid=${this.projectId}&secret=${this.secret}`,"_blank")
      this.$message("开发中...")
    },
    //创建已选
    handleClickCreate() {
      let list = this.multipleSelection.map((item) => {
        return item.id
      })
      if (list && list.length) {
        let param = {
					filters: `id in ${JSON.stringify(list)}`
        }
        createPropertys(param, res => {
					this.$message.success("创建成功!")
					this.$router.push({
					name: "propertyLedger",
					params: { Family: this.params.Family }
					})
        })
      } else {
				this.$message.info("请选择设备！")
      }
    }
  }
};
</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;
      height: calc(100% - 96px);
    }

    .footer {
      margin-bottom: 10px;
    }
  }
}
</style>

<style lang="less">
.el-table th {
  background-color: #d9d9d9;
  color: #000;
}
</style>

