<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"
                 @submit.native.prevent>
          <el-form-item label="" prop="objName" style="margin-right:10px">
            <el-input
                v-model="queryParams.objName"
                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="['obj:obj:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="objList" border>
      <el-table-column label="对象ID" align="center" prop="objId" width="80"/>
      <el-table-column label="对象名称" align="left" prop="objName"/>
      <el-table-column label="业务类型" align="left" prop="objType" width="120">
        <template #default="scope">
          <dict-tag :options="biz_type" :value="scope.row.objType"/>
        </template>
      </el-table-column>
      <el-table-column label="对象地址" align="left" width="200">
        <template #default="scope">
          {{ scope.row.objAddr }}:{{ scope.row.objPort }}
        </template>
      </el-table-column>
      <el-table-column label="应用模板" align="left" width="260">
        <template #default="scope">
          <span v-for="(item,index) in scope.row.tplList" :key="`ip_${item.objId}`">
            {{item.tplName}}<span v-if="index!==scope.row.tplList.length-1">,</span>
          </span>
        </template>
      </el-table-column>
<!--      <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="操作" align="center" class-name="small-padding fixed-width" width="260">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['obj:obj:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Position" @click="handleRunSearch(scope.row)">运行指标查询</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['obj:obj: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="addDialogType? 1000 : 1200" append-to-body>
      <add-component ref="addRef" :rowId.sync="rowId" v-if="open && addDialogType" @changeRowId="v => rowId = v" @handleDone="open = false"/>
      <run-metrics-info v-else :rowId.sync="rowId"/>
      <template #footer v-if="false">
        <div class="dialog-footer" v-show="addDialogType">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Obj">
import {addObj, delObj, listObj, updateObj} from "@/api/obj/obj";
import addComponent from "./component/addComponent.vue"
import runMetricsInfo from "./component/runMetricsInfo.vue"

const {proxy} = getCurrentInstance();
const {biz_type} = proxy.useDict('biz_type');

const objList = 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 daterangeUpdateTime = ref([]);
const addDialogType = ref(true)
const addRef=ref(null)

const rowId = ref(null)
const dialogComponent = ref(null)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    objType: null,
    appId: null,
    objName: null,
    objAddr: null,
    objPort: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
});

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



/** 查询业务对象列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listObj(queryParams.value).then(response => {
    objList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleRunSearch(row) {
  rowId.value = row.objId || ids.value
  open.value = true;
  title.value = "运行指标查询";
  addDialogType.value = false

}

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

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

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

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

/** 新增按钮操作 */
function handleAdd(vTitle) {
  addDialogType.value = true
  rowId.value = null;
  open.value = true;
  title.value = vTitle || "添加业务对象";
  nextTick(()=>{
    addRef.value.reset()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  this.handleAdd("修改业务对象")
  rowId.value = row.objId || ids.value
}

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

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

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

getList();
</script>
