<template>
  <div>
    <div class="titleBox">
      <div class="title">设备管理</div>
      <div class="btnBox">
        <el-button type="primary" @click="addLoad">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
        <el-button
          @click="deletList"
          :disabled="multipleSelection.length == 0"
          typeof="danger"
        >
          <el-icon style="vertical-align: middle">
            <Delete />
          </el-icon>
          <span style="vertical-align: middle">批量删除</span>
        </el-button>

        <el-button @click="reback" >
          <el-icon style="vertical-align: middle">
            <Aim />
          </el-icon>
          <span style="vertical-align: middle">返回</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form class="mosty-from-wrap" :model="listQuery" :inline="true">
        <el-form-item label="设备编号">
          <el-input
            v-model="listQuery.sbbh"
            placeholder="请输入设备编号"
          ></el-input>
        </el-form-item>
          <el-form-item  label="设备品牌">
          <el-select
            v-model="listQuery.sbPp"
            class="m-2"
            placeholder="请选择设备品牌"
            style="width: 100%"
          >
            <el-option
              v-for="dict in D_BZ_SBPP"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        @selection-change="handleSelectionChange"
        :data="tableData"
        border
        row-key="id"
        :height="tableHeight"
        :key="keyCount"
        v-loading="loadingTable"
        element-loading-background="rgba(0,0,0,0.3)"
        element-loading-text="数据加载中。。"
      >
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column
          type="index"
          show-overflow-tooltip
          align="center"
          width="60px"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="sbmc"
          show-overflow-tooltip
          align="center"
          width="250px"
          label="设备名称"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="sbFl"
          show-overflow-tooltip
          align="center"
          width="150px"
          label="设备分类"
        >
          <template #default="{ row }">
            <dict-tag :options="D_BZ_CLCS" :value="row.sbFl" :tag="false" />
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="azwz"
          show-overflow-tooltip
          align="center"
          width="150px"
          label="安装位置"
        >
          <template #default="{ row }">
            <dict-tag :options="D_BZ_AZWZ" :value="row.azwz" :tag="false" />
          </template>
        </el-table-column>

        <el-table-column
          sortable
          prop="sbbh"
          label="设备编号"
          width="250px"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          prop="sbPp"
          show-overflow-tooltip
          label="设备品牌"
          align="center"
          width="250px"
        >
          <template #default="{ row }">
            <dict-tag :options="D_BZ_SBPP" :value="row.sbPp" :tag="false" />
          </template>
        </el-table-column>

        <el-table-column label="状态" align="center">
          <template #default="{ row }">
            <dict-tag :value="row.xtSjzt" :options="D_BZ_SJZT"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="250px"
        >
          <template #default="{ row }">
            <el-button @click="update(row.id)" size="small">修改</el-button>
            <el-button @click="delDictItem(row)" type="danger" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">{{ diaTitle }}</span>
        <div>
          <el-button
            type="primary"
            :loading="btnLoading"
            size="small"
            @click="submit"
            >保存</el-button
          >
          <el-button size="small" @click="dialogFormVisible = false"
            >关闭</el-button
          >
        </div>
      </div>
      <el-form
        ref="elform"
        :model="form"
        :rules="rules"
        :inline="true"
        label-position="top"
      >
      
        <el-form-item prop="sbmc" label="设备名称">
          <el-input
            v-model="form.sbmc"
            placeholder="请输入设备名称"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item prop="sbFl" label="设备分类">
          <el-select
            v-model="form.sbFl"
            class="m-2"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in D_BZ_CLLX"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="azwz" label="安装位置">
          <el-select
            v-model="form.azwz"
            class="m-2"
            placeholder="请选择安装位置"
            style="width: 100%"
          >
            <el-option
              v-for="dict in D_BZ_AZWZ"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="xtSjzt" label="设备状态">
          <el-select
            v-model="form.xtSjzt"
            class="m-2"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in D_BZ_SJZT"
              :label="item.label"
              :key="item.value"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="sbPp" label="设备品牌">
          <el-select
            v-model="form.sbPp"
            class="m-2"
            placeholder="请选择设备品牌"
            style="width: 100%"
          >
            <el-option
              v-for="dict in D_BZ_SBPP"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否支持视频" prop="sfsp">
          <el-radio-group v-model="form.sfsp" class="ml-4">
            <el-radio
              v-for="(item, index) in D_BZ_SFSP"
              :label="item.value"
              :key="index"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
          <el-form-item prop="sbbh" label="设备编号"  class="tiaos">
          <el-input
            v-model="form.sbbh"
            placeholder="请输入设备编号"
            clearable
            style="width: 100%"
          />
          <el-button @click="getTS" v-show="form.sfsp=='1'">调试</el-button>
        </el-form-item>
  
          
        <el-form-item label="备注" style="width: 100%">
          <el-input
            v-model="form.textarea"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  getXfclsb,
  addXfclsb,
  upXfclsb,
  delXfclsb,
  infoXfsb
} from "@/api/basicsmanage/watchmanCar";
import * as rule from "@/utils/rules.js";
import emitter from "@/utils/eventBus.js";

