<template>
  <el-dialog :title="props.title" width="95%" v-model="dialogVisible" @close="onClose">
    <div class="flex just-between">
      <div style="flex:1">
        <el-form ref="formRef" label-width="110px" label-align="left" :model="querySelect" label-suffix=":"
          style="display: flex; flex-wrap: wrap">
          <el-form-item label="仓库名称" prop="storehouseName">
            <el-input v-model="querySelect.storehouseName" type="text" style="width: 180px"></el-input>
          </el-form-item>
          <el-form-item label="仓库类别" prop="storehouseTypeCode">
            <div style="width: 180px">
              <MOSTY.DictRidioAndSelect :dictData="cklbList" v-model="querySelect.storehouseTypeCode"
                selectType="select" />
            </div>
          </el-form-item>
          <el-form-item label="仓储形式" prop="cgtStoreFormCode">
            <div style="width: 180px">
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_JYCCXS" v-model="querySelect.cgtStoreFormCode"
                selectType="select" :disabled="false" />
            </div>
          </el-form-item>
          <el-form-item label="仓储性质" prop="cgtStorehousePropertyCode">
            <div style="width: 180px">
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_JYCKXZ" v-model="querySelect.cgtStorehousePropertyCode"
                selectType="select" />
            </div>
          </el-form-item>
          <el-form-item label="上报状态" prop="status">
            <el-select v-model="querySelect.status" style="width: 180px" @change="changeStatus">
              <el-option label="全部" value="" />
              <el-option label="已上报" value="1" />
              <el-option label="未上报" value="2" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div style="display: flex;gap:1px;">
        <el-button @click="onSearch" class="mr10">
          <el-icon>
            <Search />
          </el-icon>搜索</el-button>
        <el-button @click="onRest" class="mr10">
          <el-icon>
            <Refresh />
          </el-icon>重置</el-button>
        <el-button @click="onStagingReport" :disabled="gDisabled">
          <el-icon>
            <Upload />
          </el-icon>暂存上报</el-button>
        <!-- :disabled="gDisabled" -->
        <el-button class="mr10" @click="onAdd">新增</el-button>
      </div>
    </div>
    <div class="dialog-wrapper-content">
      <el-table ref="multipleTableRef" :row-key="keyid" border :data="dataTable"
        style="width: 100%; margin-bottom: 10px" v-loading="tableLoading" :header-cell-style="{ background: '#F6F6F6' }"
        height="100%" @selection-change="onSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="55" fixed />
        <el-table-column :prop="item.prop" :label="item.label" v-for="(item, index) in columnTable" :key="index">
          <template #default="{ row }" v-if="item.prop == 'conHydrantSysFlag'">
            <!-- <dict-tag :options="D_BZ_SF" v-for="it in row.boilerQty" :key="it" :value="it" :tag="false" /> -->
            {{ row.conHydrantSysFlag === "1" ? "是" : "否" }}
          </template>
          <template #default="{ row }" v-else-if="item.prop == 'conAlarmSysFlag'">
            {{ row.conAlarmSysFlag === "1" ? "是" : "否" }}
          </template>
          <template #default="{ row }" v-else-if="item.prop == 'conOutfireSysFlag'">
            {{ row.conOutfireSysFlag === "1" ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" fixed="right">
          <template #default="{ row }">
            <!-- <el-link type="primary" @click="onView(row)">查看</el-link>
          <el-link type="primary" @click="onEdit(row)" :disabled="gDisabled">编辑</el-link> -->
            <el-button type="link" @click="onView(row)">查看</el-button>
            <!-- :disabled="gDisabled" -->
            <el-button style="margin-right: 10px;" type="link" @click="onEdit(row)">编辑</el-button>
            <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" title="确定要删除?"
              @confirm="onDelete(row)">
              <template #reference>
                <el-link type="danger">删除</el-link>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="querySelect.pageCurrent" :page-sizes="[2, 5, 10, 20]" :page-size="querySelect.pageSize"
      layout="total, prev, pager, next, sizes, jumper" :total="total"></el-pagination>
  </el-dialog>
  <Detailcc @close="detailClose" :title="dialogTitle" v-if="detailVisible" :detail="detail"
    :sbsslist="{ D_BZ_JYCCXS, D_BZ_JYCKXZ, D_BZ_SF }" :query="props.params" :rowItem="rowItem" />
</template>

<script setup>
import { onMounted, ref, reactive, getCurrentInstance, nextTick, computed } from "vue"
import { Delete, Edit, Search, Share, Upload } from "@element-plus/icons-vue"
import { getApi, postApi } from "@/api/tobAcco_api.js"
import * as MOSTY from "@/components/MyComponents/index"
import Detailcc from "./Detailcc.vue"
import { getItem } from "@/utils/storage"
import { ElMessage } from "element-plus"
import { filterqx } from "@/utils/auth.js"

const { proxy } = getCurrentInstance()
const { D_BZ_JYCCXS, D_BZ_JYCKXZ, D_BZ_SF } = proxy.$dict("D_BZ_JYCCXS", "D_BZ_JYCKXZ", "D_BZ_SF")
const props = defineProps({
  title: {
    type: String,
    default: ""
  },
  // isProvinces: Boolean,
  params: Object,
  isSfsb: Boolean,
  orgCode: String,
  reportStatus: String,
  rowItem: Object
})
const emits = defineEmits(["close"])
const dialogVisible = ref(true)
const detailVisible = ref(false)
const detail = ref(null)
const dialogTitle = ref("")
const formRef = ref()
const multipleTableRef = ref(null)
const columnTable = ref([
  { label: "企业名称", prop: "baOrgName" },
  { label: "仓库名称", prop: "storehouseName" },
  { label: "仓库类型", prop: "storehouseTypeName" },
  { label: "物流节点名称", prop: "cgtLogtNodeName" },
  { label: "建筑结构", prop: "structureName" },
  { label: "占地面积", prop: "storehouseUseArea" },
  { label: "建筑面积", prop: "totalBuildingArea" },
  { label: "库容量", prop: "storehouseInventoryCapacity" },
  { label: "投产时间", prop: "logtWhseCommissioningTime" },
  { label: "火灾危险性等级", prop: "fireHazardClassifiesName" },
  { label: "仓储物资类别", prop: "storehouseMeterialCatName" },
  { label: "仓储形式", prop: "cgtStoreFormName" },
  { label: "仓储性质", prop: "cgtStorehousePropertyName" },
  { label: "是否配置消火栓系统", prop: "conHydrantSysFlag" },
  { label: "是否配置火灾自动报警系统", prop: "conAlarmSysFlag" },
  { label: "是否配置自动灭火系统", prop: "conOutfireSysFlag" }
])

//县级账号
const isTown = computed(() => {
  return filterqx("isaqbm") == "1" && filterqx("isgs") != "1" && filterqx("isSgs") != "1"
})
const gDisabled = computed(() => {
  return props.rowItem.status === "2" && props.orgCode.includes(currentCode) ? false : true
})
const cklbList = ref([
  { label: "露天货场", value: "1" },
  { label: "洞库", value: "2" },
  { label: "单层仓库", value: "3" },
  { label: "多层仓库", value: "4" },
  { label: "立体仓库", value: "5" },
  { label: "停车场中转", value: "6" }
])
const total = ref(0)
const dataTable = ref([])
const querySelect = reactive({
  pageCurrent: 1,
  pageSize: 10,
  storehouseName: "",
  storehouseTypeCode: "",
  cgtStoreFormCode: "",
  cgtStorehousePropertyCode: "",
  year: props.params.year,
  orgCode: props.orgCode,
  bn: props.params.bn,
  status: props.reportStatus
})
const multipleSelection = ref([])
const currentCode = getItem("deptId").ssdwid
const pageData = reactive({
  ids: []
})
const reportSore = ref([])
const reportChange = reactive({
  notReportIds: [],
  ids: []
})

const tableLoading = ref(false)
onMounted(() => {
  getDataList()
})

// 获取表格数据
const getDataList = () => {
  dataTable.value = []
  // tableLoading.value = true;
  // dataTable.value = [{ baOrgName: "22222" }];
  // querySelect.orgcode = props.orgCode;
  postApi(querySelect, "/mosty-jcgl/data/StorageEscalation/queryStorageEscalationData").then(res => {
    if (res) {
      dataTable.value = res.records
      nextTick(() => {
        // if (querySelect.status === "1") {
        dataTable.value.forEach(item => {
          if (item.status === "1") multipleTableRef.value.toggleRowSelection(item)
          if (!reportSore.value.includes(item.sourceId)) reportSore.value.push(item.sourceId)
        })
        // }
      })

      tableLoading.value = false
      total.value = res.total
    }
  })
}
// 点击操作
const onView = data => {
  detail.value = data
  dialogTitle.value = `仓储设备设施详情`
  detailVisible.value = true
}
const onAdd = () => {
  detail.value = {}
  dialogTitle.value = `仓储设施新增`
  detailVisible.value = true
}
const onEdit = data => {
  detail.value = data
  dialogTitle.value = `仓储设备设施编辑`
  detailVisible.value = true
}
const onDelete = (row) => {
  postApi([row.sourceId], "/mosty-jcgl/data/StorageEscalation/delStorageEscalation").then(res => {
    if (res) {
      ElMessage({
        message: "删除成功",
        type: "success"
      })
      getDataList()
    }
  })

}
// 选择锅炉类型
const onChangesblx = e => {
  getDataList()
}

const keyid = row => {
  return row.sourceId
}
//搜索
function onSearch() {
  querySelect.pageCurrent = 1
  total.value = 0
  getDataList()
}
// 重置
const onRest = () => {
  querySelect.pageCurrent = 1
  querySelect.pageSize = 10
  querySelect.storehouseName = ""
  querySelect.storehouseTypeCode = ""
  querySelect.cgtStoreFormCode = ""
  querySelect.cgtStorehousePropertyCode = ""
  total.value = 0
  reportSore.value = []
  reportChange.ids = []
  reportChange.notReportIds = []
  getDataList()
}
// 关闭弹窗
const onClose = () => {
  emits("close", true)
}
//关闭详情弹窗
const detailClose = val => {
  if (val) {
    getDataList()
  }
  detailVisible.value = false
}
// 切换条数
const handleSizeChange = val => {
  querySelect.pageSize = val
  sorgeIds()
  getDataList()
}
// 切换分页
const handleCurrentChange = val => {
  querySelect.pageCurrent = val
  sorgeIds()
  getDataList()
}
const onSelectionChange = val => {
  multipleSelection.value = val
}
const sorgeIds = () => {
  multipleSelection.value.forEach(item => {
    if (!reportChange.ids.includes(item.sourceId)) reportChange.ids.push(item.sourceId)
  })
  reportChange.notReportIds = reportSore.value.filter(item => !reportChange.ids.includes(item))
}
const onStagingReport = () => {
  sorgeIds()
  postApi({ ...reportChange, ...props.params, orgCode: props.orgCode }, "/mosty-jcgl/data/StorageEscalation/stagingReportData")
    .then(res => {
      if (res) {
        ElMessage({
          message: "暂存上报数据成功",
          type: "success"
        })
        onRest()
      }
    })
    .finally(() => {
      reportChange.ids = []
      reportChange.notReportIds = []
    })
}
const changeStatus = e => {
  getDataList()
}
</script>

<style lang="scss" scoped>
::v-deep .el-link.el-link--primary.is-disabled {
  color: #808596;
  cursor: not-allowed;
}

::v-deep .el-button--link {
  border: none;
  background: none;
  color: var(--el-color-primary);
  padding: 0;
  margin-left: 10px !important;

  &:hover,
  &:focus {
    background: none;
    border: none;
    opacity: 0.9;
  }
}

::v-deep .el-button--link.is-disabled {
  color: #808596;
  border: none;
  background: none;
}

.content {
  .topper {
    display: flex;

    .sub-le {
      flex: 1;
      padding: 15px 15px 5px 15px;
      border-right: 1px solid #ebeef5;
    }
  }

  .bottom {
    padding: 0 15px 10px 15px;
  }
}
</style>
