<template>
  <box-content>
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <!-- Site Name -->
        <el-form-item :label="$t('institutions:sites:label:siteName')">
          <el-input v-model="searchData.SiteName" style="width: 120px" />
        </el-form-item>
        <!-- Alias Name -->
        <el-form-item :label="$t('institutions:sites:label:aliasName')">
          <el-input v-model="searchData.AliasName" style="width: 120px" />
        </el-form-item>
        <!-- Country -->
        <el-form-item :label="$t('institutions:sites:label:country')">
          <el-input v-model="searchData.Country" style="width: 120px" />
        </el-form-item>
        <!-- City -->
        <el-form-item :label="$t('institutions:sites:label:city')">
          <el-input v-model="searchData.City" style="width: 120px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
      <div style="margin-left: auto">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAddSystemSite"
          >{{ $t("trial:dictionary:institutions:site:addSystemSite") }}
        </el-button>
        <el-button
          type="primary"
          style="margin-left: 10px"
          @click="handleAddSite"
        >
          {{ $t('common:button:new') }}
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="loading"
      v-adaptive="{ bottomOffset: 60 }"
      :data="list"
      stripe
      height="100"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="40" />
      <!-- Site Code -->
      <el-table-column
        prop="SiteCode"
        :label="$t('institutions:sites:label:siteCode')"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Site Name -->
      <el-table-column
        prop="SiteName"
        :label="$t('institutions:sites:label:siteName')"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="SiteNameCN"
        :label="$t('institutions:sites:label:siteNameCN')"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      
      <!-- Alias Name -->
      <el-table-column
        prop="AliasName"
        :label="$t('institutions:sites:label:aliasName')"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- <el-table-column
        prop="UniqueCode"
        label="组织机构代码"
        min-width="130"
        show-overflow-tooltip
        sortable="custom"
      /> -->
      <!-- Country -->
      <el-table-column
        prop="Country"
        :label="$t('institutions:sites:label:country')"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- City -->
      <el-table-column
        prop="City"
        :label="$t('institutions:sites:label:city')"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Address -->
      <el-table-column
        prop="Address"
        :label="$t('institutions:sites:label:address')"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Affiliated Hospital -->
      <el-table-column
        prop="HospitalName"
        :label="$t('institutions:sites:label:affiliatedHospital')"
        min-width="180"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Director Name -->
      <el-table-column
        prop="DirectorName"
        :label="$t('institutions:sites:label:directorName')"
        min-width="160"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Director Phone -->
      <el-table-column
        prop="DirectorPhone"
        :label="$t('institutions:sites:label:directorPhone')"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Contact Name -->
      <el-table-column
        prop="ContactName"
        :label="$t('institutions:sites:label:contactName')"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Contact Phone -->
      <el-table-column
        prop="ContactPhone"
        :label="$t('institutions:sites:label:contactPhone')"
        min-width="160"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- CreateTime -->
      <el-table-column
        prop="CreateTime"
        :label="$t('institutions:sites:label:createTime')"
        min-width="160"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column :label="$t('common:action:action')" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            {{ $t('common:button:edit') }}
          </el-button>   
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
          >
            {{ $t('common:button:delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      class="page"
      :total="total"
      :page.sync="searchData.PageIndex"
      :limit.sync="searchData.PageSize"
      @pagination="getList"
    />
    <el-dialog
      v-if="editVisible"
      :visible.sync="editVisible"
      :close-on-click-modal="false"
      :title="title"
      width="800px"
      custom-class="base-dialog-wrapper"
    >
      <site-form
        v-if="editVisible"
        :data="rowData"
        @close="close"
        @getList="getList"
      />
    </el-dialog>
    <base-model
      v-if="addSystemSite_model.visible"
      :config="addSystemSite_model"
    >
      <template slot="dialog-body">
        <div class="search">
          <el-form :inline="true" size="small" class="base-search-form">
            <el-form-item
              :label="
                $t(
                  'trial:dictionary:institutions:site:table:addSystemSite:TrialCode'
                )
              "
            >
              <el-input
                v-model="addSystemSiteSearchData.TrialCode"
                style="width: 100px"
              />
            </el-form-item>
            <el-form-item
              :label="
                $t(
                  'trial:dictionary:institutions:site:table:addSystemSite:TrialSiteName'
                )
              "
            >
              <el-input
                v-model="addSystemSiteSearchData.TrialSiteName"
                style="width: 100px"
              />
            </el-form-item>
            <el-form-item
              :label="
                $t(
                  'trial:dictionary:institutions:site:table:addSystemSite:TrialSiteAliasName'
                )
              "
            >
              <el-input
                v-model="addSystemSiteSearchData.TrialSiteAliasName"
                style="width: 100px"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="getTrialSiteList"
                >{{ $t("trial:dictionary:institutions:site:button:sreach") }}
              </el-button>
              <el-button
                type="primary"
                icon="el-icon-refresh-left"
                @click="handleResetAddSite"
                >{{ $t("trial:dictionary:institutions:site:button:reset") }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table
          v-loading="addSystemSiteLoading"
          :data="addSystemSiteList"
          stripe
          height="300"
          @sort-change="handleSortByColumnAddSite"
        >
          <el-table-column type="index" width="40" />
          <el-table-column
            prop="TrialCode"
            :label="
              $t(
                'trial:dictionary:institutions:site:table:addSystemSite:TrialCode'
              )
            "
            show-overflow-tooltip
            sortable="custom"
          />
          <el-table-column
            prop="TrialSiteName"
            :label="
              $t(
                'trial:dictionary:institutions:site:table:addSystemSite:TrialSiteName'
              )
            "
            show-overflow-tooltip
            sortable="custom"
          />
          <el-table-column
            prop="TrialSiteAliasName"
            :label="
              $t(
                'trial:dictionary:institutions:site:table:addSystemSite:TrialSiteAliasName'
              )
            "
            show-overflow-tooltip
            sortable="custom"
          />
          <el-table-column
            :label="
              $t(
                'trial:dictionary:institutions:site:table:addSystemSite:Action'
              )
            "
            fixed="right"
            width="80"
          >
            <template slot-scope="scope">
              <el-button
                circle
                type="text"
                :title="$t('trial:dictionary:institutions:site:button:add')"
                @click="addSystemSite(scope.row)"
                >{{
                  $t("trial:dictionary:institutions:site:button:add")
                }}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
          class="page"
          style="float: right"
          :total="addSystemSiteSearchDataTotal"
          :page.sync="addSystemSiteSearchData.PageIndex"
          :limit.sync="addSystemSiteSearchData.PageSize"
          @pagination="getTrialSiteList"
        />
      </template>
    </base-model>
  </box-content>
</template>
<script>
import {
  getSiteList,
  deleteSite,
  addOrUpdateSite,
  getTrialSiteList,
} from "@/api/dictionary";
import BaseModel from "@/components/BaseModel";
import BoxContent from "@/components/BoxContent";
import Pagination from "@/components/Pagination";
import SiteForm from "./SiteForm";
const searchDataDefault = () => {
  return {
    SiteName: "",
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: "CreateTime",
    AliasName: "",
    Country: null,
    City: null,
  };
};
const searchSiteDataDefault = () => {
  return {
    // SiteName: "",
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: "",
    TrialCode: null,
    TrialSiteName: null,
    TrialSiteAliasName: null,
  };
};
export default {
  name: "Sites",
  components: { BoxContent, Pagination, SiteForm, BaseModel },
  data() {
    return {
      editVisible: false,
      title: this.$t('common:button:new'),
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      addSystemSite_model: {
        visible: false,
        title: this.$t("trial:dictionary:institutions:site:addSystemSite"),
        width: "800px",
        appendToBody: true,
      },
      addSystemSiteLoading: false,
      addSystemSiteList: [],
      addSystemSiteSearchData: searchSiteDataDefault(),
      addSystemSiteSearchDataTotal: 0,
    };
  },
  mounted() {
    this.getList();
    this.getTrialSiteList();
  },
  methods: {
    // 获取项目级别列表
    async getTrialSiteList() {
      try {
        this.addSystemSiteLoading = true;
        let res = await getTrialSiteList(this.addSystemSiteSearchData);
        if (res.IsSuccess) {
          this.addSystemSiteLoading = false;
          this.addSystemSiteList = res.Result.CurrentPageData;
          this.addSystemSiteSearchDataTotal = res.Result.TotalCount;
        }
      } catch (err) {
        this.addSystemSiteLoading = false;
        console.log(err);
      }
    },
    // 将项目级别中心转换为系统级别中心
    async addSystemSite(item) {
      try {
        let data = {
          SiteName: item.TrialSiteName,
          SiteNameCN: item.TrialSiteName,
          AliasName: item.TrialSiteAliasName,
          SiteCode: item.TrialCode,
          TrialSiteId: item.TrialSiteId,
        };
        this.addSystemSiteLoading = true;
        let res = await addOrUpdateSite(data);
        if (res.IsSuccess) {
          this.getTrialSiteList();
          this.getList();
        }
      } catch (err) {
        this.addSystemSiteLoading = false;
        console.log(err);
      }
    },
    handleAddSystemSite() {
      this.addSystemSite_model.visible = true;
      this.addSystemSiteSearchData = searchSiteDataDefault();
      this.addSystemSiteList = [];
      this.addSystemSiteSearchDataTotal = 0;
      this.getTrialSiteList();
    },
    // 获取Site列表信息
    getList() {
      this.loading = true;
      getSiteList(this.searchData)
        .then((res) => {
          this.loading = false;
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 新增Site信息
    handleAddSite() {
      this.rowData = {};
      this.title = this.$t('common:button:new');
      this.editVisible = true;
    },
    // 编辑Site信息
    handleEdit(row) {
      this.rowData = row;
      this.title = this.$t('common:action:edit');
      this.editVisible = true;
    },
    // 删除Site
    handleDelete(row) {
      this.$confirm(this.$t("trials:uploadedDicoms:message:deleteMes"), {
        type: "warning",
        distinguishCancelAndClose: true,
      }).then(() => {
        this.loading = true;
        deleteSite(row.Id)
          .then((res) => {
            this.loading = false;
            if (res.IsSuccess) {
              this.list.splice(
                this.list.findIndex((item) => item.Id === row.Id),
                1
              );
              this.$store.dispatch("global/setSite", {});
              this.$message.success(
                this.$t("common:message:deletedSuccessfully")
              );
            }
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    // 重置列表
    handleReset() {
      this.searchData = searchDataDefault();
      this.getList();
    },
    handleResetAddSite() {
      this.addSystemSiteSearchData = searchSiteDataDefault();
      this.getTrialSiteList();
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1;
      this.getList();
    },
    // 指定排序字段，对列表进行排序
    handleSortByColumn(column) {
      if (column.order === "ascending") {
        this.searchData.Asc = true;
      } else {
        this.searchData.Asc = false;
      }
      this.searchData.SortField = column.prop;
      this.searchData.PageIndex = 1;
      this.getList();
    },
    handleSortByColumnAddSite(column) {
      if (column.order === "ascending") {
        this.addSystemSiteSearchData.Asc = true;
      } else {
        this.addSystemSiteSearchData.Asc = false;
      }
      this.addSystemSiteSearchData.SortField = column.prop;
      this.addSystemSiteSearchData.PageIndex = 1;
      this.getTrialSiteList();
    },
    // 关闭模态框
    close() {
      this.editVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.Sites {
  height: 100%;
}
</style>
