<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.localName ? scope.row.Building.localName : '' : '' }} -
                {{ scope.row.floor ? scope.row.floor.localName ? scope.row.Floor.localName : '' : '' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="localName" label="资产本地名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="localId" label="资产本地编码" show-overflow-tooltip width="200"></el-table-column>
          <el-table-column prop="familyName.name" label="设备族"></el-table-column>
          <el-table-column prop="infos.installLocation" label="安装位置"></el-table-column>
          <el-table-column prop="type" label="现场验证操作规定" width="200">
            <template slot-scope="scope">
              <el-select style="width:100px;" v-model="scope.row.schemeId" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </template>
          </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
      },
      options: [{//方案
        value: '1',
        label: '极简'
      }, {
        value: '2',
        label: '标准'
      }],
      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: ["localName", "name", "id"]
          },
          {
            name: "floor",
            projection: ["localName", "name", "id"]
          }
        ],
        filters: `projectId='${this.projectId}'`,
        orders: "createTime desc, equipId asc",
        pageNumber: this.page.pageNumber,
        pageSize: this.page.pageSize
      }

      if (this.params.isVerification !== false) {
        // params.Filters += `;(TaskState isNull || TaskState=0)`
        // params.Filters += `;(TaskState!=0 || TaskState!=-1 || TaskState!=1)`
        params.filters += `;taskState isnull`

      } else {
        // params.Filters += `;(TaskState!=1)`
        params.filters += `;(taskState isnull or taskState !=1 )`

      }

      if (this.keycode) {
        params.filters += `;infos.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() {
      if (this.multipleSelection.length) {
        let list = this.multipleSelection.map((item) => {
          if (item.schemeId) {
            return {
              equipId: item.id,
              schemeId: item.schemeId
            }
          } else {
            return {
              equipId: item.id
            }
          }
        })
        let param = {
          content: list
        }
        createAssetsTask(param, res => {
          this.$message.success("创建成功!")
          this.$router.push({name: "buildTask", query: {newTaskTypes: this.$route.query.newTaskTypes}})
        })
      } else {
        this.$message('请选择要创建的任务！')
      }
    }
  }
};
</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>

