<!--
    changeRea 厂商
-->
<template>
  <el-dialog title="关联资产" :visible.sync="dialog.changeRea" width="800px">
    <div>
      <div id="changeRea">
        <div class="title-search" style="margin-bottom: 10px;">
          <el-input
            placeholder="输入名称查询"
            v-model="search"
            size="small"
            style="width:300px;"
            clearable
          ></el-input>
          <el-button @click="getData" size="small">查找</el-button>
        </div>
        <div>
          <floor-cascader @change="changeFloor"></floor-cascader>
        </div>
        <el-radio-group v-model="radio" style="width:100%;">
          <el-table :data="tableData" class="data-table border" style="width: 100%" height="300px">
            <el-table-column label="资产名称">
              <template slot-scope="scope">
                <el-radio
                  v-model="radio"
                  :label="scope.row.id"
                >{{scope.row.infos.EquipLocalName || scope.row.infos.EquipName}}</el-radio>
              </template>
            </el-table-column>
            <el-table-column label="本地编码">
              <template
                slot-scope="scope"
              >{{scope.row.infos.EquipLocalID || scope.row.infos.EquipID}}</template>
            </el-table-column>
            <el-table-column prop="name" label="所在楼层">
              <template slot-scope="scope">{{getFloor(scope.row.relation)}}</template>
            </el-table-column>
            <el-table-column label="设备族">
              <template slot-scope="scope">{{getFramily(scope.row.family)}}</template>
            </el-table-column>
            <el-table-column label="安装位置">
              <template slot-scope="scope">{{scope.row.infos.InstallLocation || "--"}}</template>
            </el-table-column>
          </el-table>
        </el-radio-group>
        <my-pagination :page="page" @change="changed" style="margin-top:10px;"></my-pagination>
        <div class="footer">
          <el-button
            type="primary"
            @click="clickTrue"
            style="display:block;margin: 10px auto 0;"
          >确 定</el-button>
        </div>
      </div>
    </div>
    <param-details :isLoad="isLoad" :data="data"></param-details>
  </el-dialog>
</template>
<script>
import myPagination from "@/components/ledger/lib/myPagination";
import paramDetails from "@/components/dialogs/list/paramDetails"
import { getBillPropList, getEquipmentFamily, getSpaceFloor, getBussines2, upDateTableMain } from "@/api/scan/request"
import floorCascader from "@/components/ledger/lib/floorCascader";
import {mapGetters, mapActions} from "vuex"

export default {
  components: {
    myPagination,
    paramDetails,
    floorCascader
  },
  props: {
    dialog: {
      type: Object,
      default: function () {
        return {
          changeRea: true
        };
      }
    },
    category: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      search: "", //搜索文案
      radio: "",
      tableData: [
      ],
      data: {},
      isLoad: {
        paramShow: false
      },
      page: {
        size: 50,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 0,
        currentPage: 1
      },
      mess: {},
      codeListCode: [],
      floorList: []
    };
  },
  created() {
    this.getCode()
  },
  computed: {
    ...mapGetters("layout", [
        "projectId",
        "secret",
        "userId"
    ])
  },
  mounted() { },
  methods: {
    getFloor(relation) {
      let floor = !!relation ? relation.floor || "" : ""
      if (!!floor) {
        console.log(floor)
        let floorName = ""
        this.floorList.map(item => {
          if (!!item.floors && item.floors.length) {
            item.floors.map(child => {
              if (child.id == floor) {
                floorName = child.infos.FloorLocalName || child.infos.FloorName
              }
            })
          }
        })
        return floorName
      } else {
        return "--"
      }
    },

    clickTrue() {
      if (!!this.radio) {
        let param = {
          UserId: this.userId,
          ProjId: this.projectId
        }
        let paramList = [{
          EquipmentId: this.category.id,
          FmId: this.radio
        }]
        console.log(param, paramList)
        upDateTableMain(param, paramList).then(res => {
          this.$emit("close")
          this.dialog.changeRea = false
        })
        // upDateTableMain(param, res => {
        // })
      } else {
        this.$message("请先选择资产")
      }
    },

    getFramily(code) {
      console.log(code)
      let name = ""
      this.codeListCode.map(item => {
        if (item.code == code) {
          name = item.name
        }
      })
      return name || "--"
    },
    getCode() {
      getEquipmentFamily(res => {
        this.codeListCode = res.Content
        this.getFloorList()
      })
    },
    getFloorList() {
      let param = {
        secret: this.secret,
        ProjId: this.projectId
      }
      getSpaceFloor(param).then(res => {
        if (res.data.Result == "success") {
          this.floorList = res.data.Content
        } else {
          this.$message.error("请求错误：" + res.data.ResultMsg)
        }
      }).catch(_ => {
        this.$message.error("请求错误")
      })
    },
    changeFloor(val) {
      this.mess = {
        buildId: val[0] || "",
        floorId: val[1] || ""
      }
      this.getData()
      console.log(val)
    },
    changed() {
      this.getData()
    },
    //查询技术参数
    lookParam(data) {
      this.data = data
      this.isLoad.paramShow = true
    },
    getChange() {
      if (!!this.radio) {
        this.$emit("changechangeRea", this.radio)
        this.dialog.changeRea = false
      } else {
        this.$message("请选择型号")
      }
    },
    searchKey() {
      this.changePage()
    },
    changePage() {
      this.page = {
        size: 50,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 0,
        currentPage: 1
      }
      this.getData()
    },
    getData() {
      let param = {
        data: {
          directOnly: false,
          criteria: {
            EquipLocalName: [this.search == ""? null: this.search],
            id: "",
            type: [],
            exclude: [{
              graphType: "Pe2Eq",
              side: "fromId",
            }]
          }
        },
        secret: this.secret,
        ProjId: this.projectId
      }
      if (this.mess.buildId == "all") {
        param.data.criteria.id = this.mess.ProjId
      } else if (this.mess.buildId == "noKnow") {
        param.data.criteria.id = this.mess.ProjId
        param.data.directOnly = true
      } else if (this.mess.floorId == "noKnow") {
        param.data.criteria.id = this.mess.buildId
        param.data.directOnly = true
      } else if (this.mess.floorId && this.mess.floorId != "all") {
        param.data.criteria.id = this.mess.floorId
      } else if (this.mess.floorId == 'all') {
        param.data.criteria.id = this.mess.buildId
      } else {
        param.data.criteria.id = this.projectId
      }
      param.data.criteria.type = [this.getType(this.category.category)]
      console.log(param)
      getBussines2(param).then(res => {
        this.tableData = res.data.Content
        console.log(this.tableData, "this.tableData")
        this.page.total = res.data.Count
      })
    },

    getType(code) {
      console.log(code, "code")
      console.log(code.substring(2, 6))
      let system = code.substring(2, 6), myCode = ""
      this.codeListCode.map(item => {
        if (!!item.content && item.content.length) {
          item.content.map(child => {
            if (child.code == system) {
              myCode = item.code
            }
          })
        }
      })
      console.log(myCode)
      return myCode
    }
  },
  watch: {
    dialog: {
      deep: true,
      handler: function () {
        if (this.dialog.changeRea) {
          this.changePage()
          this.radio = ""
        }
      }
    }
  }
};
</script>

<style lang="less">
#changeRea {
  .el-table thead {
    tr {
      th {
        background-color: #f5f7fa;
      }
    }
  }
}
</style>
