<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <div class="filter-container">
      <!-- 系统选择框 -->
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.startPage.filter.system')"
        style="width:120px;"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatforms"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span style="float: left;">
            <sys-origin-icon :icon="item.value" :desc="item.value"
          /></span>
          <span style="float: left;margin-left:10px">{{ item.label }}</span>
        </el-option>
      </el-select>

      <!-- 状态选择框 -->
      <el-select
        v-model="listQuery.showcase"
        :placeholder="$t('pages.startPage.filter.status')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in showcaseOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <!-- 活动类型选择框 -->
      <el-select
        v-model="listQuery.type"
        :placeholder="$t('pages.startPage.filter.activityType')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in planTypeOptions"
          :key="item.value"
          :label="item.displayName"
          :value="item.value"
        >
          <span>{{ item.displayName }}</span>
        </el-option>
      </el-select>

      <!-- 操作按钮 -->
      <!-- 搜索 按钮 -->
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t("pages.startPage.action.search") }}
      </el-button>
      <!-- 新增 按钮 -->
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        {{ $t("pages.startPage.action.add") }}
      </el-button>
    </div>

    <!-- 表单 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.startPage.loading')"
      fit
      highlight-current-row
      @cell-mouse-enter="handleMouseEnter"
    >
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column
        :label="$t('pages.startPage.table.system')"
        align="center"
      >
        <template slot-scope="scope">
          <sys-origin-icon
            :icon="scope.row.sysOrigin"
            :desc="scope.row.sysOrigin"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('pages.startPage.table.activityType')"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ getPlanTypeLabel(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('pages.startPage.table.cover')"
        align="center"
        width="400"
      >
        <template slot-scope="scope">
          <el-image
            :lazy="true"
            style="width: 351px; height: 111px"
            :src="scope.row.cover"
            :preview-src-list="[scope.row.cover]"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="expireTime"
        :label="$t('pages.startPage.table.expireTime')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.expireTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        :label="$t('pages.startPage.table.createdAt')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('pages.startPage.table.actions')"
        align="center"
        width="200"
      >
        <template slot-scope="scope">
          <el-button type="text" @click.native="handleUpdate()">{{
            $t("pages.startPage.action.edit")
          }}</el-button>
          <el-button type="text" @click.native="handlDel(scope.row)"
            >{{ $t("pages.startPage.action.delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页模块 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <!-- 新增、修改弹窗 -->
    <form-edit
      v-if="formEditVisible"
      :row="thatRow"
      @close="formEditVisible = false"
      @success="formEditSuccess"
    />
  </div>
</template>

<script>
import { pageData, deleteData } from "@/api/start-page";
import Pagination from "@/components/Pagination";
import { sysOriginPlatforms } from "@/constant/origin";
import { appStartPagePlanTypes } from "@/constant/type";
import FormEdit from "./form-edit.vue";
import { mapGetters } from "vuex";

export default {
  name: "StartPage",
  components: { Pagination, FormEdit },
  data() {
    return {
      thatRow: {},
      sysOriginPlatforms,
      appStartPagePlanTypes,
      txtVal: 0,
      activeGiftId: "",
      pushTextHistoryLoading: false,
      pushTextHistoryVisible: false,
      pushTextHistory: [],
      list: [],
      delarr: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        showcase: 1,
        sysOrigin: "TIM_CHAT",
        type: "KING_GAMES"
      },
      formEditVisible: false,
      textOptTitle: "",
      listLoading: true
    };
  },
  computed: {
    ...mapGetters(["permissionsSysOriginPlatforms"]),
    planTypeTextMap() {
      return {
        KING_GAMES: this.$t("pages.startPage.planType.kingGames"),
        CP: this.$t("pages.startPage.planType.cp")
      };
    },
    planTypeOptions() {
      return this.appStartPagePlanTypes.map(item => ({
        ...item,
        displayName: this.planTypeTextMap[item.value] || item.name
      }));
    },
    showcaseOptions() {
      return [
        {
          label: this.$t("pages.startPage.status.normal"),
          value: 1
        },
        {
          label: this.$t("pages.startPage.status.expired"),
          value: 0
        }
      ];
    }
  },
  created() {
    const that = this;
    const querySystem = this.permissionsSysOriginPlatforms[0];
    if (!querySystem) {
      return;
    }
    that.listQuery.sysOrigin = querySystem.value;
    that.renderData();
  },
  methods: {
    renderData(isClean) {
      const that = this;
      if (isClean === true) {
        this.listQuery.cursor = 1;
        this.listQuery.list = [];
      }
      that.listLoading = true;
      pageData(that.listQuery).then(res => {
        const { body } = res;
        that.total = body.total || 0;
        that.list = body.records;
        that.listLoading = false;
      });
    },
    handleSearch() {
      this.renderData(true);
    },
    renderDataSuccess() {
      this.$message({
        message: this.$t("pages.startPage.message.success"),
        type: "success"
      });
      this.renderData();
    },
    queryUserDetails(row) {
      this.userDeatilsDrawer = true;
      this.thatSelectedUserId = row.id;
    },
    // 删除
    handlDel(row) {
      this.$confirm(
        this.$t("pages.startPage.confirm.deleteMessage"),
        this.$t("pages.startPage.confirm.title"),
        {
          type: "warning"
        }
      )
        .then(() => {
          this.listLoading = true;
          deleteData(row.id).then(res => {
            this.listLoading = false;
            this.$message({
              message: this.$t("pages.startPage.message.deleteSuccess"),
              type: "success"
            });
            this.renderData();
          });
        })
        .catch(() => {});
    },
    handleCreate() {
      this.thatRow = null;
      this.formEditVisible = true;
    },
    handleUpdate() {
      this.formEditVisible = true;
    },
    handleMouseEnter(row) {
      this.thatRow = row;
    },
    getPlanTypeLabel(type) {
      return this.planTypeTextMap[type] || type;
    },
    formEditSuccess() {
      this.formEditVisible = false;
      this.renderData();
    }
  }
};
</script>
<style scoped lang="scss">
.popover-content {
  max-width: 300px;
  line-height: 20px;
}
</style>
