<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>ID:</span>
        <el-input
          v-model="listQuery.infoId"
          placeholder="ID"
          clearable
          @input="(e) => (listQuery.infoId = checkedInput(e))"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>文章标题:</span>
        <el-input
          v-model="listQuery.title"
          placeholder="文章标题"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>统计状态:</span>
        <el-select v-model="listQuery.status" style="width: 120px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="0" label="统计中" />
          <el-option :value="1" label="暂停统计" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="articleDialogVisible = true"> 添加文章 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="600">
      <el-table-column prop="infoId" fixed label="ID" align="center" width="80" />
      <el-table-column label="引导图" prop="guidanceImage" align="center" width="100">
        <template slot-scope="{ row }">
          <el-popover placement="top-start" title="" width="80" trigger="hover">
            <img :src="row.guidanceImage" alt="" style="width: 100px; height: 100px" />
            <img slot="reference" :src="row.guidanceImage" alt="" style="width: 50px; height: 50px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="文章标题" align="center" />
      <el-table-column prop="status" label="统计状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 0" type="success" size="small">统计中</el-tag>
          <el-tag v-if="row.status === 1" type="danger" size="small">暂停统计</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="pubdateString" label="发布时间" align="center" width="200">
        <template slot-scope="{ row }">
          {{ row.pubdateString ? row.pubdateString : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="添加时间" align="center" width="200">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleSitting(row)">
            设置统计状态
          </el-button>
          <el-button type="danger" size="mini" style="margin: 2px" @click="handleDelete(row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getShopInfoList"
    />
    <!--  选择文章弹窗 -->
    <article-dialog
      v-if="articleDialogVisible"
      ref="articleDialog"
      @confirm="handleArticleConfirm"
      @cancel="handleCancel"
    />
    <!-- 设置统计状态 -->
    <el-dialog title="设置统计状态" :visible.sync="dialogFormVisible" width="500px">
      <el-form ref="dataForm" :model="renewCustome" label-position="right">
        <el-form-item prop="status">
          <el-radio v-model="renewCustome.status" :label="0">统计中</el-radio>
          <el-radio v-model="renewCustome.status" :label="1">暂停统计</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getShopInfoList, saveShopInfo, renewShopInfo, delShopInfo } from '@/api/user/customer/customer'
import ArticleDialog from './components/article-dialog'
export default {
  name: 'CustomerArticleList',
  components: {
    ArticleDialog
  },
  filters: {},
  data() {
    return {
      isLoading: true,
      listQuery: {
        shopId: this.$route.query.shopId,
        infoId: '',
        title: '',
        status: '',
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      articleDialogVisible: false,
      dialogFormVisible: false,
      renewCustome: {
        id: '',
        status: ''
      },
      rules: {
        status: [{ required: true, message: '请设置统计状态', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取供应商相关文章列表
    async getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getShopInfoList()
    },
    // 获取供应商相关文章列表
    async getShopInfoList() {
      try {
        this.isLoading = true
        const res = await getShopInfoList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 确认选择供文章
    handleArticleConfirm(data) {
      console.log('data', data)
      const infoIds = []
      data.forEach((ele) => {
        infoIds.push(ele.id)
      })
      this.saveShopInfo({ shopId: this.listQuery.shopId, infoIds: infoIds.join(',') })
    },
    // 保存添加供应商文章
    async saveShopInfo(params) {
      try {
        await saveShopInfo(params)
        this.articleDialogVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    handleCancel() {
      // 取消选择供应商
      this.articleDialogVisible = false
      this.$refs.articleDialog.visible = false
    },
    // 设置统计状态
    handleSitting(row) {
      this.renewCustome.id = row.id
      this.renewCustome.status = row.status
      this.dialogFormVisible = true
    },
    // 确认设置统计状态
    handleConfirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.renewShopInfo(this.renewCustome)
        } else {
          return false
        }
      })
    },
    // 调用统计状态API
    async renewShopInfo(params) {
      try {
        await renewShopInfo(params)
        this.dialogFormVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 操作删除文章
    async handleDelete(row) {
      try {
        await this.$confirm('确定删除该文章吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.delShopInfo(row)
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 调用删除文章
    async delShopInfo(row) {
      try {
        await delShopInfo(row.id)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    checkedInput(event) {
      const pattern = /[^0-9.]/g
      return event.replace(pattern, '')
    }
  }
}
</script>

<style></style>
