<!-- 计算指标管理 -->
<template>
  <div class="page page-full model-detail-wrap">
    <div class="content">
      <div class="cont_box">
        <div class="title">模型标识：{{ detail.key }}</div>
        <div class="title" style="margin-left: 20px">模型表名：{{ detail.name }}</div>
        <div class="pro-status"><span :class="developer_status == 1 ? 'on' : 'off'"></span>{{ developer_status == 1 ? '已发布' : '未发布' }}</div>
        <!-- <div class="pro-option" v-auth="'startOrStop'" @click="CkOption">{{ developer_status == 1 ? '停用' : '发布' }}</div> -->
      </div>
    </div>

    <div class="content-box page page-full-part">
      <div class="wu-box">
        <div class="system-user-search mb15">
          <el-form :model="tableData.param" ref="queryRef" inline @submit.prevent @keyup.enter="typeList">
            <el-form-item label="指标标识" prop="name">
              <el-input v-model="tableData.param.keyWord" placeholder="请输入指标标识" clearable style="width: 220px" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" class="ml10" @click="typeList">
                <el-icon>
                  <ele-Search />
                </el-icon>
                查询
              </el-button>
              <el-button @click="resetQuery()">
                <el-icon>
                  <ele-Refresh />
                </el-icon>
                重置
              </el-button>
              <el-button type="primary" class="ml10" @click="onOpenAdd">
                <el-icon>
                  <ele-FolderAdd />
                </el-icon>
                新增计算指标模型
              </el-button>
              <el-button :loading="publishLoading" type="success" class="ml10" @click="onPublish()">
                <el-icon>
                  <ele-Upload />
                </el-icon>
                {{ publishStatus }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table :data="tableData.data" style="width: 100%" v-loading="tableData.loading">
          <el-table-column label="ID" align="center" prop="id" width="100" />
          <el-table-column label="指标名称" prop="name" width="140" show-overflow-tooltip />
          <el-table-column label="指标标识" prop="key" width="140" show-overflow-tooltip />
          <el-table-column label="计算公式" prop="formula" show-overflow-tooltip />
          <el-table-column label="数据类型" prop="types" width="85" show-overflow-tooltip />
          <el-table-column label="指标说明" prop="description" show-overflow-tooltip />
          <el-table-column prop="createdAt" label="创建时间" align="center" width="160" />
          <el-table-column label="操作" width="100" align="center" fixed="right">
            <template #default="scope">
              <el-button size="small" text type="warning" @click="onOpenEdit(scope.row)">修改</el-button>
              <el-button size="small" text type="danger" @click="onRowDel(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
					v-show="tableData.data.length > 0"
					:total="tableData.total"
					v-model:page="tableData.param.pageNum"
					v-model:limit="tableData.param.pageSize"
					@pagination="typeList"
				/>
      </div>
    </div>
    <AddOrEditIndictor ref="editDicRef" @typeList="updateList" />
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';
import AddOrEditIndictor from './component/addOrEditIndictor.vue';
import api from '/@/api/datahub';

const editDicRef = ref();
const route = useRoute();
const detail = ref<any>({});
const developer_status = ref(0);
const publishLoading = ref(false);
const publishStatus = ref('');
const tableData = ref(
  {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 20,
      tid: route.params.id as string,
      keyWord: '',
    },
  }
)

const typeList = () => {
  tableData.value.loading = true;
  api.calculationIndicator.getList(tableData.value.param).then((res: any) => {
    tableData.value.data = res.list;
    tableData.value.total = res.Total;
  }).finally(() => (tableData.value.loading = false));
};

const resetQuery = () => {
  tableData.value.param.keyWord = ''
  typeList();
};

const onRowDel = (row: any) => {
  let msg = '你确定要删除所选数据？';
  let ids: number[] = [];
  if (row) {
    msg = `此操作将永久删除数据节点：“${row.name}”，是否继续?`;
    ids = row.id;
  } else {
    // ids = ids;
  }
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的数据。');
    return;
  }
  ElMessageBox.confirm(msg, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      api.calculationIndicator.delete(ids).then(() => {
        ElMessage.success('删除成功');
        typeList();
      });
    })
    .catch(() => { });
};

const onPublish = () => {
  ElMessageBox.confirm(`是否${publishStatus.value}？`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    publishLoading.value = true;
    api.calculationIndicator.deploy({ dataTemplateKey: detail.value.key }).then(() => {
      ElMessage.success('操作成功');
      checkDeploy();
      typeList();
    }).finally(() => (publishLoading.value = false));
  })
  .catch(() => { })
  

};

const checkDeploy = () => {
  const ids = route.params?.modelId as string;
  api.calculationIndicator.checkDeploy({ dataTemplateKey: ids }).then((res: any) => {
    if (res === true) {
      publishStatus.value = '取消发布';
    }else if (res === false) {
      publishStatus.value = '发布';
    }
  })
};

// 打开修改数据源弹窗
const onOpenEdit = (row: any) => {
  editDicRef.value.openDialog(row);
};

const onOpenAdd = () => {
  editDicRef.value.openDialog(null, detail.value.key);
};

// const CkOption = () => {
//   //检测是否需要设置关联
//   api.template.relation_check(route.params.id).then((res: any) => {
//     if (res.yes && developer_status.value == 0) {
//       let ids = {
//         id: route.params.id,
//       }
//       relationRef.value.openDialog(ids);
//     } else {
//       if (developer_status.value == 1) {
//         api.tnode.undeploy({ id: route.params.id }).then((res: any) => {
//           ElMessage.success('操作成功');
//           developer_status.value = 0;
//         });
//       } else {
//         api.tnode.deploy({ id: route.params.id }).then((res: any) => {
//           ElMessage.success('操作成功');
//           developer_status.value = 1;
//         });
//       }
//     }
//   });
// };

const updateList = () => {
  tableData.value.param.pageNum = 1;
  tableData.value.param.keyWord = '';
  typeList();
}

onMounted(() => {
  const ids = route.params?.modelId as string;
  api.template.detail(ids).then((res: any) => {
    detail.value = res.data;
    developer_status.value = res.data.status;
  });
  checkDeploy()
  typeList();
});
</script>

<style lang="scss" scoped>
.content {
  background: #fff;
  width: 100%;
  padding: 20px;
}

.content-box {
  background: #fff;
  width: 100%;
  padding: 20px;
  margin-top: 15px;
}

.cont_box {
  display: flex;
}

.cont_box .title {
  font-size: 18px;
}

.cont_box .pro-status {
  line-height: 30px;
  margin-left: 30px;
}

.cont_box .pro-status .on {
  background: #52c41a;
}

.cont_box .pro-status .off {
  background: #c41a1a;
}

.cont_box .pro-status span {
  position: relative;
  top: -1px;
  display: inline-block;
  width: 6px;
  height: 6px;
  vertical-align: middle;
  border-radius: 50%;
  margin-right: 5px;
}

.cont_box .pro-option {
  line-height: 30px;
  margin-left: 10px;
  color: #1890ff;
  cursor: pointer;
}

.content-box .pro-box {
  display: flex;
  padding: 10px;
}

.content-box .pro-box .protitle {
  font-size: 18px;
  font-weight: bold;
  line-height: 35px;
}

.content-box .pro-box .buttonedit {
  border: 0px;
  color: #1890ff;
}
</style>
