<template>
  <div class="story-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>

      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select v-if="false" v-model="queryForm.story_enable">
              <el-option :label="__('全部')" :value="null" />
              <el-option :label="__('待审核')" :value="0" />
              <el-option :label="__('有效')" :value="1" />
            </el-select>

            <el-radio-group v-model="queryForm.story_enable" @change="queryData">
              <el-radio-button :label="null" >{{__('全部')}}</el-radio-button>
              <el-radio-button :label="0" >{{__('待审核')}}</el-radio-button>
              <el-radio-button :label="1" >{{__('已审核')}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.story_index"
              clearable
              :placeholder="__('请输入搜索内容')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="120"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('编号')"
        prop="story_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('审核通过')"
        prop="story_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.story_enable"
            :active-value="1"
            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('标题')"
        prop="story_title"
        show-overflow-tooltip
        width="240"
      />
      <el-table-column
        align="center"
        :label="__('主图')"
        prop="story_file"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-image
            v-for="(storefile, index) in scope.row.story_file.split(',')"
            v-show="scope.row.story_file"
            :key="index"
            :preview-src-list="[storefile]"
            :src="storefile"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('视频')"
        prop="topic_id"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <vab-icon
            v-if="row.story_video"
            icon="video-fill"
            style="font-size: xx-large"
            @click="showPlayer(row)"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('类型')"
        prop="story_type"
        show-overflow-tooltip
        width="125"
      />
      <el-table-column
        align="center"
        :label="__('会员编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('添加时间')"
        prop="story_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatStatus"
        :label="__('状态')"
        prop="story_status"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('隐私可见度')"
        prop="story_privacy"
        show-overflow-tooltip
        width="140"
      >
        <template #default="{ row }">
          <el-select
            v-model="row.story_privacy"
            :placeholder="__('请选择')"
            @change="sChange(row)"
          >
            <el-option
              v-for="item in privacyOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('隐私可见度')"
        prop="story_privacy"
        show-overflow-tooltip
        width="125"
      />
      <el-table-column
        align="center"
        :label="__('评论数')"
        prop="story_comment_count"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('转发数')"
        prop="story_forward_count"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('点赞数')"
        prop="story_like_count"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('帖子收藏数')"
        prop="story_collection_count"
        show-overflow-tooltip
        width="125"
      />
      <el-table-column
        align="center"
        :formatter="formatTop"
        :label="__('置顶')"
        prop="story_is_top"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('访问数')"
        prop="story_brower_count"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="story_category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('标签')"
        prop="story_tags"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatSource"
        :label="__('数据源')"
        prop="story_source"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="product_id"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <player ref="player" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doDelete, doEdit } from '@/api/story/base'
  import Edit from './components/StoryBaseEdit'
  import { renderTime } from '@/utils/date'
  import player from '@/components/material/Player'

  export default {
    name: 'StoryBase',
    components: { Edit, player },
    data() {
      return {
        // 0-所有人可见; 1-好友可见; 2-仅自己可见
        privacyOptions: [
          {
            value: 0,
            label: this.__('所有人可见'),
          },
          {
            value: 1,
            label: this.__('好友可见'),
          },
          {
            value: 2,
            label: this.__('仅自己可见'),
          },
        ],
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          story_enable: null,
          story_index: '',
        },
        imagesTest: [
          'http://lancerdt.oss-accelerate.aliyuncs.com/mall/images/media/plantform/20220805/246dd609269a4e0e9c3806db698b8933.png',
          'http://lancerdt.oss-accelerate.aliyuncs.com/mall/images/media/plantform/20220805/7e20e91231664f52b2a3a1d5b57e7dfd.png',
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      showPlayer(row) {
        const d = {media_id:row.story_id, media_url:row.story_video}

        this.$refs['player'].showEdit(d)
      },
      formatTime(row) {
        const cardTime = row.story_time
        return cardTime ? renderTime(cardTime) : ''
      },

      formatPrivacy(row) {
        return row.story_privacy === 0
          ? this.__('所有人可见')
          : row.story_privacy === 1
          ? this.__('好友可见')
          : this.__('仅自己可见')
      },
      formatSource(row) {
        return row.story_source === 1001
          ? this.__('帖子')
          : row.story_source === 1002
          ? this.__('商品评论')
          : this.__('商品问答')
      },
      formatType(row) {
        return row.story_type === 1
          ? this.__('文字')
          : row.story_type === 2
          ? this.__('图片')
          : row.story_type === 3
          ? this.__('音乐')
          : row.story_type === 4
          ? this.__('视频')
          : this.__('商品')
      },
      formatStatus(row) {
        return row.story_status === 0 ? this.__('草稿') : this.__('发布')
      },
      formatTop(row) {
        return row.story_is_top ? this.__('置顶') : this.__('正常')
      },
      handleEdit(row) {
        if (row.story_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            story_id: row.story_id,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          await this.fetchData()
        })
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async sChange(row) {
        const param = {
          story_id: row.story_id,
          story_enable: row.story_enable,
          user_id: row.user_id,
          story_forward: row.story_forward,
          story_privacy: row.story_privacy,
        }

        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
    },
  }
</script>
