<template>
  <ContainerQuery>
    <div slot="left">
      <el-button
        v-if="$accessCheck($Access.PushNotification_addMsg)"
        size="small"
        type="primary"
        @click="informFlag = true"
      >
        开通通知
      </el-button>
    </div>
    <div slot="more">
      <el-form :inline="true" size="small">
        <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>
    </div>
    <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>
    <vxe-table border="inner" :data="tableData">
      <vxe-table-column field="id" title="ID" width="60"></vxe-table-column>
      <vxe-table-column field="name" title="通知名称"></vxe-table-column>
      <!--<vxe-table-column field="note" title="短信">
        <template #default="{ row }">
          <el-switch
            v-model="row.note"
            class="switchStyle"
            active-color="#36B365"
            :active-value="5"
            :inactive-value="4"
            active-text="启用"
            inactive-text="禁用"
          ></el-switch>
        </template>
      </vxe-table-column>-->
      <vxe-table-column field="miniProgram" title="小程序订阅消息">
        <template #default="{ row }">
          <el-switch
            v-if="$accessCheck($Access.PushNotification_updatePushEnableStatus)"
            v-model="row.enabledStatus"
            class="switchStyle"
            active-color="#36B365"
            :active-value="5"
            :inactive-value="4"
            active-text="启用"
            inactive-text="禁用"
            @change="updatePushEnableStatus($event, row)"
          ></el-switch>
          <div v-else>
            <span v-if="row.enabledStatus === 5" class="success-status">
              启用
            </span>
            <span v-else class="danger-status">禁用</span>
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column
        v-if="$accessCheck($Access.PushNotification_setMsg)"
        title="操作"
      >
        <template #default="{ row }">
          <el-button
            v-if="$accessCheck($Access.PushNotification_setMsg)"
            type="text"
            @click="
              $router.push(
                `/SystemSettings/jiaoyiset/PushNotificationSet/${row.settingId}`
              )
            "
          >
            设置
          </el-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
    <addInform :inform-flag="informFlag" @close="closeAddInform"></addInform>
  </ContainerQuery>
</template>
<script>
  import addInform from "@/views/SystemSettings/components/addInform";
  import {
    getAllSystemPushMessage,
    updatePushEnableStatus,
  } from "@/api/System";
  export default {
    components: {
      addInform,
    },
    data() {
      return {
        keyword: "",
        activeName: "0",
        tableData: [{ name: "下单成功", note: 5, miniProgram: 5 }],
        informFlag: false,
        page: 1,
        pageSize: 10,
        total: 0,
      };
    },
    created() {
      this.getAllSystemPushMessage();
    },
    methods: {
      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: 5,
          keyword: this.keyword,
        });
        this.tableData = data;
        this.total = pageTotal;
      },
      closeAddInform() {
        this.informFlag = false;
        this.pageChange(1);
      },
      async updatePushEnableStatus(val, row) {
        try {
          const data = await updatePushEnableStatus({
            id: row.id,
            weixinEnabledStatus: val,
            smsEnabledStatus: val,
          });
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        } catch (e) {
          await this.getAllSystemPushMessage();
        }
      },
    },
  };
</script>
<style></style>
