<template>
  <div class="dicomAE">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <!--AE Title-->
        <el-form-item label="AE Title" prop="CalledAE">
          <el-input
            v-model="searchData.CalledAE"
            style="width: 140px"
            clearable
          />
        </el-form-item>
        <!--IP-->
        <el-form-item label="IP" prop="IP">
          <el-input v-model="searchData.IP" style="width: 140px" clearable />
        </el-form-item>
        <!--Port-->
        <el-form-item label="Port" prop="Port">
          <el-input v-model="searchData.Port" style="width: 140px" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="getList"
            >搜索</el-button
          >
          <el-button type="primary" size="mini" @click="reset">重置</el-button>
          <el-button
            type="primary"
            size="mini"
            v-hasPermi="['system:dicom:add']"
            @click="openDialog('add')"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        v-adaptive="{ bottomOffset: 45 }"
        height="100"
        :data="list"
        class="table"
      >
        <!--AE Title-->
        <el-table-column
          label="AE Title"
          prop="CalledAE"
          min-width="120"
          show-overflow-tooltip
        />
        <!--IP-->
        <el-table-column
          label="IP"
          prop="IP"
          min-width="120"
          show-overflow-tooltip
        />
        <!--Port-->
        <el-table-column
          label="Port"
          prop="Port"
          min-width="120"
          show-overflow-tooltip
        />
        <!--Modality-->
        <el-table-column
          label="Modality"
          prop="Modality"
          min-width="120"
          show-overflow-tooltip
        />
        <!--Description-->
        <el-table-column
          label="Description"
          prop="Description"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="actions"
          fixed="right"
          prop="UserTypeShortName"
          min-width="200"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              v-hasPermi="['system:dicom:edit']"
              @click="openDialog('edit', scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-hasPermi="['system:dicom:del']"
              @click="delAE(scope.row)"
              >删除</el-button
            >
            <el-button size="mini" type="text" @click="test(scope.row)"
              >测试</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="text-align: right; margin-top: 5px">
        <pagination
          :total="total"
          :page.sync="searchData.PageIndex"
          :limit.sync="searchData.PageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <editDicom
      :visible.sync="editDicomVisible"
      :title="editDicomTitle"
      :dicom="DICOM"
      @getList="getList"
    />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import editDicom from "./components/edit-dicom";
import { getDicomAEList, deleteDiicomAE, testConnect } from "@/api/dicomAE.js";
export default {
  name: "dicomAE",
  components: { Pagination, editDicom },
  data() {
    return {
      // 查询
      searchData: {
        CalledAE: null,
        IP: null,
        Port: null,
        PageIndex: 1,
        PageSize: 10,
      },
      total: 0,
      // 列表
      loading: false,
      list: [],
      // 弹窗
      editDicomVisible: false,
      editDicomTitle: "添加",
      DICOM: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表
    async getList() {
      let data = {};
      Object.keys(this.searchData).forEach((key) => {
        data[key] = this.searchData[key];
      });
      try {
        this.loading = true;
        let res = await getDicomAEList(data);
        this.loading = false;
        if (res.IsSuccess) {
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
        }
      } catch (err) {
        console.log(err);
      }
    },
    // 重置
    reset() {
      Object.keys(this.searchData).forEach((key) => {
        this.searchData[key] = null;
      });
      this.searchData.PageIndex = 1;
      this.searchData.PageSize = 10;
      this.getList();
    },
    // 打开弹框
    openDialog(key, item = {}) {
      this.editDicomVisible = true;
      this.editDicomTitle = "编辑";
      if (key === "add") {
        this.editDicomTitle = "添加";
      }
      this.DICOM = JSON.parse(JSON.stringify(item));
    },
    // 删除
    async delAE(item) {
      try {
        let confirm = await this.$confirm(
          this.$t("trials:staffResearch:message:confirmDel"),
          this.$t("trials:uploadDicomList:label:prompt"),
          {
            confirmButtonText: this.$t("trials:reviewTrack:impactList:save"),
            cancelButtonText: this.$t("common:button:cancel"),
            type: "warning",
          }
        );
        if (confirm !== "confirm") return;
        let res = await deleteDiicomAE(item.Id);
        if (res.IsSuccess) {
          this.$message.success(
            this.$t("trials:crcUpload:message:deleteVisitSuccessfully")
          );
          this.getList();
        }
      } catch (err) {
        console.log(err);
      }
    },
    // 测试连通性
    async test(item) {
      let res = await testConnect(item.Id);
      if (res.IsSuccess && res.Result) {
        this.$message.success(this.$t("system:dicomAE:connect:success"));
      } else {
        this.$message.error(this.$t("system:dicomAE:connect:error"));
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.dicomAE {
  height: 100%;
  box-sizing: border-box;
  display: flex;
  padding: 10px;
  border-radius: 5px;
  .left {
    display: flex;
    flex-direction: column;
    width: 0;
    flex-grow: 4;
    // border-right: 1px solid #ccc;
    .filter-container {
      display: flex;
      align-items: center;
      margin: 5px;
    }
    .data-table {
      flex: 1;
      padding: 5px 0px;
    }
    .pagination-container {
      text-align: right;
    }
  }
  .right {
    width: 0;
    flex-grow: 6;
    overflow-y: auto;
    border-right: 1px solid #ccc;
  }
  .selected-row {
    background-color: cadetblue;
  }
}
</style>