<template>
  <div class="table">
    <!-- 高级搜索 -->
    <TableSearch @searchTable="searchTable" />
    <PopForm v-if="isShow" @closePopForm="closePopForm" :data="FormData" :that="this" />

    <div class="columns-action">
      <div class="add" :style="{ marginRight: '15px' }" @click="edit('', that)">
        <a-tooltip title="新增">
          <a-icon class="action" type="plus" />
        </a-tooltip>
      </div>

      <!-- 表格大小设置 -->
      <TableSize class="action-size" @changeTableSize="changeTableSize" :that="this" />

      <!-- 表格列设置 -->
      <ActionColumns
        class="action-column"
        @columns="getcolumns"
        :columns-keys="columnsKeys"
        @getTableFixed="getTableFixed"
        @resetTableFixed="resetTableFixed"
        :columns-fixed="columnsFixed"
        :that="this"
      />


      
       <div class="add" :style="{ marginRight: '15px' }" @click="getList">
        <a-tooltip title="刷新">
          <a-icon class="action" type="reload" />
        </a-tooltip>
      </div>
    </div>
    <a-spin :spinning="isSpinning">
      <a-table
        v-if="data"
        :columns="columns"
        :data-source="data"
        :scroll="{ x: 1820, y: fullHeight - 310 }"
        :size="tableSize"
        row-key="id"
        :pagination="{ total: total }"
        @change="changepage"
      >
        <!-- 名称 -->
        <template slot="name" slot-scope="name">{{ name }}</template>

        <!-- 项目 -->
        <span
          :style="{ color: '#16D46B', fontWeight: '700' }"
          slot="adevrtGamePlate"
          slot-scope="adevrtGamePlate"
          >{{ adevrtGamePlate == 10019 ? "Ludo" : "Other" }}</span
        >

        <!-- 图片 -->
        <span slot="img" slot-scope="img">
          <div class="img" :style="{ width: '100%', textAlign: 'center' }">
            <img
              :src="
                img.indexOf('https://file.yallaludo.com/') == -1
                  ? 'https://file.yallaludo.com/' + img
                  : img
              "
              alt=""
              srcset=""
              :style="{ width: '100px', textAlign: 'center' }"
            />
          </div>
        </span>

        <!-- 名称  -->
        <span slot="customTitle"><a-icon type="smile-o" /> 名称</span>

        <!-- 位置  -->
        <span slot="advertPosition" slot-scope="advertPosition">
          {{
            advertPosition == 1
              ? "大厅"
              : advertPosition == 2
              ? "Events"
              : advertPosition == 3
              ? "ChatRoom-Explore"
              : "ChatRoom-Hot"
          }}
        </span>

        <!-- 语言  -->
        <span slot="language" slot-scope="language">
          {{ language == 1 ? "英语" : "阿语" }}
        </span>

        <!-- 设备类型 -->
        <span slot="deviceType" slot-scope="deviceType">
          <a-tag :color="deviceType == '1' ? 'geekblue' : 'green'">
            {{ deviceType == "1" ? "IOS" : "Android" }}
          </a-tag>
        </span>

        <!-- 跳转地址 -->
        <span slot="h5Url" slot-scope="text">
          <a>
            {{ text.h5Url }}
          </a>
        </span>

        <!-- 通用范围  -->
        <span slot="softwareHD" slot-scope="softwareHD">
          {{ !softwareHD ? "ALL" : softwareHD == 1 ? "主包" : "HD" }}
        </span>

        <!-- 状态  -->
        <span
          slot="status"
          slot-scope="status, info"
          :style="{ color: `${status ? '#FD572B' : '#16D46B'}` }"
        >
          <a-switch
            checked-children="开"
            un-checked-children="关"
            @change="changeStatus($event, info)"
            :default-checked="status ? false : true"
          />
        </span>

        <!-- 时间
      <template slot="date" slot-scope="text">
        <span>{{ text.date }}</span>
      </template> -->

        <!-- 操作 -->
        <template slot="operation" slot-scope="x, y">
          <div>
            <a-button
              type="primary"
              @click="edit(y, that)"
              :style="{ marginRight: '5px' }"
            >
              编辑
            </a-button>

            <a-popconfirm title="删除该广告?" @confirm="() => deletedADInfo(y.id)">
              <a-button
                type="danger"
                :style="{ background: '#FD572B', marginRight: '5px' }"
              >
                删除
              </a-button>
            </a-popconfirm>

            <a-button
              type="primary"
              :style="{
                background: '#16D46B',
                border: '#16D46B',
              }"
              @click="goDetail(y)"
            >
              查看详情
            </a-button>
          </div>
        </template>
      </a-table>
    </a-spin>
  </div>