import { selectDeptPage, getUserInfoToId } from "@/api/user-manage";
import * as MOSTY from "@/components/MyComponents/index";
import { ref, reactive, onMounted, getCurrentInstance,   watch,onUnmounted } from "vue";
import { number } from "echarts";
import { getVideoUrl , getReplayUrl} from "@/api/dpApi/otherApi";
const { proxy } = getCurrentInstance();
const {
  D_BZ_SJZT,
  D_BZ_CLLX,
  D_BZ_CLPP,
  D_BZ_CLCS,
  D_BZ_AZWZ,
  D_BZ_SBPP,
  D_BZ_SFSP
} = proxy.$dict(
  "D_BZ_CLLX",
  "D_BZ_CLCS",
  "D_BZ_SJZT",
  "D_BZ_CLPP",
  "D_BZ_AZWZ",
  "D_BZ_SBPP",
  "D_BZ_SFSP"
);
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20,
  sbbh: "",
  sbPp: "",
  sbmc: "",
  cid:props.id
});
const props = defineProps({
  id: String
})
const chackipt = ref(true);
const keyCount = ref(0); //tabel组件刷新值
const cascader = ref(null);
const loadingTable = ref(true);
const multipleSelection = ref([]); //批量数据
const depList = ref([]); //部门数据

onMounted(() => {
  selectDeptPage({}).then((res) => {
    depList.value = res.records;
  });
  getListData();
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});

onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
const btnLoading = ref(false);
const cId=ref(props.id)
const disable = ref(false);
const chackadd = ref(false); //切换新增修改
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const tableData = ref([]); //表单数据
const total = ref(0); //总数据
const diaTitle = ref("新增巡防设备");
const dialogFormVisible = ref(false);
  const urls =ref ({
    realplay: '',
    talk: '',
    playback: ''
  })
const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  getListData();
};

