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

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('消息编号')"
        prop="message_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('消息内容')"
        prop="category_content"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('公司所在地')"
        prop="message_area"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('公司所在地')"
        prop="message_full_area"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('地址')"
        prop="message_address"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('副图')"
        prop="message_image_other"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('关键字')"
        prop="message_keywords"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系人')"
        prop="message_username"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系人手机号')"
        prop="message_mobile"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否置顶（0 否 1 过期置顶  3 是）')"
        prop="stick_status"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('置顶时间')"
        prop="stick_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('创建时间')"
        prop="created_at"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('更新时间')"
        prop="updated_at"
        show-overflow-tooltip
      />
      <el-table-column align="center" :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>
    <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" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doDelete } from '@/api/user/service/message'
  import Edit from './components/userServiceMessageEdit'

  export default {
    name: 'UserServiceMessage',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      handleEdit(row) {
        if (row.message_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ message_id: row.message_id })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          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
      },
    },
  }
</script>
