<template>
  <el-dialog title="添加文章" :visible.sync="visible" width="1200px" :close-on-click-modal="false" :show-close="false">
    <div class="filter-container">
      <div class="filter-control">
        <span>ID:</span>
        <el-input
          v-model="listQuery.id"
          placeholder="ID"
          clearable
          @input="(e) => (listQuery.id = checkedInput(e))"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>标题:</span>
        <el-input
          v-model="listQuery.title"
          placeholder="标题"
          clearable
          style="width: 160px"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
      </div>
    </div>
    <el-table
      ref="table"
      v-loading="isLoading"
      :data="list"
      height="400px"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" :selectable="selectable" />
      <el-table-column label="ID" prop="id" align="center" width="100" />
      <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 label="文章标题" prop="title" align="center" />
      <el-table-column label="发布时间" prop="pubdateString" align="center">
        <template slot-scope="{ row }">
          {{ row.pubdateString ? row.pubdateString : '---' }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
    <div slot="footer">
      <el-button @click="handleCanle"> 取 消 </el-button>
      <el-button type="primary" :disabled="disabled" @click="handleConfirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getInfoList } from '@/api/user/customer/customer'

export default {
  name: 'ArticleDialog',
  filters: {
    NumFormat(value) {
      // 处理金额
      return Number(value).toFixed(2)
    }
  },
  data() {
    return {
      visible: true,
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 20, // 页面数据数
        id: '', // 文章Id
        title: '' // 文章标题
      },
      list: [],
      total: 0,
      acticleRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.acticleRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取所有供应商列表
    async getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getInfoList()
    },
    // 获取所有供应商列表
    async getInfoList() {
      try {
        const res = await getInfoList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 选择文章
    handleSelectionChange(row) {
      this.acticleRadio = row
      console.log('row', row)
    },
    // 确认选择文章
    handleConfirm() {
      this.$emit('confirm', this.acticleRadio)
    },
    handleCanle() {
      // 取消弹窗
      this.$emit('cancel')
    },
    // 已选择的禁用勾选框
    selectable(row) {
      if (row.flag) {
        return true
      } else {
        return false
      }
    },
    checkedInput(event) {
      const pattern = /[^0-9.]/g
      return event.replace(pattern, '')
    }
  }
}
</script>
<style lang="scss" scoped></style>
