<template>
  <box-content>
    <div class="search">
      <el-form :inline="true" class="base-search-form" @submit.native.prevent="">
        <!-- Sponsor Name -->
        <el-form-item :label="$t('institutions:sponsors:label:sponsorName')">
          <el-input v-model="searchData.SponsorName" style="width: 100px" />
        </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>
      <span style="margin-left: auto">
        <el-button
          type="primary"
          style="margin-left: auto"
          @click="handleAddSponsor"
          >{{ $t('common:button:new') }}</el-button
        >
      </span>
    </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" />
      <!-- Sponsor Code -->
      <el-table-column
        prop="SponsorCode"
        :label="$t('institutions:sponsors:label:sponsorCode')"
        min-width="100"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Sponsor Name -->
      <el-table-column
        prop="SponsorName"
        :label="$t('institutions:sponsors:label:sponsorName')"
        min-width="100"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Sponsor NameCN -->
      <el-table-column
        prop="SponsorNameCN"
        :label="$t('institutions:sponsors:label:sponsorNameCN')"
        min-width="100"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Level -->
      <el-table-column
        prop="IsTrialLevel"
        :label="$t('institutions:sponsors:label:level')"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
          <span>{{ $fd("IsTrialLevel", String(scope.row.IsTrialLevel)) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common:action:action')" min-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="600px"
      custom-class="base-dialog-wrapper"
    >
      <sponsor-form
        v-if="editVisible"
        :IsTrialLevel="rowData.IsTrialLevel"
        :data="rowData"
        @close="close"
        @getList="getList"
      />
    </el-dialog>
  </box-content>
</template>
<script>
import { getSponsorPageList, deleteSponsor } from "@/api/dictionary";
import BoxContent from "@/components/BoxContent";
import Pagination from "@/components/Pagination";
import SponsorForm from "./SponsorForm";
const searchDataDefault = () => {
  return {
    SponsorName: "",
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: "",
  };
};
export default {
  name: "Sponsors",
  components: { BoxContent, Pagination, SponsorForm },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      editVisible: false,
      title: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取Sponsors列表
    getList() {
      this.loading = true;
      getSponsorPageList(this.searchData)
        .then((res) => {
          this.loading = false;
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 新增Sponsor
    handleAddSponsor() {
      this.rowData = {};
      this.title = this.$t('common:button:new');
      this.editVisible = true;
    },
    // 编辑Sponsor
    handleEdit(row) {
      this.rowData = row;
      this.title = this.$t('common:action:edit');
      this.editVisible = true;
    },
    // 删除Sponsor
    handleDelete(row) {
      this.$confirm(this.$t("trials:uploadedDicoms:message:deleteMes"), {
        type: "warning",
        distinguishCancelAndClose: true,
      }).then(() => {
        this.loading = true;
        deleteSponsor(row.Id)
          .then((res) => {
            this.loading = false;
            if (res.IsSuccess) {
              this.list.splice(
                this.list.findIndex((item) => item.Id === row.Id),
                1
              );
              this.$message.success(
                this.$t("common:message:deletedSuccessfully")
              );
            }
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    // 重置列表
    handleReset() {
      this.searchData = searchDataDefault();
      this.getList();
    },
    // 查询
    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();
    },
    // 关闭模态框
    close() {
      this.editVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.sponsors {
  height: 100%;
}
</style>
