<template>
  <Dialog
    v-model="visible"
    diaTitle="参数引用"
    :width="'70%'"
    :height="'50%'"
    :zIndex="100"
    @close="handleCloseFun"
    show-footer
  >
    <div class="param-page">
      <BaseTable ref="paramTableRef" tableName="PARAMETER_LIST" hidePagination>
        <template #leftOptions>
          <el-form inline :model="paramFormData">
            <el-form-item label="参数库名称：">
              <el-select
                class="m150"
                v-model="paramFormData.parameterLibraryId"
                @change="libChangeFun"
              >
                <el-option
                  v-for="item in paramLibList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="分类名称：">
              <el-select
                class="m150"
                v-model="paramFormData.parameterLibraryCategoryId"
                @change="paramTypeChangeFun"
              >
                <el-option
                  v-for="item in paramLibTypeList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="参数对象名称：">
              <el-select
                class="m150"
                v-model="paramFormData.parameterLibraryCategoryObjectId"
                @change="parameterLibraryCategoryObjecChangeFun"
              >
                <el-option
                  v-for="item in paramLibTypeObjList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </template>
      </BaseTable>
    </div>
    <template #footer>
      <el-button type="" @click="handleCloseFun">关闭</el-button>
      <el-button type="primary" @click="submitFun">应用</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { nextTick, onMounted, reactive, ref } from 'vue';
import Dialog from '@/components/common/dialog/index.vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import {
  getSimulationParameterTreeApi,
  getSimulationParameterLibraryCategoryObjectApi,
} from '@/api/data/simulationParameterLibrary';
import { ElMessage } from 'element-plus';

const visible = ref(true);
const emits = defineEmits(['close', 'update']);

const paramFormData = reactive<any>({
  parameterLibraryId: '',
  parameterLibraryCategoryId: '',
  parameterLibraryCategoryObjectId: '',
});

const handleCloseFun = () => {
  emits('close');
};

const tableData = ref<any>([]);
const paramTableRef = ref();

const paramLibList = ref<any>([]);
const paramLibTypeList = ref<any>([]);
const paramLibTypeObjList = ref<any>([]);

const getSimulationParameterTreeDAtaFun = async (type: any, id?: any) => {
  const param: any = {
    type: type,
  };

  if (id) {
    param.id = id;
  }

  const res: any = await getSimulationParameterTreeApi(param);
  if (res && res.code === 200) {
    if (type === 1) {
      if (id) {
        paramLibTypeList.value = res.data;
      } else {
        paramLibList.value = res.data;
      }
    } else {
      paramLibTypeObjList.value = res.data;
    }
  }
};

const libChangeFun = async () => {
  paramFormData.parameterLibraryCategoryId = '';
  await getSimulationParameterTreeDAtaFun(1, paramFormData.parameterLibraryId);
};

const paramTypeChangeFun = async () => {
  paramFormData.parameterLibraryCategoryObjectId = '';
  await getSimulationParameterTreeDAtaFun(2, paramFormData.parameterLibraryCategoryId);
};

const parameterLibraryCategoryObjecChangeFun = async () => {
  const res: any = await getSimulationParameterLibraryCategoryObjectApi({
    ObjectId: paramFormData.parameterLibraryCategoryObjectId,
  });
  if (res && res.code === 200) {
    tableData.value = res.data.parameterJsonValue;

    nextTick(() => {
      paramTableRef.value.setDataFun(tableData.value);
    });
  }
};

const submitFun = async () => {
  if (!tableData.value?.length) {
    ElMessage.warning('没有参数，无需引用！');
    return;
  }

  emits('update', tableData.value);
};

onMounted(async () => {
  await getSimulationParameterTreeDAtaFun(1);

  nextTick(() => {
    paramTableRef.value.setDataFun(tableData.value);
  });
});
</script>

<style lang="scss" scoped>
.param-page {
  width: 100%;

  .el-form-item {
    margin-bottom: 0;
  }

  .m150 {
    width: 150px;
  }
}
</style>
