<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="appName" style="margin-right:10px">
            <el-input
                v-model="queryParams.appName"
                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:app:add']"
        >新增</el-button>
        <el-button type="info" plain icon="Download">下载agent</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="appList" border>
      <el-table-column label="应用ID" align="center" prop="appId" width="80" />
      <el-table-column label="应用名称" align="left" prop="appName"  width="300"/>
      <el-table-column label="应用编码" align="left" prop="appCode"  width="180" />
      <el-table-column label="IP地址" align="left" width="200">
        <template #default="scope">
            {{scope.row.appAddr}}:{{scope.row.appPort}}
        </template>
      </el-table-column>
      <el-table-column label="pinpoint启动参数">
        <template #default="scope">
          <div v-html="outConfigKey(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" prop="remark"  width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['obj:app:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['obj:app: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="500px" append-to-body>
      <el-form ref="appRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="应用编码" prop="appCode">
          <el-input v-model="form.appCode" placeholder="请输入应用编码" />
        </el-form-item>
        <el-form-item label="IP地址" prop="appAddr">
          <el-input v-model="form.appAddr" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="端口号" prop="appPort">
          <el-input v-model="form.appPort" placeholder="请输入端口号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" show-word-limit :maxlength="300" :autosize="{maxRows: 6, minRows: 4}"/>
        </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>
  </div>
</template>

<script setup name="App">
import { listApp, getApp, delApp, addApp, updateApp } from "@/api/obj/app";
const { proxy } = getCurrentInstance();
const appList = 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 daterangeCreateTime = ref([]);
const daterangeUpdateTime = ref([]);
const configKey=ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    appName: null,
    appCode: null,
    appAddr: null,
    appPort: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  },
  rules: {
    appName: [
      { required: true, message: "应用名称不能为空", trigger: "blur" }
    ],
    appCode: [
      { required: true, message: "应用编码不能为空", trigger: "blur" }
    ],
    appAddr: [
      { required: true, message: "IP地址不能为空", trigger: "blur" }
    ],
    appPort: [
      { required: true, message: "端口号不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询应用程序管理列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listApp(queryParams.value).then(response => {
    appList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    appId: null,
    appName: null,
    appCode: null,
    appAddr: null,
    appPort: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("appRef");
}

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

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

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

/**获取配置**/


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加应用程序管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _appId = row.appId || ids.value
  getApp(_appId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改应用程序管理";
  });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _appIds = row.appId || ids.value;
  proxy.$modal.confirm('是否确认删除应用程序管理编号为"' + _appIds + '"的数据项？').then(function() {
    return delApp(_appIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

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

function outConfigKey(item){
  return configKey.value.replace(/\$\{code}/g, item.appCode);
}

function handleConfigKey() {
    proxy.getConfigKey("pp.start").then(response => {
      configKey.value = response.msg.replace(/\n/g, '<br/>')
    })
}

getList();
handleConfigKey();
</script>
