<template>
  <div>
    <div v-show="!isShowDetail">
      <div class="top-tips">在普通素材页面，“网页”类型素材支持启用雷达功能，可以跟踪客户的查看记录，向关联的员工发送提醒。并支持对查看素材的客户自动打标签。</div>
      <!-- 筛选条件 -->
      <div class="screen-container">
        <el-form ref="screenForm" :model="screenForm" label-width="50px" size="small" label-position="left">
          <el-row :gutter="20">
            <el-col :xs="18" :sm="12" :lg="12">
              <el-form-item label="名称" prop="keyword">
                <el-input v-model="screenForm.keyword" placeholder="请输入名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="6" :sm="6" :lg="6">
              <el-form-item label="">
                <el-button size="small" @click="resetScreenForm">清空</el-button>
                <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="table">
        <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="操作" width="100" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="toDetail(scope.row)">详情</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="链接名称" prop="title" min-width="160"></el-table-column>
          <el-table-column align="center" label="链接" prop="url" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.type == 4">{{scope.row.url}}</span>
              <span v-if="scope.row.type == 6">{{scope.row.fileUrl}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="客户标签" prop="linkName" min-width="160">
            <template slot-scope="scope">
              <el-tag v-for="(item, index) in scope.row.tags" :key="index" size="medium" style="margin: 3px;">{{item.name}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建人" prop="createName" min-width="160"></el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime" min-width="160"></el-table-column>
          <el-table-column align="center" label="类型" prop="type" min-width="100">
            <template slot-scope="scope">
              {{scope.row.type | typeFilter}}
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal">
          </el-pagination>
        </div>
      </div>
    </div>

    <DynamicMaterialDetail :listItem="queryItem" v-if="isShowDetail" @backListFormDetail="backList" />

  </div>
</template>

<script>
import DynamicMaterialDetail from "@/views/marketing/components/material-dt-detail";
import { getDynamicList } from "@/api/marketing";

export default {
  components: {
    DynamicMaterialDetail
  },
  filters: {
    typeFilter(val) {
      const MAP = {
        1: '文本',
        2: '图片',
        3: '视频',
        4: '网页',
        5: '小程序',
        6: '文件',
      }
      return MAP[val];
    }
  },
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      screenForm: {
        keyword: '',
      },

      queryItem: {},
    }
  },

  computed: {
    isShowDetail() {
      return this.queryItem.hasOwnProperty('marketingMaterialsId');
    },
  },

  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.listLoading = true;

      let params = {
        name: this.screenForm.keyword,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }

      getDynamicList(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    // 进入详情
    toDetail(item) {
      this.queryItem = item;
      this.$parent.handleDisplay(false);
    },

    backList() {
      this.queryItem = {};
      this.$parent.handleDisplay(true);
    },
  }
}
</script>

<style lang="scss" scoped>
  .top-tips {
    font-size: 14px;
    margin-bottom: 20px;
  }
</style>