<template>
  <div>
    <el-dialog title="设备设施选择" width="1200px" v-model="props.modelValue" :before-close="closed">
      <el-form :model="listQuery" class="mosty-from-wrap" :inline="true" style="width: 960px;">
        <el-form-item label="所属部门" style="width: 300px;">
          <MOSTY.Department width="250px" clearable @depitem="changedept" :searchType="props.searchType"
            :deptId="props.deptId" v-model="listQuery.deptId" :key="keys" />
        </el-form-item>
        <el-form-item label="模糊查询" style="width: 300px;">
          <el-input placeholder="请输入名称或内部编号" v-model="listQuery.keywords" clearable style="width: 250px;"></el-input>
        </el-form-item>
        <el-form-item style="width: 144px;">
          <el-button type="success" @click="handleFilter">查询</el-button>
          <el-button type="success" @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
      <div class="flex just-between treeBox2 ww100">
        <div class="tabBox table-box-radio" style="margin-top: 0px;height: 100%;">
          <el-table stripe ref="sbssRef" v-loading="tableloading" @selection-change="handleSelectionChange"
            @row-click="changecoose" :data="tableData" border :row-key="keyid" v-model="currentRow" height="540">
            <el-table-column width="55" #default="{ row }">
              <el-radio v-model="ridioIndex" :label="row.id"></el-radio>
            </el-table-column>
            <el-table-column prop="sitename" align="center" label="场所"></el-table-column>
            <el-table-column prop="partname" align="center" label="部位"></el-table-column>
            <el-table-column prop="name" align="center" label="设备名称"></el-table-column>
            <el-table-column prop="nbbh" align="center" label="内部编号"></el-table-column>
          </el-table>
        </div>
      </div>
      <template #footer>
        <div class="flex just-between">
          <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="listQuery.pageCurrent" :page-sizes="[2, 5, 10, 20]" :page-size="listQuery.pageSize"
            layout="total, prev, pager, next, sizes, jumper" :total="total"></el-pagination>
          <span class="dialog-footer">
            <el-button type="primary" @click="handleSave">
              <el-icon>
                <DocumentChecked />
              </el-icon>
              <span>确定</span>
            </el-button>
            <el-button @click="closed" type="primary">
              <el-icon>
                <DocumentDelete />
              </el-icon>
              <span>关闭</span>
            </el-button>
          </span>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { defineProps, watch, ref, defineEmits, nextTick } from "vue";
import { getApi } from "@/api/tobAcco_api.js";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import { getItem } from "@/utils/storage";
const total = ref(0);
const tableData = ref([]);
const ridioIndex = ref(null);
const sbssRef = ref(null);
const tableloading = ref(false);
const rightData = ref([])
const keys = ref(1)
/** 选中的数据 */
const selectData = ref([])
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 10,
});
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  // 选中的
  data: {
    type: String,
    default: ''
  },
  // 选择设备的回显
  sbid: {
    type: String,
    default: null
  },
  // 查询当前部门下的人员
  deptId: {
    type: String,
    default: () => getItem("deptId").ssdwid
  },
  // 部门树查询类型
  searchType: {
    type: String,
    defaule: "04"
  },
  //需要查询的设备类型
  sblx: {
    type: String,
    default: ''
  }
});
const currentRow = ref(-1); //选中的数据
const emits = defineEmits(["update:modelValue", "choosedSbss"]);
const closed = () => {
  sbssRef.value.clearSelection();
  currentRow.value = [];
  listQuery.value.keywords = []
  emits("update:modelValue", false);
};
const keyid = (row) => {
  return row.id;
};
watch(
  () => props.modelValue,
  (val) => {
    if (val) {
      sbssRef.value = null
      ridioIndex.value = null
      listQuery.value.deptId = props.deptId;
      listQuery.value.sblx = props.sblx;
      handleFilter();
      keys.value++
      selectData.value = props.data;
    }
  }
);
watch(
  () => props.sbid,
  (val) => {
    multipleUser(val);
  }
);
function reset() {
  listQuery.value.keywords = ''
  listQuery.value.deptId = props.deptId;
  listQuery.value.pageCurrent = 1;
  keys.value++
  getListData();
}
//获取查询值
function changedept(val) {
  listQuery.value.deptId = val.orgCode ? val.orgCode : props.deptId;
  listQuery.value.pageCurrent = 1;
  getListData();
}
const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  getListData();
};
const getListData = async () => {
  tableData.value = [];
  tableloading.value = true;
  const params = listQuery.value;
  getApi(params, "/mosty-jcgl/sbglAqsbss/getPage").then((res) => {
    if (res) {
      tableData.value = res?.records;
      total.value = Number(res.total);
      tableloading.value = false;
    }
    if (selectData.value && selectData.value.length > 0 && rightData.value.length == 0) {
      multipleUser(selectData.value);
    }
  })
};
// 列表回显
function multipleUser(row) {
  tableData.value.forEach((val) => {
    sbssRef.value.toggleRowSelection(val, false);
  });
  if (row) {
    ridioIndex.value = row;
  } else {
    ridioIndex.value = null;
  }
}
const handleSelectionChange = (val) => {
  currentRow.value = val;
};
//每一项点击事件
function changecoose(val) {
  ridioIndex.value = val.id
}
const handleSave = () => {
  const info = tableData.value.find((item) => {
    return item.id === ridioIndex.value;
  });
  emits("choosedSbss", info);
  closed();
  sbssRef.value.clearSelection();
};

const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};

const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

</script>

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

::v-deep .el-form--inline {
  padding-left: 0 !important;
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}

::v-deep .el-radio__label {
  display: none !important;
}

.table-box-radio {
  width: 100%;
}

.table-box-radio .el-table1__header-wrapper .el-checkbox {
  display: none !important;
}

:v-deep .el-tree-node {
  .is-leaf+el-checkbox .el-checkbox__inner {
    display: inline-block;
  }
}

::v-deep .el-overlay-dialog {
  overflow: hidden !important;
}

:v-deep .el-checkbox .el-checkbox__inner {
  display: none;
}

:v-deep .wrap .el-checkbox__input.is-disabled {
  display: none;
}

.el-form {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}

.search {
  width: 300px;
  display: flex;
  position: absolute;
  right: 20px;
  top: 20px;
}

.el-form-item {
  text-align: center;
  width: 400px;
}

.flx {
  display: flex;
  width: 100%;

  .el-button {
    margin-left: 10px;
  }
}

.box {
  width: 100%;
  height: 516px;
  overflow-y: hidden;
  border: 1px solid var(--el-color-primary);
  display: flex;
  justify-content: space-between;
}

.treeBox2 {
  overflow: auto;
}

.cskbox {
  width: 40px;
  padding: 107px 0;
  margin-right: 20px;

  .el-icon {
    width: 4em !important;
    height: 6em !important;
  }

  .el-icon svg {
    width: 3em !important;
    height: 3em !important;
  }
}

.listbox {
  border: 1px solid var(--el-color-primary);
  width: 45%;
  overflow-y: scroll;

  ul>li {
    padding: 4px 10px;
    box-sizing: border-box;
  }

  .activ {
    background: var(--el-color-primary);
    color: #fff
  }
}

.bot_font {
  width: 100%;
  text-align: center;
  font-size: 16px;
  color: #ffff;
  margin-top: 10px;
}

.fenye {
  margin-top: 10px;
}

::v-deep .el-dialog__body {
  overflow: auto !important;
  height: 610px !important;
}
</style>
