<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" class="my-custom-form">
      <el-row>
        <el-col :span="4">
          <el-form-item label="用户名" prop="userName" label-width="auto">
            <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="密码" prop="passWord" label-width="auto">
            <el-input v-model="queryParams.passWord" placeholder="请输入密码" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="IP地址" prop="restUrl" label-width="auto">
            <el-input v-model="queryParams.restUrl" placeholder="请输入IP地址" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="端口号" prop="restPort" label-width="auto">
            <el-input v-model="queryParams.restPort" placeholder="请输入端口号" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="是否可用" prop="enabled" label-width="auto">
            <el-select v-model="queryParams.enabled" placeholder="请选择是否可用" clearable>
              <el-option v-for="dict in enabled" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="服务名" prop="seviceName" label-width="auto">
            <el-input v-model="queryParams.seviceName" placeholder="请输入服务名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="备注" prop="note" label-width="auto">
            <el-input v-model="queryParams.note" placeholder="请输入备注" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="服务类型" prop="seviceType" label-width="auto">
            <el-select v-model="queryParams.seviceType" placeholder="请选择服务类型" clearable>
              <el-option v-for="dict in dc_collectserver_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item class="childTextAlignLeft">
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button type="info" plain icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="Edit" @click="handleUpdate">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="success" icon="Delete" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tbserverinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="密码" align="center" prop="passWord" />
      <el-table-column label="IP地址" align="center" prop="restUrl" />
      <el-table-column label="端口号" align="center" prop="restPort" />
      <el-table-column label="是否可用" align="center" prop="enabled" >
        <template #default="scope">
          <dict-tag :options="enabledTable" :value="scope.row.enabled" />
        </template>
      </el-table-column>
      <el-table-column label="服务名" align="center" prop="seviceName" />
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="服务类型" align="center" prop="seviceType" >
        <template #default="scope">
          <dict-tag :options="dc_collectserver_type" :value="scope.row.seviceType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)">删除</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" />

    <!-- 添加或修改TB服务信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body draggable>
      <el-form ref="tbserverinfoRef" :model="form" :rules="rules" class="my-custom-form">

        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="userName" label-width="auto">
              <el-input v-model="form.userName" placeholder="请输入用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="passWord" label-width="auto">
              <el-input v-model="form.passWord" placeholder="请输入密码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP地址" prop="restUrl" label-width="auto">
              <el-input v-model="form.restUrl" placeholder="请输入IP地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口号" prop="restPort" label-width="auto">
              <el-input v-model="form.restPort" placeholder="请输入端口号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否可用" prop="enabled" label-width="auto">
              <el-select v-model="form.enabled" placeholder="请选择是否可用">
                <el-option v-for="dict in enabled" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务名" prop="seviceName" label-width="auto">
              <el-input v-model="form.seviceName" placeholder="请输入服务名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="note" label-width="auto">
              <el-input v-model="form.note" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务类型" prop="seviceType" label-width="auto">
              <el-select v-model="form.seviceType" placeholder="请选择服务类型">
                <el-option v-for="dict in dc_collectserver_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
      </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>
<style lang="scss" > 
  @import "@/assets/styles/self-defined.scss";
</style>
<script setup name="Tbserverinfo">
import { listTbserverinfo, getTbserverinfo, delTbserverinfo, addTbserverinfo, updateTbserverinfo } from "@/api/model/tbserverinfo";

const { proxy } = getCurrentInstance();
const { dc_collectserver_type } = proxy.useDict('dc_collectserver_type');
const tbserverinfoList = 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 enabled = [
  {
    value: true,
    label: '是'
  },
  {
    value: false,
    label: '否'
  }
];
const enabledTable = [
  {
    value: 'true',
    label: '是'
  },
  {
    value: 'false',
    label: '否'
  }
];

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    passWord: null,
    restUrl: null,
    restPort: null,
    enabled: true,
    seviceName: null,
    note: null,
    seviceType: null
  },
  rules: {
  }
});

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

/** 查询TB服务信息列表 */
function getList() {
  loading.value = true;
  listTbserverinfo(queryParams.value).then(response => {
    tbserverinfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    userName: null,
    passWord: null,
    restUrl: null,
    restPort: null,
    enabled: null,
    seviceName: null,
    note: null,
    id: null,
    seviceType: null
  };
  proxy.resetForm("tbserverinfoRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加TB服务信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTbserverinfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改TB服务信息";
  });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除TB服务信息编号为"' + _ids + '"的数据项？').then(function () {
    return delTbserverinfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

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

getList();
</script>
