<template>
  <div class="user-resource-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button type="primary" @click="showImport">
          {{ __('批量导入') }}
        </el-button>
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <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-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
          <el-button icon="el-icon-refresh" type="primary" @click="queryData">
            {{ __('刷新') }}
          </el-button>
        </el-form>
      </vab-query-form-right-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="handleEdit(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
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('重置支付密码')"
        prop="set_pay_passwd"
        show-overflow-tooltip
        width="140"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.set_pay_passwd"

            :disabled="!scope.row.set_pay_passwd"

            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('用户资金')"
        prop="user_money"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('冻结资金')"
        prop="user_money_frozen"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('充值卡余额')"
        prop="user_recharge_card"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('冻结充值卡')"
        prop="user_recharge_card_frozen"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('积分')"
        prop="user_points"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('冻结积分')"
        prop="user_points_frozen"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户信用')"
        prop="user_credit"
        show-overflow-tooltip
      />
      <el-button class="el-icon-delete" type="text" @click="handleEdit(row)" />
    </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" />
    <userInfo-import ref="userInfoImport" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { enable, getList } from '@/api/user/resource'
  import Edit from './components/UserResourceEdit'
  import userInfoImport from './components/UserInfoImport'

  export default {
    name: 'UserResource',
    components: { Edit, userInfoImport },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      handleEdit(row) {
        if (row.user_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      showImport() {
        this.$refs.userInfoImport.showEdit()
      },

      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,
          set_pay_passwd: 0,
          enable_name: 'set_pay_passwd',
          enable_id: 'user_id',
        }
        const { msg } = await enable(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.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
