<template>
  <div class="list_container">
    <apass-list
      ref="list"
      :list-header="listHeader"
      :list-data="listData"
      :list-total="listTotal"
      :hide-header="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.recommended") }}
        </el-breadcrumb-item>
      </el-breadcrumb>

      <template slot="top">
        <div class="top_fliter">
          <show-more-filter class="filter_list">
            <div class="filter_item">
              <span class="filter_title">推荐位名称：</span>
              <el-input
                v-model="topFilter.name"
                placeholder="请输入推荐位名称"
              ></el-input>
            </div>
            <div class="filter_item">
              <span class="filter_title">状态：</span>
              <el-select v-model="topFilter.state" placeholder="请选择">
                <el-option label="全部" value=""> </el-option>
                <el-option label="正常" value="1"> </el-option>
                <el-option label="异常" value="2"> </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">最后更新时间：</span>
              <el-date-picker
                v-model="topFilter.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </div>
          </show-more-filter>
          <div class="filter_action apaas_button">
            <el-button type="primary" @click="topFilterAction">
              查询
            </el-button>
            <el-button type="defalut" @click="topFilterClear">
              重置
            </el-button>
          </div>
        </div>
      </template>
    </apass-list>
  </div>
</template>

<script>
import helper from "@/services/helper.js";
import apassList from "@/components/apass-list";
import showMoreFilter from "@/components/show-more-filter";

export default {
  components: {
    apassList,
    showMoreFilter,
  },
  data: () => ({
    listHeader: [],
    listData: [],
    listTotal: 0,
    topFilter: {
      name: "",
      state: "",
      time: "",
    },
    tempFilter: null,
  }),
  created() {
    this.listHeader = [
      {
        label: "",
        prop: "state",
        type: "image-tooltip",
        getImage(item) {
          return item.state == 1
            ? require("@/assets/imgs/ic_true.png")
            : require("@/assets/imgs/ic_failed.png");
        },
        getTooltip(item) {
          return item.state == 1 ? "状态正常" : "状态异常";
        },
        align: "center",
        width: 60,
      },
      {
        label: "推荐位名称",
        prop: "ad_name",
        type: "button",
        callback: this.detailAction,
        align: "center",
        width: 100,
      },
      {
        label: "服务个数",
        prop: "serv_count",
        align: "center",
        width: 160,
      },
      {
        label: "服务详情",
        prop: "service_names",
        getText(item) {
          let services = item.service_names || [];

          return services.map((item) => item.name).join("，");
        },
      },
      {
        label: "操作人",
        prop: "update_user",
        align: "center",
        width: 120,
      },
      {
        label: "最后更新 时间",
        prop: "update_time",
        getText(item) {
          let date = item.update_time || "";
          return helper.dateStringTransform(date);
        },
        align: "center",
        width: 240,
      },
      {
        label: "操作",
        type: "buttons",
        align: "center",
        width: 160,
        actionList: [
          {
            label: "编辑",
            callback: this.editAction,
          },
        ],
      },
    ];
  },
  methods: {
    initList(filter) {
      let params = {
        page: filter.page,
        size: filter.size,
        name: this.topFilter.name,
        state: this.topFilter.state,
        uptime_s:
          (this.topFilter.update_time && this.topFilter.update_time[0]) || "",
        uptime_e:
          (this.topFilter.update_time && this.topFilter.update_time[1]) || "",
      };

      // console.log(params);

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

      this.$http
        .get("/apaas/service/v3/recommend/manage/featured/list", { params })
        .then(({ data }) => {
          if (data.success == 1) {
            this.listTotal = (data.data && data.data.total) || 0;
            this.listData = (data.data && data.data.data) || [];
          } 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: "",
        state: "",
        time: "",
      };

      this.initList(this.tempFilter);
    },
    detailAction(item) {
      this.$router.push('/message/recommended/edit?ad_type='+item.ad_type+'&id='+item.id+'&look=1')
    },
    editAction(item) {
      this.$router.push('/message/recommended/edit?ad_type='+item.ad_type+'&id='+item.id+'&look=2')
    },
  },
};
</script>

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