<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('infouslist.手机号码')" prop="phone">
        <el-input
          v-model="queryParams.phone"
          :placeholder="$t('infouslist.请输入手机号码')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('infouslist.用户昵称')" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          :placeholder="$t('infouslist.请输入用户昵称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('infouslist.用户邮箱')" prop="email">
        <el-input
          v-model="queryParams.email"
          :placeholder="$t('infouslist.请输入用户邮箱')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('infouslist.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('infouslist.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['infouser:user:export']"
        >{{$t('infouslist.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('infouslist.用户ID')" align="center" prop="userId" />
      <el-table-column :label="$t('infouslist.手机号码')" align="center" prop="phone" />
      <el-table-column :label="$t('infouslist.用户昵称')" align="center" prop="nickName" />
      <el-table-column :label="$t('infouslist.用户邮箱')" align="center" prop="email" />
      <el-table-column :label="$t('infouslist.用户性别')" align="center" prop="sex">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex==0" type="primary" effect="dark">{{generateTitle('男')}}</el-tag>
          <el-tag v-if="scope.row.sex==1" type="danger" effect="dark">{{generateTitle('女')}}</el-tag>
          <el-tag v-if="scope.row.sex==3" type="info" effect="dark">{{generateTitle('未知')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('infouslist.头像')" align="center" prop="avatar">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('infouslist.积分')" align="center" prop="pointsWallet" />
      <el-table-column :label="$t('infouslist.帐号状态')" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==0" type="primary">{{generateTitle('正常')}}</el-tag>
          <el-tag v-if="scope.row.status==1" type="danger">{{generateTitle('暂停')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('infouslist.操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['infouser:user:edit']"
          >{{$t('infouslist.修改')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'160px':'160px'">
        <!-- <el-form-item label="用户类型" prop="userType">
          <el-radio-group v-model="form.userType">
            <el-radio
              v-for="dict in dict.type.info_user_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->

        <el-form-item :label="$t('infouslist.帐号状态')" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_job_status"
              :key="dict.value"
              :label="dict.value"
            >{{generateTitle(dict.label)}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('infouslist.确定')}}</el-button>
        <el-button @click="cancel">{{$t('infouslist.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/infouser/user";
import {generateTitle} from '../../../utils/i18.js'
export default {
  name: "User",
  dicts: ['info_user_type', 'sys_job_status', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType:0,
        phone: null,
        nickName: null,
        email: null,
        status: null,
        delFlag:0,
        mycode: null,
        thiscode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ],
        avatar: [
          { required: true, message: "头像地址不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: this.$t('infouslist.帐号状态不能为空'), trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        phone: null,
        nickName: null,
        userType: null,
        email: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        delFlag: null,
        cid: null,
        mycode: null,
        thiscode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('infouslist.修改用户信息');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('infouslist.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户信息编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('infouser/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
