<!-- 设备分配 -->
<template>
  <div class="innerMargin">
      <el-card class="box-card">
        <div style="cursor: default">
          <div class="search-box">
            <div class="filter-box">
              <el-select
                size="small"
                v-model="type"
                @change="equipCheck()"
                clearable
                placeholder="请选择设备类型"
              >
                <el-option label="杀虫灯" value="2"></el-option>
                <el-option label="测报灯" value="3"></el-option>
                <el-option label="智能性诱" value="4"></el-option>
                <el-option label="气象站" value="5"></el-option>
                <el-option label="监控设备" value="6"></el-option>
                <el-option label="孢子仪" value="7"></el-option>
                <el-option label="灾害预警" value="1112"></el-option>
                <!-- <el-option label="性诱设备" value="8"></el-option>
                <el-option label="糖醋测报" value="9"></el-option> -->
              </el-select>
              <el-input
                style="cursor: pointer"
                size="small"
                clearable
                placeholder="请输入设备ID"
                @change="equipCheck()"
                v-model="equipId"
              >
                <i slot="suffix" class="el-input__icon el-icon-search"></i>
              </el-input>
              <el-date-picker
                style="cursor: pointer"
                size="small"
                v-model="timeRange"
                @change="DateChange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </div>
            <el-button type="blueInfo" @click="distSubm" size="mini"
              >确定分配</el-button
            >
          </div>
          <el-row :gutter="10">
            <el-col
              :xs="24"
              :sm="24"
              :md="12"
              :lg="6"
              :xl="4"
              v-for="(item, index) in dataList"
              :key="index"
            >
              <el-card
                @click.native="itemFun(item)"
                :class="['box-card', item.checked ? 'selected' : '']"
              >
                <el-checkbox
                  :ref="'checkboxRef'"
                  v-model="item.checked"
                  @click.native="stopDefault($event)"
                  @change="checkboxChange($event, item.d_id)"
                ></el-checkbox>
                <div class="img-box">
                  <template v-if="item.device_type_id == 2">
                    <img src="@/assets/images/equipdistribute/scdIconOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 3">
                    <img src="@/assets/images/equipdistribute/cbdIconOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 4">
                    <img src="@/assets/images/equipdistribute/znxyOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 5">
                    <img src="@/assets/images/equipdistribute/hjjcIconOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 6">
                    <img src="@/assets/images/equipdistribute/jkIconOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 7">
                    <img src="@/assets/images/equipdistribute/bzyIconOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 8">
                    <img src="@/assets/images/equipdistribute/xyOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 9">
                    <img src="@/assets/images/equipdistribute/tccbOn.png" />
                  </template>
                  <template v-if="item.device_type_id == 11">
                    <img src="@/assets/images/equipdistribute/warnOn11.png" />
                  </template>
                  <template v-if="item.device_type_id == 12">
                    <img src="@/assets/images/equipdistribute/warnOn12.png" />
                  </template>
                  <p>{{ item.device_id }}</p>
                </div>
                <div class="detail">
                  <p v-if="item.name">
                    设备组名称
                    <span>{{ item.name }}</span>
                  </p>
                  <p v-else>
                    设备名称
                    <span>{{ item.device_name || "无" }}</span>
                  </p>
                  <p>
                    设备类型
                    <span>{{ (item.device_type_id + "") | equipType }}</span>
                  </p>
                  <p>
                    设备添加时间
                    <span>{{ (item.creat_time * 1000) | formatTime }}</span>
                  </p>
                  <p>
                    设备地址
                    <span>{{ item.device_location }}</span>
                  </p>
                </div>
              </el-card>
            </el-col>
          </el-row>
          <!-- 暂无数据 -->
          <div class="expertDiagnosis_referral_units_not" v-if="dataList.length <= 0">
            <img
              :src="$imghost + zanwu"
              alt
              class="expertDiagnosis_referral_units_notImg"
            />
          </div>
          <div class="userTip">
            *注：以上所选设备分配给用户：{{ this.$store.state.allo_equip_usename }}
          </div>
          <el-pagination
            v-if="dataList.length > 0"
            background
            :page-size="12"
            layout="prev, pager, next"
            :current-page="page"
            :total="totalNum"
            @current-change="changePage"
          ></el-pagination>
        </div>
      </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      zanwu: "/images/expertDiagnosis/zanwu.png",
      uid: this.$store.state.allo_equip_user_id,
      timeRange: "",
      equipId: "",
      begin: "",
      end: "",
      type: "",
      dataList: [],
      own_device: [],
      device_ids: [], //选中的所有设备ID
      page: 1,
      totalNum: 0,
      equipID: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=device.device_manage.user_device_info",
        data: this.qs.stringify({
          owner_uid: this.uid,
          page_size: 12,
          device_id: this.equipId,
          device_type_id: this.type,
          start_time: this.begin,
          end_time: this.end,
          page: this.page,
        }),
      }).then((res) => {
        if (res.data.message == "") {
          let data = res.data.data.data;
          this.dataList = data.map((item) => {
            item.checked = item.device_user != 0; //device_user=0代表待分配
            return item;
          });
          this.own_device = res.data.data.own_device;
          this.totalNum = res.data.data.counts;
        }
      });
    },
    DateChange(val) {
      if (this.timeRange) {
        this.begin = parseInt(new Date(this.timeRange[0]).getTime() / 1000);
        this.end = parseInt(new Date(this.timeRange[1]).getTime() / 1000);
        this.page = 1;
        this.getList();
      } else {
        this.begin = "";
        this.end = "";
        this.getList();
      }
    },
    changePage(val) {
      this.page = val;
      this.getList();
    },
    distSubm() {
      this.dataList.forEach((item) => {
        if (item.checked == true) {
          this.device_ids.push(item.d_id);
        }
      });
      this.device_ids = this.device_ids.concat(this.own_device);
      this.device_ids = [...new Set(this.device_ids)]; //数组去重
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=device.device_manage.allot_device",
        data: this.qs.stringify({
          device_ids: this.device_ids.join(","),
          uid: this.uid,
        }),
      }).then((res) => {
        if (res.data.message == "") {
          this.$message.success("分配设备成功！");
          // this.$router.go(-1);
        }
      });
    },
    equipCheck() {
      this.page = 1;
      this.getList();
    },
    stopDefault(e) {
      e.stopPropagation();
    },
    checkboxChange(event, d_id) {
      for (let i in this.own_device) {
        if (!event && d_id == this.own_device[i]) {
          //取消勾选并且是用户已拥有的设备
          this.own_device.splice(i, 1);
        }
	  }
    },
    itemFun(item) {
      item.checked = !item.checked;
      this.checkboxChange(item.checked, item.d_id);
    },
  },
};
</script>

