<template>
  <div class="PublishingChannelManagement">
    <!-- 搜索栏 -->
    <div class="filter">
      <div>
        <ButtonGradient @click.native="handleOpenDialogAddEdit()">
          <template #prefix>
            <img src="@screen/images/insert.svg" />
          </template>
          新增
        </ButtonGradient>
        <ButtonGradient @click.native="onRefreshForm">
          <template #prefix>
            <img src="@screen/images/refresh.svg" />
          </template>
          刷新
        </ButtonGradient>
      </div>

      <InputSearch
        ref="searchComp"
        style="width: 480px"
        :formList="searchFormList"
        @handleSearch="handleSearch"
      />
    </div>

    <!-- 内容 -->
    <div class="body">
      <Card v-for="item in channelsList" :key="item.id" :data="item" />
    </div>

    <!-- 分页 -->
    <div class="footer">
      <Pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        width="'100%'"
        :page-sizes="[10]"
        :page-size="searchData.pageSize"
        :current-page.sync="searchData.pageNum"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </Pagination>
    </div>

    <!-- 新增编辑弹窗 -->
    <AddNEditDialog v-model="addNEditDialogVisible" :data="dialogData" />
  </div>
</template>

<script>
import Pagination from "@screen/components/Pagination.vue";
import InputSearch from "@screen/components/InputSearch/index.vue";
import Card from "./components/Card";
import ButtonGradient from "@screen/components/Buttons/ButtonGradient.vue";
import AddNEditDialog from "./components/AddNEditDialog.vue";
import {
  getChannelsList,
  getChannels,
} from "@/api/service/PublishingChannelManagement.js";

import { searchFormList } from "./data";

export default {
  name: "PublishingChannelManagement",
  components: {
    Pagination,
    InputSearch,
    Card,
    ButtonGradient,
    AddNEditDialog,
  },
  data() {
    return {
      searchFormList,
      data: [],
      addNEditDialogVisible: false,
      dialogData: null,
      channelsList: [],
      total: 0,
      searchData: {
        pageSize: 10,
        pageNum: 1,
      },
    };
  },
  methods: {
    handleOpenDialogAddEdit(data) {
      console.log(77777, data);
      this.addNEditDialogVisible = true;
      if (data) {
        data.publishChannels = data.publishChannels.toString().split(",");
        this.dialogData = data;
      } else {
        this.dialogData = data;
      }
    },
    onRefreshForm() {
      this.$refs.searchComp.handleResetForm();
    },
    handleSearch(data) {
      if (data && data.publishChannels) {
        data.publishChannels = data.publishChannels.toString();
      }
      if (data && data.date) {
        data.startDate = data.date[0];
        data.endDate = data.date[1];
        // data.date = "";
      }
      data = { ...this.searchData, ...data };
      getChannelsList(
        this.searchData.pageNum,
        this.searchData.pageSize,
        data
      ).then((res) => {
        this.channelsList = res.rows;
        this.total = res.rows.length;
      });
    },
    handleCurrentChange(pageNum) {
      searchData.pageNum = pageNum;
      this.handleSearch();
    },
    handleSizeChange(size) {
      searchData.pageSize = size;
      this.handleSearch();
    },
  },
  mounted() {
    this.handleSearch();
  },
};
</script>

<style lang="scss" scoped>
.PublishingChannelManagement {
  padding: 21px;

  height: 100%;
  display: flex;
  flex-direction: column;
  z-index: 6;
  width: 100%;

  .filter {
    height: 60px;
    display: flex;
    justify-content: space-between;
    div {
      display: flex;
      gap: 6px;
    }
  }

  .body {
    flex: 1;
    overflow: hidden;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-gap: 24px;
    // grid-row-gap: 9px;
    // grid-column-gap: 9px;
    grid-auto-rows: min-content;
  }

  .footer {
    margin-top: 15px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