</template>
<script>
import "@/common/components/Commonality/Table/less/table.less";
import { getADList } from "@/api/job/execute.js";
import ActionColumns from "@/common/components/Commonality/Table/components/TableSet.vue";
import PopForm from "@/common/components/Commonality/Form/Index.vue";
import TableSearch from "@/common/components/Commonality/Table/components/TableSearch.vue";
import TableSize from "@/common/components/Commonality/Table/components/TableSize.vue";
import {
  resetFullHeight,
  changeTableSize,
  getcolumns,
  getTableFixed,
  resetTableFixed,
  edit,
  closePopForm,
  goDetail,
  columnsFixed,
  columnsKeys,
} from "@/common/components/Commonality/Table/js/table.js";
import { columns } from "@/common/components/Commonality/Table/js/columns.js";
export default {
  data() {
    return {
      columns,
      columns2: [],
      data: [], // 数据
      data2: undefined,
      isShow: false,
      tableSize: "default", // 表格默认大小
      total: 0, // 分页参数
      searchParams: {}, // 高级搜索参数
      fullHeight: "", // 页面高度
      FormData: "",
      isSpinning: false,
    };
  },

  created() {
    // 初始化表格窗口高度
    resetFullHeight(this);
    this.columns2 = JSON.parse(JSON.stringify(this.columns));

    // //获取广告列表
    this.getList(this.searchParams);
  },
  methods: {
    // 改变表格size  子传父
    changeTableSize,
    // 列设置 展示指定 的参数
    getcolumns,
    // 编辑跳转 表单
    edit,
    // 改变列固定方向
    getTableFixed,
    // 重置 列 固定方向
    resetTableFixed,
    // 关闭表单（编辑）
    closePopForm,
    // 跳转详情页
    goDetail,
    // 获取 广告列表
    getList(searchParams) {
      this.isSpinning = true;
      getADList(searchParams).then((res) => {
        this.total = res.totalCount;
        this.data = res.data;
        this.data2 = JSON.parse(JSON.stringify(this.data));
        console.log(res.data);
        this.isSpinning = false;
      });
    },
    // 翻页
    changepage(index) {
      getADList({ Page: index.current, ...this.searchParams }).then((res) => {
        var ADList = res.data;
        this.data = ADList;
        this.data2 = JSON.parse(JSON.stringify(this.data));
      });
    },
    // 查询
    searchTable(searchParams) {
      this.searchParams = searchParams;
      this.data = JSON.parse(JSON.stringify(this.data2));
      this.getList(this.searchParams);
    },
    // 改变广告状态
    changeStatus(status, info) {
      // console.log(status, info.id);
      this.data = this.data.map((item) => {
        if (item.id == info.id) item.status = status ? 0 : 1;
        return item;
      });

      // console.log(info.id, info.status);
      info.status = info.status ? 1 : "0";
      changeADStatus(info.id, info.status).then(() => {
        // console.log(res.data);
        this.$message.success("更改成功");
      });
    },
    // 删除广告信息
    deletedADInfo(id) {
      console.log(id);
      // delectedADInfo(id).then(() => {
      //   this.$message.success("删除成功");
      //   this.data = this.data.filter((item) => item.id != id);
      //   this.data2 = JSON.parse(JSON.stringify(this.data));
      // });
    },
  },

  computed: {
    // 提取 标题 传递给子组件
    columnsKeys,
    // 提取 fixed 传递给子组件
    columnsFixed,

    that() {
      return this;
    },
    // tabsInfo() {
    //   return this.$store.state.tabsInfo;
    // },
  },
  components: {
    ActionColumns,
    PopForm,
    TableSearch,
    TableSize,
  },
  watch: {
    fullHeight(val) {
      // console.log(val);
      this.fullHeight = val;
    },
  },
};
</script>
