<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <el-form-item :label="$t('dictionary:browser:search:title')">
          <el-input v-model="searchData.Title" style="width: 100px" />
        </el-form-item>
        <el-form-item :label="$t('dictionary:browser:search:version')">
          <el-input v-model="searchData.Version" style="width: 100px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">{{
            $t("dictionary:browser:button:reset")
          }}</el-button>
          <el-button type="primary" @click="handleSearch">{{
            $t("dictionary:browser:button:search")
          }}</el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left: auto">
        <el-button type="primary" size="mini" @click="handleAdd">
          {{ $t("dictionary:browser:button:add") }}
        </el-button>
      </span>
    </div>

    <!-- 浏览器推荐版本列表 -->
    <el-table
      v-loading="loading"
      v-adaptive="{ bottomOffset: 45 }"
      :data="list"
      stripe
      height="100"
      style="width: 100%"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="40" />

      <el-table-column
        prop="Title"
        :label="$t('dictionary:browser:table:title')"
        sortable="custom"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ExploreType"
        :label="$t('dictionary:browser:table:exploreType')"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="Version"
        :label="$t('dictionary:browser:table:version')"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="DownloadUrl"
        :label="$t('dictionary:browser:table:path')"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="FileName"
        :label="$t('dictionary:browser:table:fileName')"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
          <a
            :href="OSSclientConfig.basePath + scope.row.Path"
            target="_blank"
            style="color: #428bca"
          >
            {{ scope.row.FileName }}
          </a>
        </template>
      </el-table-column>
      <el-table-column
        prop="IsDeleted"
        :label="$t('dictionary:browser:table:IsDeleted')"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.IsDeleted" type="danger">{{
            $fd("IsSiteDisable", scope.row.IsDeleted)
          }}</el-tag>
          <el-tag v-else>{{
            $fd("IsSiteDisable", scope.row.IsDeleted)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="UpdateTime"
        :label="$t('dictionary:browser:table:updateTime')"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        :label="$t('dictionary:browser:table:action')"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">
            {{ $t("dictionary:browser:button:edit") }}
          </el-button>
          <el-button type="text" @click="handleDelete(scope.row)">
            {{ $t("dictionary:browser: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"
    />
    <browser-form
      :config="config"
      :data="rowData"
      v-if="config.visible"
      @close="close"
      @getList="getList"
    />
  </box-content>
</template>
<script>
import {
  getExploreRecommendList,
  deleteExploreRecommend,
} from "@/api/dictionary";
import Pagination from "@/components/Pagination";
import BoxContent from "@/components/BoxContent";
import browserForm from "./form.vue";
const searchDataDefault = () => {
  return {
    Version: null,
    Title: null,
    DownloadUrl: null,
    FileName: null,
    IsDeleted: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: null,
  };
};
export default {
  name: "browser",
  components: { BoxContent, Pagination, browserForm },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      rowData: {},
      config: {
        visible: false,
        showClose: true,
        width: "600px",
        title: "",
        appendToBody: true,
        status: "add",
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      try {
        this.loading = true;
        let res = await getExploreRecommendList(this.searchData);
        if (res.IsSuccess) {
          this.loading = false;
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
        }
      } catch (err) {
        console.log(err);
        this.loading = false;
      }
    },
    // 新增
    handleAdd() {
      this.rowData = {};
      this.config.title = this.$t("dictionary:browser:form:title:add");
      this.config.status = "add";
      this.config.visible = true;
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row };
      this.config.title = this.$t("dictionary:browser:form:title:edit");
      this.config.status = "edit";
      this.config.visible = true;
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t("dictionary:browser:message:deleteMessage"), {
        type: "warning",
        distinguishCancelAndClose: true,
      }).then(() => {
        deleteExploreRecommend(row.Id).then((res) => {
          if (res.IsSuccess) {
            this.list.splice(
              this.list.findIndex((item) => item.Id === row.Id),
              1
            );
            this.$message.success(
              this.$t("common:message:deletedSuccessfully")
            );
          }
        });
      });
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1;
      this.getList();
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault();
      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.config.visible = false;
    },
  },
};
</script>
