<template>
  <div class="page-user-form-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.page_id"
              clearable
              filterable
              :placeholder="__('请选择页面')"
              reserve-keyword
            >
              <el-option
                v-for="item in pageList"
                :key="item.page_id"
                :label="item.page_name"
                :value="item.page_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.user_nickname"
              clearable
              :placeholder="__('请输入用户昵称')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :height="tableConfig.height"
      @selection-change="setSelectRows"
    >
      <el-table-column fixed="left" type="expand">
        <template #default="{ row }">
          <div class="ms-table-expand">
            <div v-for="item in row.keyVlaueMap">
              问：{{ item.key }} 答：{{ item.value }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="left"
        :label="__('编号')"
        prop="id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('页面编号')"
        prop="page_id"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
        width="90"
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('问答详情')"
        prop="keyVlaueMap"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <div style="max-height: 50px">
            <div v-for="item in scope.row.keyVlaueMap">
              问：{{ item.key }} 答：{{ item.value }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('表单时间')"
        prop="form_time"
        show-overflow-tooltip
        width="200"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/base/userformpage'
  import { getList as getPageList } from '@/api/page/base'

  export default {
    name: 'PageUserForm',
    components: {},
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 50,
        },
        list: [],
        pageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
          user_nickname: undefined,
        },
        lecturerList: [],
        cycleList: [],
        classes_lecturer_id: '',
      }
    },
    created() {
      this.getPageList()
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      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 getPageList() {
        this.listLoading = true
        this.queryForm.pageSize = 9999
        const { data } = await getPageList(this.queryForm)
        this.pageList = data.items
        this.queryForm.pageSize = 20
        this.listLoading = false
      },
    },
  }
</script>
