<template>
  <div>

    <!-- 查询 -->
    <div class='tabox'>
      <div class="headBox3">
        <el-input placeholder="请输入楼层名称进行查询" v-model="pageData.listQuery.placename" clearable
          style='width:400px'></el-input>
        <el-button type="primary" @click="Searchdata">
          <el-icon>
            <Search />
          </el-icon>
          <span>查询</span>
        </el-button>
        <el-button type="primary" @click="addUserHander" v-if='props.obj.saveBtnShow'>
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>新增</span>
        </el-button>
      </div>
      <MOSTY.Table :table-columns="pageData.tableColumns" :tableData="pageData.tableData" :tableloading="tableloading"
        :tableHeight="'40vh'">
        <template #sfds="{ row }">
          <dict-tag v-if="row.sfds" :options="D_BZ_SF" :value="row.sfds" :tag="false" />
          <span v-else>无</span>
        </template>
        <template #actions="{ row }">

          <span class="operedit" @click="handlexg(row)" v-if='props.obj.saveBtnShow'>
            <el-icon>
              <EditPen />
            </el-icon>修改</span>
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" title="确定要删除?"
            @confirm="handledelete(row)" v-if='props.obj.saveBtnShow'>
            <template #reference>
              <span class="operdel" size="small">
                <el-icon>
                  <Delete />
                </el-icon>
                删除</span>
            </template>
          </el-popconfirm>
          <span class="operesee" @click="handleSee(row)">
            <el-icon>
              <Compass />
            </el-icon>查看</span>
        </template>
        <template #fenye>
          <div class="fenye" :style="{ top: tableHeight + 'px' }">
            <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="pageData.listQuery.pageCurrent" background :page-sizes="[10, 20, 50, 100]"
              :page-size="pageData.listQuery.pageSize" layout="total,  prev, pager, next, sizes, jumper"
              :total="total"></el-pagination>
          </div>
        </template>
      </MOSTY.Table>
    </div>
  </div>
  <!-- 楼层 -->
  <div v-if="showdialoglc" class="dialog-box">
    <floorindex :formData="formData" @saveSuccess="saveSuccess" :type="type" />
  </div>
</template>
<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import floorindex from "@/components/dialog/floorindex.vue";
import { reactive, ref, onMounted, getCurrentInstance } from "vue"
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const { D_BZ_SF } = proxy.$dict("D_BZ_SF");
const emits = defineEmits(["handleChange", 'searchChange']);
const tableloading = ref(false);
const total = ref(0)
const showdialoglc = ref(false);
const props = defineProps({
  obj: {
    default: null,
    type: Object
  },
  type: {
    default: '',
    type: String
  }
});
const formData = ref({
  title: ""
});
const type = ref('')
const pageData = reactive({

  listQuery: {
    pageCurrent: 1,
    pageSize: 10,
    placename: ''
  },
  tableColumns: [
    {
      title: "楼层名称",
      prop: "floorname"
    },
    {
      title: "所属场所",
      prop: "sitename"
    },
    {
      title: "所属建筑物",
      prop: "buildingname"
    },
    {
      title: "是否地上",
      prop: "sfds",
      slot: true
    },
    {
      title: "楼层数",
      prop: "floornumber"
    },

    {
      title: "操作",
      prop: "actions",
      slot: true
    }
  ],
  tableData: []
})
onMounted(() => {
  pageData.listQuery.buildingid = props.obj.id
  gettablist()
})
//根据建筑物id分页查询楼层
function gettablist() {
  tableloading.value = true
  postApi(pageData.listQuery, "/mosty-jcgl/floor/listByBuildId").then((res) => {
    if (res) {
      tableloading.value = false
      pageData.tableData = res.records
      pageData.tableData.forEach(item => {
        item.buildingname = props.obj.buildingname
        item.sitename = props.obj.sitename
      })
      total.value = res.total
    }
  })
}
//新增
function addUserHander() {
  type.value = 'add'
  formData.value.title = '新增楼层';
  formData.value.siteid = props.obj.siteid
  formData.value.sitename = props.obj.sitename
  formData.value.buildingid = props.obj.id
  formData.value.buildingname = props.obj.buildingname
  // formData.value.publicarea = props.obj.publicarea
  formData.value.saveBtnShow = true;
  showdialoglc.value = true
  tableloading.value = false
}
//修改
function handlexg(row) {
  type.value = 'xg'
  getApi({ id: row.id }, "/mosty-jcgl/floor/querySceneFloorById").then((res) => {
    if (res) {
      formData.value = res;
      formData.value.title = "修改楼层";
      formData.value.saveBtnShow = true;
      showdialoglc.value = true
    }

  });
}
//查看
function handleSee(row) {
  type.value = 'ck'
  getApi({ id: row.id }, "/mosty-jcgl/floor/querySceneFloorById").then((res) => {
    if (res) {
      formData.value = res;
      formData.value.title = "楼层详情";
      formData.value.saveBtnShow = false;
      showdialoglc.value = true
    }

  });
}
//删除
function handledelete(row) {
  getApi({ id: row.id }, '/mosty-jcgl/floor/deleteSceneFloorOne').then(res => {
    ElMessage.success("删除成功");
    emits("searchChange", 'lc');
    gettablist()
  })
}
function saveSuccess(obj) {
  formData.value = {}
  showdialoglc.value = false
  emits("searchChange", 'lc');
  if (obj) {
    obj.buildingname = props.obj.buildingname
    obj.sitename = props.obj.sitename
    //新增成功不刷新表格直接追加
    pageData.tableData.unshift(obj);
    total.value + 1
  } else {
    //修改不追加，直接刷新
    gettablist()
  }

}
//查询
function Searchdata() {
  gettablist()
}
//页码发生变化
const handleCurrentChange = (val) => {
  pageData.listQuery.pageCurrent = val;
  gettablist()
};
//查询条数发生变化
const handleSizeChange = (val) => {
  pageData.listQuery.pageSize = val;
  gettablist()
};
</script>

<style lang="scss" scoped>
.headBox3 {
  display: flex;
  justify-content: end;
  width: 100%;
  margin-bottom: 10px;
}

.top_ul {
  display: flex;
  justify-content: start;
  flex-wrap: wrap;

  >li {
    width: 15%;
    height: 83px;
    margin: 6px 32px;
  }
}
</style>
