<template>
  <vxe-modal
    v-model="informFlag"
    title="消息通知"
    width="1000"
    min-height="500"
    resize
    remember
    @close="closeInform"
  >
    <template #default>
      <el-tabs v-model="activeName" type="card" @tab-click="pageChange(1)">
        <el-tab-pane label="全部" name="0"></el-tab-pane>
        <el-tab-pane label="交易物流" name="1"></el-tab-pane>
        <el-tab-pane label="营销关怀" name="2"></el-tab-pane>
      </el-tabs>
      <el-form :inline="true" size="small">
        <el-form-item>
          <el-select
            v-model="enableStatus"
            placeholder="选择状态"
            style="width: 150px"
            clearable
            @change="pageChange(1)"
          >
            <el-option label="全部" :value="0"></el-option>
            <el-option label="已开启" :value="5"></el-option>
            <el-option label="未开启" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="keyword"
            style="width: 220px"
            placeholder="搜索消息通知名称"
            clearable
            @keyup.enter.native="pageChange(1)"
            @clear="pageChange(1)"
          >
            <el-button slot="append" @click="pageChange(1)">
              <i class="el-icon-search"></i>
            </el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <vxe-table :data="tableData" :show-header="false">
        <vxe-table-column field="name" title="通知名称"></vxe-table-column>
        <vxe-table-column field="note" width="100">
          <template #default="{ row }">
            <el-switch
              v-model="row.enableStatus"
              class="switchStyle"
              active-color="#36B365"
              :active-value="5"
              :inactive-value="4"
              active-text="启用"
              inactive-text="禁用"
              @change="updateEnableStatus($event, row)"
            ></el-switch>
          </template>
        </vxe-table-column>
      </vxe-table>
      <FooterPage
        :page-size="pageSize"
        :total-page.sync="total"
        :current-page.sync="page"
        @pageChange="pageChange"
        @sizeChange="sizeChange"
      ></FooterPage>
    </template>
  </vxe-modal>
</template>
<script>
  import { getAllSystemPushMessage, updateEnableStatus } from "@/api/System";
  import { GoodsUpdateEnableStatus } from "@/api/goods";
  export default {
    props: {
      informFlag: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        activeName: "0",
        keyword: "",
        enableStatus: "",
        tableData: [],
        page: 1,
        pageSize: 10,
        total: 0,
      };
    },
    created() {
      this.getAllSystemPushMessage();
    },
    methods: {
      closeInform() {
        this.$emit("close");
      },
      pageChange(val) {
        this.page = val;
        this.getAllSystemPushMessage();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      async getAllSystemPushMessage() {
        const { data, pageTotal } = await getAllSystemPushMessage({
          page: this.page,
          pageSize: this.pageSize,
          type: Number(this.activeName),
          enabledStatus: this.enableStatus,
          keyword: this.keyword,
        });
        this.tableData = data;
        this.total = pageTotal;
      },
      async updateEnableStatus(val, row) {
        try {
          const data = await updateEnableStatus({
            id: row.id,
            enabledStatus: val,
          });
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        } catch (e) {
          await this.getAllSystemPushMessage();
        }
      },
    },
  };
</script>
<style></style>
