<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px"
      :key="data.elKey">
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId" clearable :disabled="data.currentPrjId ? true : false" filterable
          placeholder="请选择所属项目" style="width: 200px" @change="queryProjectChange">
          <el-option v-for="item in data.projects" :key="item.id" :label="item.projectName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属单位" prop="deptId">
        <el-select v-model="queryParams.deptId" clearable filterable placeholder="请选择所属单位" style="width: 200px">
          <el-option v-for="item in data.subDepts" :key="item.id" :label="item.subDeptName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模型名称" prop="modelName">
        <el-input v-model="queryParams.modelName" placeholder="请输入模型名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" v-if="data.isAdmin" @click="handleAdd">绑定模型</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bimModelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="modelId" />
      <el-table-column label="租户id" align="center" prop="comId" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="单位id" align="center" prop="deptId" />
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <el-table-column label="轻量化名称" align="center" prop="lightweightNam lightweightName" />
      <el-table-column label="api服务返回的json" align="center" prop="gisJson" />
      <el-table-column label="轻量化状态" align="center" prop="modelStatus" />
      <el-table-column label="模型类型" align="center" prop="modelType" />
      <el-table-column label="模型文件类型" align="center" prop="fileType" />
      <el-table-column label="文件大小" align="center" prop="fileSize" />
      <el-table-column label="是否在沙盘显示0不显示，1显示" align="center" prop="showSand" />
      <el-table-column label="${comment}" align="center" prop="status" />
      <el-table-column label="${comment}" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bim:bimModel:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bim:bimModel:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改Bim模型对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <div style="line-height: 60px;">
        <el-button type="primary" @click="selectModelHandler">从模型服务器选择模型</el-button>
      </div>
      <el-form ref="bimModelRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属单位" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入单位id" />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="轻量化名称" prop="lightweightNam lightweightName">
          <el-input v-model="form.lightweightName" placeholder="请输入轻量化名称" />
        </el-form-item>
        <el-form-item label="api服务返回的json" prop="gisJson">
          <el-input v-model="form.gisJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文件大小" prop="fileSize">
          <el-input v-model="form.fileSize" placeholder="请输入文件大小" />
        </el-form-item>
        <el-form-item label="是否在沙盘显示0不显示，1显示" prop="showSand">
          <el-input v-model="form.showSand" placeholder="请输入是否在沙盘显示0不显示，1显示" />
        </el-form-item>
        <el-form-item label="${comment}" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <modelSelectDialog ref="selDlg" />
  </div>
</template>

<script setup name="BimModel">
import { listBimModel, getBimModel, delBimModel, addBimModel, updateBimModel } from "@/api/bim/bimModel";
import { listProProjectInfoSubdepts } from "@/api/manage/proProjectInfoSubdepts";
import { findMyProjectList } from "@/api/publics";
import useUserStore from '@/store/modules/user'
import { onMounted } from "vue";
import modelSelectDialog from './modelSelectDialog.vue'
const { proxy } = getCurrentInstance();

const userStore = useUserStore()
const bimModelList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const selDlg = ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    comId: null,
    projectId: null,
    deptId: null,
    modelName: null,
    lightweightName: null,
    gisJson: null,
    modelStatus: null,
    modelType: null,
    fileType: null,
    fileSize: null,
    showSand: null,
    status: null,
  },
  rules: {
    comId: [
      { required: true, message: "租户id不能为空", trigger: "blur" }
    ],
    projectId: [
      { required: true, message: "项目id不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "单位id不能为空", trigger: "blur" }
    ],
  },
  isAdmin: false,
  projects: [],
  currentPrjId: null,
  subDepts: [],
});

const { queryParams, form, rules } = toRefs(data);

function selectModelHandler() {
  selDlg.value.showDialog();
}
/** 查询Bim模型列表 */
function getList() {
  loading.value = true;
  listBimModel(queryParams.value).then(response => {
    bimModelList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    modelId: null,
    comId: null,
    projectId: null,
    deptId: null,
    modelName: null,
    lightweightName: null,
    gisJson: null,
    modelStatus: null,
    modelType: null,
    fileType: null,
    fileSize: null,
    showSand: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("bimModelRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.modelId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加Bim模型";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _modelId = row.modelId || ids.value
  getBimModel(_modelId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改Bim模型";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bimModelRef"].validate(valid => {
    if (valid) {
      if (form.value.modelId != null) {
        updateBimModel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBimModel(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _modelIds = row.modelId || ids.value;
  proxy.$modal.confirm('是否确认删除Bim模型编号为"' + _modelIds + '"的数据项？').then(function () {
    return delBimModel(_modelIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bim/bimModel/export', {
    ...queryParams.value
  }, `bimModel_${new Date().getTime()}.xlsx`)
}

function queryProjectChange() {
  data.subDepts = []
  queryParams.value.deptId = "";
  if (queryParams.value.projectId) {
    getSubDepts(queryParams.value.projectId, res => {
      data.subDepts = res;
    })
  }
}
function getSubDepts(prjId, cb) {
  listProProjectInfoSubdepts({ projectId: prjId, pageNum: 1, pageSize: 100, activeTags: "finished" }).then(d => {
    let subdepts = d.rows || [];
    cb && cb(subdepts);
  });
}
/** 查询项目列表 */
function getProjectList() {
  findMyProjectList({ pageNum: 1, pageSize: 100 }).then(response => {
    data.projects = response.rows;
  });
}
onMounted(() => {
  queryParams.value.projectId = userStore.currentPrjId;
  queryParams.value.comId = userStore.currentComId;
  queryParams.value.deptId = '';
  data.isAdmin = userStore.isAdmin;
  queryProjectChange();
  data.currentPrjId = userStore.currentPrjId;
  getProjectList();
  getList();
});
</script>