// 返回
function reback(){
  emitter.emit("reback")
}
//获取数据
const getListData = () => {
  loadingTable.value = true;
   listQuery.value.cid=props.id
  const params = listQuery.value;
  form.value.cid=props.id
  getXfclsb(params).then((res) => {
    tableData.value = res.records;
    total.value = res.total;
    loadingTable.value = false;
  }).catch(()=>{
    loadingTable.value = false;
  });
};
//重置搜索
const reset = () => {
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 20,
    sbbh: "",
    sbPp: "",
    sbmc: "",
    cid:props.id
  };
  getListData();
};
//调试getTS
function getTS() {
  if (form.value.sbbh == "" || form.value.sbbh == null) {
    proxy.$message({
      type: "warning",
      message: `请先输入设备编号`
    });
  } else if (form.value.sbPp == "" || form.value.sbPp == null) {
    proxy.$message({
      type: "warning",
      message: `请先选择设备品牌`
    });
  } else{
    //海能达(通过设备编号获取感知流)
    if(form.value.sbPp == "01"){
        const params = {
    cameraIndexCode: form.value.sbbh
  };
      getVideoUrl(params).then(res=>{
        if(res.data.data){
          urls.value.realplay = res.data.data.url
          realplay()
        }else{
          proxy.$message({
            message: "获取不到视频流，播放失败!!!",
            grouping:true,
            type: "info"
          });
        }
      })
    }else{
      //海康adas流
      const sbbh= '51050400991318001379'
      emitter.emit("openGzyVideo",sbbh)
    }
   
  }
}
//新增弹窗
const addDict = (row) => {
  chackipt.value = false;
  isEdit.value = false;
  dialogForm.value = {};
  dialogFormVisible.value = true;
};
//新增表单对象
const form = ref({
  cid:props.id,
  azwz: "",
  sbFl: "",
  sbbh: "",
  sbPp: "",
  sbmc: "",
  xtSjzt: "",
  sfsp: "0",
  bz: ""
});
const rules = reactive({
  sbFl: [{ required: true, message: "请选择设备种类", trigger: "change" }],
  azwz: [{ required: true, message: "请选择安装位置", trigger: "change" }],
  sbbh: [{ required: true, message: "请输入设备编号", trigger: "change" }],
  sbPp: [{ required: true, message: "请输入设备品牌", trigger: "change" }],
  sbmc: [{ required: true, message: "请输入设备名称", trigger: "change" }],
  sfsp: [{ required: true, trigger: "change" }]
});
//修改
function update(id) {
  chackipt.value = true;
  infoXfsb(id).then((res) => {
    disable.value = false;
    chackadd.value = true;
    diaTitle.value = "设备修改";
    form.value = res;
    dialogFormVisible.value = true;
  });
}
//重置表单
function formReset() {
  form.value = {
   cid:props.id,
  azwz: "",
  sbFl: "",
  sbbh: "",
  sbPp: "",
  sbmc: "",
  xtSjzt: "",
  sfsp: "0",
  bz: ""
  };
}
function addLoad() {
  chackipt.value = false;
  formReset();
  disable.value = false;
  chackadd.value = false;
  diaTitle.value = "新增设备";
  dialogFormVisible.value = true;
 
}
const elform = ref(null);
//提交
function submit() {
  elform.value.validate((valid) => {
    if (valid) {
      btnLoading.value = true;
      setTimeout(() => {
        btnLoading.value = false;
      }, 1500);
      let data = new FormData();
      const keys = Object.keys(form.value);
      keys.map((item) => {
        if (form.value[item] != null && item != "cid") {
          data.append(item, form.value[item]);
        } else if (item == "cid") {
          data.append("cid", form.value.cid);
        }
      });
      data.append("sbbh", form.value.sbbh);
      if (chackadd.value) {
          form.value.cid=props.id;
        upXfclsb(form.value).then((res) => {
          proxy.$message({
            type: "success",
            message: "修改成功"
          });
          dialogFormVisible.value = false;
          getListData();
        });
      } else {
          form.value.cid=props.id;
        addXfclsb(form.value).then((res) => {
          proxy.$message({
            type: "success",
            message: "新增成功"
          });
          dialogFormVisible.value = false;
          getListData();
        });
      }
    }
  });
}
//删除
function delDictItem(row) {
  proxy
    .$confirm("确定要删除", "警告", {
      type: "warning"
    })
    .then(() => {
      const ids = [row.id];
      delXfclsb(ids).then((res) => {
        proxy.$message({
          type: "success",
          message: "删除成功"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
}
//批量数据
const handleSelectionChange = (val) => {
  multipleSelection.value = [];
  if (val) {
    val.forEach((item) => {
      multipleSelection.value.push(item.id);
    });
  }
};
//批量删除
const deletList = () => {
  proxy
    .$confirm("确定要删除", "警告", {
      type: "warning"
    })
    .then(() => {
      const ids = multipleSelection.value;
      delXfclsb(ids).then(() => {
        proxy.$message({
          type: "success",
          message: "删除成功"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
};
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
// 表格高度计算
const tabHeightFn = () => {
 tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";

.tiaos {
  position: relative;
  .el-button {
    position: absolute;
    right: 0;
    bottom: 0;
    right: -100px;
  }
}
</style>