<style lang='less' scoped>
.search-box {
  display: flex;
  justify-content: flex-left;
  justify-content: space-between;
  margin-bottom: 10px;
  .filter-box > div {
    margin-right: 15px;
  }
  .el-input {
    width: 200px;
  }
  .el-date-editor--daterange {
    width: 222px;
  }
}
.el-card.selected {
  border: 1px solid #0295ff;
}
.el-card {
  box-sizing: border-box;
  .img-box {
    text-align: center;
    border-bottom: 1px solid #eee;
    padding: 15px 0;
    img {
      height: 45px;
    }
    p {
      font-size: 14px;
      font-weight: 800;
      margin-top: 10px;
    }
  }
  .detail {
    border-bottom: 1px solid #eee;
    padding: 10px 0;
    p {
      display: flex;
      justify-content: space-between;
      font-size: 13px;
      line-height: 34px;
      color: #666;
      padding: 0 0px;
    }
  }
}
.userTip {
  text-align: left;
  font-size: 14px;
}
// 分页为蓝色调
/deep/.el-pagination .el-pager li.active{
  background-color: #0295ff!important;  
}
/deep/.el-pagination.is-background .el-pager li:not(.disabled):hover{
  color: #0295ff;
}
/deep/.el-input__inner:focus{
  outline: none;
  border-color: #0295ff;
}
// 复选框颜色覆盖
/deep/.el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #0295ff;
    border-color: #007bd3;
}
/deep/.el-checkbox__inner:hover{
  border-color: #007bd3;
}
</style>