<template>
  <div class="list_container">
    <apass-list
      ref="list"
      :list-header="listHeader"
      :list-data="listData"
      :list-total="listTotal"
      :hide-search="true"
      :list-padding-left="0"
      @list-action="initList"
    >
      <el-breadcrumb separator="/" slot="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/message' }">
          {{ $t("lang.message") }}
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          {{ $t("lang.message_template") }}
        </el-breadcrumb-item>
      </el-breadcrumb>

      <template slot="top">
        <div class="top_fliter">
          <div class="filter_list">
            <div class="filter_item">
              <span class="filter_title">消息模板搜索：</span>
              <el-input
                v-model="topFilter.name"
                placeholder="请输入消息模板名称/id"
              ></el-input>
            </div>
            <div class="filter_item">
              <span class="filter_title">模板类型：</span>
              <el-select v-model="topFilter.type" placeholder="请选择">
                <el-option label="服务推送" value="0"> </el-option>
                <el-option label="平台维护" value="1"> </el-option>
                <el-option label="版本更新" value="2"> </el-option>
                <el-option label="自动消息" value="3"> </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">最后更新时间：</span>
              <el-date-picker
                v-model="topFilter.update_time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </div>
          </div>
          <div class="filter_action apass_button">
            <el-button type="primary" @click="topFilterAction">
              查询
            </el-button>
            <el-button type="defalut" @click="topFilterClear">
              重置
            </el-button>
          </div>
        </div>
      </template>

      <el-button type="primary" @click="addNew" slot="header-left">
        新建
      </el-button>
    </apass-list>

    <apass-dialog
      ref="dialog"
      :msg="dialogInfo.msg"
      :submit="dialogInfo.submit"
    ></apass-dialog>
  </div>
</template>

<script>
import helper from "@/services/helper.js";
import apassList from "@/components/apass-list";
import apassDialog from "@/components/apass-dialog";

export default {
  components: {
    apassList,
    apassDialog,
  },
  data: () => ({
    listHeader: [],
    listData: [],
    listTotal: 0,
    dialogInfo: {
      msg: "",
      submit: null,
    },
    topFilter: {
      name: "",
      type: "",
      update_time: "",
    },
    tempFilter: null,
    types: [
      {
        name: "全部",
        value: "",
      },
      {
        name: "服务推送",
        value: 1,
      },
      {
        name: "平台维护",
        value: 2,
      },
      {
        name: "版本更新",
        value: 3,
      },
      {
        name: "自动消息",
        value: 4,
      },
    ],
  }),
  created() {
    let _self = this;

    _self.listHeader = [
      {
        label: "模板ID",
        prop: "id",
        width: 240,
      },
      {
        label: "消息模板名称",
        prop: "name",
        type: "button",
        callback: _self.detailAction,
        width: 240,
      },
      {
        label: "模板类型",
        prop: "tpl_type",
        getText(item) {
          let types = [..._self.types];

          types[0] = {
            name: "-",
            value: "",
          };

          return types[item.tpl_type || 0].name;
        },
        width: 160,
        align: "center",
      },
      {
        label: "模板详情",
        prop: "content",
      },
      {
        label: "最后更新时间",
        prop: "update_time",
        getText(item) {
          let date = item.update_time || "";
          return helper.dateStringTransform(date);
        },
        align: "center",
        width: 180,
      },
      {
        label: "操作",
        type: "buttons",
        align: "center",
        width: 160,
        actionList: [
          {
            label: "编辑",
            callback: this.editAction,
          },
          {
            label: "删除",
            class: "warn",
            callback: this.deleteAction,
          },
        ],
      },
    ];
  },
  methods: {
    initList(filter) {
      let params = {
        page: filter.page,
        size: filter.size,
        keyword: this.topFilter.name,
        tpl_type: this.topFilter.type,
        update_time_s:
          (this.topFilter.update_time && this.topFilter.update_time[0]) || "",
        update_time_e:
          (this.topFilter.update_time && this.topFilter.update_time[1]) || "",
      };

      // console.log(params);

      this.tempFilter = filter; // filter存档，用于页面刷新

      this.$http
        .get("/apaas/service/v3/msg/manage/tpl/list", { params })
        .then(({ data }) => {
          if (data.success == 1) {
            this.listTotal = (data.data && data.data.total) || 0;
            this.listData = (data.data && data.data.list) || [];
          } else {
            this.$message({
              message: data.errMsg || "获取列表失败",
              type: "warning",
            });
          }
        })
        .catch((error) => {
          console.log(error);

          this.$message({
            message: "获取列表失败",
            type: "warning",
          });
        });
    },
    topFilterAction() {
      this.initList(this.tempFilter);
    },
    topFilterClear() {
      this.topFilter = {
        name: "",
        type: "",
        update_time: "",
      };

      this.initList(this.tempFilter);
    },
    showDialog() {
      this.$refs.dialog.show();
    },
    addNew() {
      this.$router.push("/message/message_template/add");
    },
    detailAction(item) {
      this.$router.push(`/message/message_template/detail/${item.id}`);
    },
    editAction(item) {
      this.$router.push(`/message/message_template/edit/${item.id}`);
    },
    deleteAction(item) {
      this.dialogInfo.msg = `
        您确认删除这条数据吗？</br>
        <span style='color: #e15260;'>删除后无法撤销</span>，您还要继续吗？
      `;
      this.dialogInfo.submit = () => {
        this.$http
          .post(`/apaas/service/v3/msg/manage/tpl/delete/${item.id}`)
          .then(({ data }) => {
            if (data.success == 1) {
              this.$message({
                message: data.errMsg || `删除${item.name}成功`,
                type: "success",
              });

              this.initList(this.tempFilter);
            } else {
              this.$message({
                message: data.errMsg || `删除${item.name}失败`,
                type: "warning",
              });
            }
          })
          .catch((error) => {
            console.log(error);

            this.$message({
              message: `删除${item.name}失败`,
              type: "warning",
            });
          });
      };
      this.$refs.dialog.show();
    },
  },
};
</script>

<style scoped>
.list_container {
  height: 100%;
}
</style>
