<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit1">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.user_account"
              clearable
              :placeholder="__('请输入用户名')"
            />
          </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-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" :label="__('序号')" width="80">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('操作')"
        show-overflow-tooltip
        width="180"
      >
        <template #default="{ row }">
          <el-button
            type="text"
            @click="handleEdit(row)"
          >
            设置角色
          </el-button>
          <el-button
            type="text"
            @click="handleEdit1(row)"
          >
            编辑
          </el-button>
          <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="__('角色')" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tag
            v-for="(rights_group_name, index) in row.rights_group_names"
            :key="index"
          >
            {{ rights_group_name }}
          </el-tag>
        </template>
      </el-table-column>

      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </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" />
    <add ref="add" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getList } from '@/api/user/base'
  import Edit from './components/UserManagementEdit'
  import { translateTitle as __ } from '@/utils/i18n'
  import add from './components/UserAccountEdit'

  export default {
    name: 'UserManagement',
    components: { Edit, add },
    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,
          user_account: '',
          is_admin: 'sys',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },

      handleEdit(row) {
        if (row.user_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleEdit1(row) {
        if (row.user_id) {
          this.$refs['add'].showEdit(row)
        } else {
          this.$refs['add'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.user_id) {
          if (row.store_ids != '' && row.store_ids.length > 0) {
            this.$baseAlert('该用户为店铺账户，不能删除！')
            return
          }
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg, status } = await doDelete({ user_ids: row.user_id })
            this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            let isStore = false
            for (let i = 0; i < this.selectRows.length; i++) {
              if (
                this.selectRows[i].store_ids != '' &&
                this.selectRows[i].store_ids.length > 0
              ) {
                isStore = true
                break
              }
            }
            if (isStore) {
              this.$baseAlert('您选中的用户包含店铺账户，不能删除！')
              return
            }
            const user_ids = this.selectRows.map((item) => item.user_id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg, status } = await doDelete({ user_ids })
              this.$baseMessage(
                msg,
                'success',
                false,
                'vab-hey-message-success'
              )
              await this.fetchData()
            })
          } else {
            this.$baseMessage(
              '未选中任何行',
              'error',
              false,
              'vab-hey-message-error'
            )
            return false
          }
        }
      },
      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>
