<!--
  -    Copyright (c) 2018-2025, linshenxs All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: linshenxs
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="handleDel">
                <template slot="check" slot-scope="scope">
                  <el-radio v-model="selectRow" :label="scope.row.$index"><span></span></el-radio>
                </template>
                <template slot="a" slot-scope="scope" >
                    <el-button type="text" @click="onViewList('AList', scope.row)">{{scope.row.a}}</el-button>
                </template>
                <template slot="b" slot-scope="scope" >
                    <el-button type="text" @click="onViewList('BList', scope.row)">{{scope.row.b}}</el-button>
                </template>
                <template slot="value" slot-scope="scope" >
                    <el-button type="text" @click="onViewList('ValueList', scope.row)">{{scope.row.value}}</el-button>
                </template>
                <template slot="growthValue" slot-scope="scope" >
                    <el-button type="text" @click="onViewList('GrowthList', scope.row)">{{scope.row.growthValue}}</el-button>
                </template>
                <template slot-scope="{row,index}" slot="menu">
                    <el-button
                        v-if="isPermit('general_baseuser_view')"
                        type="text"
                        icon="el-icon-view"
                        size="small"
                        @click="onViewDetails(row)">查看详情</el-button>
                </template>
                <template slot-scope="scope" slot="menuLeft">
                  <div class="layout_h">
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_show_id')"
                      type="primary"
                      size="small"
                      @click="onChange('ID')">修改ID</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_value')"
                      type="primary"
                      size="small"
                      @click="onChange('Value')">修改VIP财富值</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_growth')"
                      type="primary"
                      size="small"
                      @click="onChange('Growth')">修改成长值</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_promote')"
                      type="primary"
                      size="small"
                      @click="onChange('PromoteTrue')">设为推广员</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_promote')"
                      type="primary"
                      size="small"
                      @click="onChange('PromoteFalse')">取消推广员</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_invitor')"
                      type="primary"
                      size="small"
                      @click="onChange('Invitor')">修改邀请用户</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_a')"
                      type="primary"
                      size="small"
                      @click="onChange('A')">修改海盗币</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_b')"
                      type="primary"
                      size="small"
                      @click="onChange('B')">修改巨雾币</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_permission')"
                      type="primary"
                      size="small"
                      @click="onChange('Permission')">用户权限</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_block')"
                      type="primary"
                      size="small"
                      @click="onChange('Block')">设为禁用</el-button>
                    <el-button
                      v-if="isPermit('admin_user_mgt_change_block')"
                      type="primary"
                      size="small"
                      @click="onChange('UnBlock')">设为有效</el-button>
                  </div>
                </template>
            </avue-crud>
        </basic-container>

        <!-- 修改各种值的弹窗 -->
        <user-edit-pop :show="showEdit" :obj="row" :type="editType" @close="showEdit = false" @success="onChangeDone" />

        <!-- 海盗币，巨雾币，财富值，成长值的流水列表 -->
        <user-detail-pop :show="showList" :obj="row" :type="listType" @close="showList = false" />

    </div>
</template>

<script>
    import {fetchList, userDetails} from '@/api/userMgt/userMgt.js'
    import {tableOption} from '@/const/crud/userMgt/baseuser.js'
    import {mapGetters} from 'vuex'
    import {
      ZmMixin
    } from '@/mixins/ZmMixin.js'

    export default {
        name: 'baseuser',
        mixins: [ZmMixin],
        components: {
          'user-edit-pop': () => import('./components/UserEditPopup.vue'),
          'user-detail-pop': () => import('./components/UserDetailsPopup.vue')
        },
        data() {
            return {
                selectRow: -1,
                row: undefined,
                editType: '',
                listType: '',
                showEdit: false,
                showList: false,
                searchForm: {},
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.general_baseuser_add, false),
                    delBtn: this.vaildData(this.permissions.general_baseuser_del, false),
                    editBtn: this.vaildData(this.permissions.general_baseuser_edit, false),
                    viewBtn: this.vaildData(this.permissions.general_baseuser_view, false)
                };
            }
        },
        methods: {
           onChange(type) {
             if(this.selectRow < 0 || this.selectRow >= this.tableData.length) {
               this.warn("请选择一行数据")
               return
             }
             this.editType = type
             this.row = this.tableData[this.selectRow]
             this.showEdit = true
           },
           // 打开详情
           onViewList(type, row) {
            this.listType = type
            this.row = row
            this.showList = true
           },
           onViewDetails(row) {
             this.$router.push('/userMgt/detail?id='+row.id)
           },
           onChangeDone() {
             this.showEdit = false
             this.refreshChange()
           },
            // 列表查询
            getList(page, params) {
                this.tableLoading = true
                let p = {
                  current: page.currentPage,
                  size: page.pageSize
                }
                if(params) {
                  let range = this.parseRange(params)
                  p = {
                    ...p,
                    startTime: range[0],
                    endTime: range[1],
                    phone: params.phone,
                    nickname: params.nickname,
                    vipLevel: params.gradeName,
                    taskLevel: params.growthName,
                    isPromote: params.isPromote,
                    status: params.isBlock
                  }
                }
                fetchList(p).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            // 删除
            handleDel: function (row, index) {
                this.$confirm('是否确认删除ID为' + row.id, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.id)
                }).then(data => {
                    this.$message.success('删除成功')
                    this.getList(this.page)
                })
            },
            // 更新
            handleUpdate: function (row, index, done,loading) {
                putObj(row).then(data => {
                    this.$message.success('修改成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            // 保存
            handleSave: function (row, done,loading) {
                addObj(row).then(data => {
                    this.$message.success('添加成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            // 每页条数改变事件
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            // 当前页发生改变事件
            currentChange(current){
                this.page.currentPage = current
            },
            // 查询事件
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            // 刷新事件
            refreshChange() {
                this.getList(this.page)
            },
            //  导出excel
            exportExcel() {
                this.downBlobFile('/general/baseuser/export',this.searchForm, 'baseuser.xlsx')
            },
        }
    }
</script>
