<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="18">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="0">
          <el-form-item label="" prop="modelName" style="margin-right:10px">
            <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-col>
      <el-col :span="6" style="text-align: right">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['hl:bm:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bmList" border>
      <el-table-column label="模型ID" align="center" prop="modelId"  width="80"/>
      <el-table-column label="模型名称" align="left" prop="modelName" />
      <el-table-column label="昨日健康度" align="center" prop="yesterdayScore"  width="180"/>
      <el-table-column label="最近一次健康度得分" align="center" prop="lastScore"  width="180"/>
      <el-table-column label="更新时间" align="left" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="480" class-name="small-padding fixed-width" align="left" >
        <template #default="scope">
          <el-button link type="primary" icon="Position" @click="handleHealthSet(scope.row,'health')">健康度配置</el-button>
          <el-button link type="primary" icon="Calendar" @click="handleHealthSet(scope.row,'history')">历史健康度</el-button>
          <el-button link type="primary" icon="Clock"  @click="handleHealthSet(scope.row,'day')">日健康度</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hl:bm:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hl:bm: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"
    />

    <!-- 添加或修改业务模型对话框 -->
    <el-dialog :title="title" v-model="open" :width="dialogType ==='health'?'1600px':'800px'" append-to-body :style="dialogType ==='health'?'margin-top:2px!important;':''">
      <add-bm-info ref="bmRef" @cancel="open = false" @success="getList" v-if="dialogType==='add'"/>
      <component :is="activeComponent[currentActive]" v-else-if="dialogType!=='add' && open" :modelId="modelId" :dialogType="dialogType"/>
    </el-dialog>
  </div>
</template>

<script setup name="Bm">

import { listBm, getBm, delBm, addBm, updateBm } from "@/api/hl/bm";
import addBmInfo from "./component/addBm.vue"
import healthSetting from "./component/healthSetting.vue"
import historyHealth from "./component/historyHealth.vue"
const { proxy } = getCurrentInstance();

const bmList = 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 dialogType = ref("add")
const modelId = ref(0)
const activeComponent = [healthSetting,historyHealth]
const currentActive=ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    modelCode: null,
    modelName: null,
    modelScore: null,
    modelType: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  },
  rules: {
  }
});

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

function handleHealthSet(row,type){
  modelId.value = row.modelId
  currentActive.value = type === "health" ? 0 : 1
  dialogType.value = type
  open.value = true
  title.value = type === "health" ? "健康度配置" :(row.modelName + ((type==="day"?"日":"历史")+"健康度"))
}



/** 查询业务模型列表 */
function getList() {
  loading.value = true;
  listBm(queryParams.value).then(response => {
    bmList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    modelId: null,
    modelCode: null,
    modelName: null,
    modelScore: null,
    modelType: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  // proxy.resetForm("bmRef");
}

/** 搜索按钮操作 */
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();
  dialogType.value ="add"
  open.value = true;
  title.value = "添加业务模型";
  proxy.$nextTick(() => {
    proxy.$refs["bmRef"].editRow(null)
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
    dialogType.value ="add"
    open.value = true;
    title.value = "修改业务模型";
    proxy.$nextTick(() => {
      proxy.$refs["bmRef"].editRow(row)
    })
}

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

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

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

getList();
</script>
