<template>
  <div class="user-paotuier-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.user_nickname"
              clearable
              :placeholder="__('请输入标题')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.user_account"
              clearable
              :placeholder="__('请输入用户名')"
            />
          </el-form-item>
          <el-form-item>
            <template>
              <el-select
                v-model="queryForm.user_certification"
                clearable
                :placeholder="__('认证状态')"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </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-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户名')"
        prop="user_account"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column :label="__('是否签约')" prop="ptm_state">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ptm_state"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('头像')"
        prop="user_avatar"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.user_avatar"
            :preview-src-list="[scope.row.user_avatar]"
            :src="scope.row.user_avatar"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('性别')"
        prop="user_gender"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('真是姓名')"
        prop="user_realname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('身份证')"
        prop="user_idcard"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('身份图片')"
        prop="user_idcard_images"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.user_idcard_images"
            :preview-src-list="[scope.row.user_idcard_images]"
            :src="scope.row.user_idcard_images"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('实名认证')"
        prop="user_certification"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBirthday"
        :label="__('生日')"
        prop="user_birthday"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('手机号码')"
        prop="user_mobile"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('电话')"
        prop="user_tel"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户邮箱')"
        prop="user_email"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('QQ')"
        prop="user_qq"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('阿里旺旺')"
        prop="user_ww"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('省')"
        prop="user_province_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('省')"
        prop="user_province_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('城市')"
        prop="user_city_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('城市')"
        prop="user_city_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('县')"
        prop="user_county_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('县')"
        prop="user_county_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('详细地址')"
        prop="user_address"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('签名')"
        prop="user_sign"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatDatetime"
        :label="__('注册时间')"
        prop="user_reg_datetime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('注册IP')"
        prop="user_reg_ip"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('登录时间')"
        prop="user_lastlogin_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('登录ip')"
        prop="user_lastlogin_ip"
        show-overflow-tooltip
        width="140"
      />
    </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 { doDelete, doEdit, getList } from '@/api/user/paotuier'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'UserPaotuier',

    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        options: [
          {
            value: 0,
            label: this.__('未认证'),
          },
          {
            value: 1,
            label: this.__('已认证'),
          },
          {
            value: 2,
            label: this.__('待审核'),
          },
        ],
        value: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,

        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      formatDatetime(row) {
        const user_reg_datetime = row.user_reg_datetime
        return user_reg_datetime ? renderTime(user_reg_datetime) : ''
      },
      formatTime(row) {
        const user_lastlogin_time = row.user_lastlogin_time
        return user_lastlogin_time ? renderTime(user_lastlogin_time) : ''
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ user_id: row.user_id })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      formatBirthday(row) {
        const user_birthday = row.user_birthday
        return user_birthday ? renderTime(user_birthday) : ''
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async sChange(row) {
        const param = {
          user_id: row.user_id,
          ptm_state: row.ptm_state,
        }

        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.tota = data.records
        this.listLoading = false
      },
    },
  }
</script>
