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

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['infouser:user:add']"
        >{{$t('infoussanhuser.新增')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['infouser:user:edit']"
        >{{$t('infoussanhuser.修改')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['infouser:user:remove']"
        >{{$t('infoussanhuser.删除')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['infouser:user:export']"
        >{{$t('infoussanhuser.导出')}}</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('infoussanhuser.用户ID')" align="center" prop="userId" />
      <el-table-column :label="$t('infoussanhuser.用户名')" align="center" prop="userName" />
      <el-table-column :label="$t('infoussanhuser.手机号码')" align="center" prop="telPhone" />
      <el-table-column :label="$t('infoussanhuser.商家名称')" align="center" prop="nickName" />
      <el-table-column :label="$t('infoussanhuser.用户邮箱')" align="center" prop="email" />
      <el-table-column :label="$t('infoussanhuser.头像')" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('infoussanhuser.帐号状态')" 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('infoussanhuser.审核状态')" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditStatus==0" type="info" effect="dark">{{generateTitle('未审核')}}</el-tag>
          <el-tag v-if="scope.row.auditStatus==1" type="primary" effect="dark">{{generateTitle('已审核')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('infoussanhuser.抽成比例')" align="center" prop="commission" />
      <el-table-column :label="$t('infoussanhuser.操作')" 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('infoussanhuser.修改')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['infouser:user:remove']"
          >{{$t('infoussanhuser.删除')}}</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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'165px':'180px'">
        <el-form-item :label="$t('infoussanhuser.用户名')" prop="userName">
          <el-input v-model="form.userName" :placeholder="$t('systemuser.请输入登录用户名')" />
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.密码')" prop="password">
          <el-input v-model="form.password" :placeholder="$t('infoussanhuser.请输入密码')" />
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.商家名称')" prop="nickName">
          <el-input v-model="form.nickName" :placeholder="$t('infoussanhuser.请输入商家名称')" />
        </el-form-item>
        <el-form-item :label="$t('systemuser.负责人')" prop="fullName">
          <el-input v-model="form.fullName" :placeholder="$t('systemuser.请输入商家负责人')" />
        </el-form-item>
        <el-form-item :label="$t('systemuser.身份证/护照号')" prop="idNumber">
          <el-input v-model="form.idNumber" :placeholder="$t('systemuser.请输入身份证/护照号')" />
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.手机号码')" prop="telPhone">
          <el-input v-model="form.telPhone" :placeholder="$t('infoussanhuser.请输入手机号码')" />
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.用户邮箱')" prop="email">
          <el-input v-model="form.email" :placeholder="$t('infoussanhuser.请输入用户邮箱')" />
        </el-form-item>
        <el-form-item :label="$t('systemuser.商家类型')" prop="merchantType">
          <el-radio-group v-model="form.merchantType">
            <el-radio
              v-for="dict in dict.type.sys_merc_type"
              :key="dict.value"
              :label="dict.value"
            >{{generateTitle(dict.label)}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.头像地址')" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.邀请码')" prop="thiscode">
          <el-input v-model="form.thiscode" :placeholder="$t('systemuser.请输入邀请标识')" />
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.抽成比例')" prop="commission">
          <el-input v-model="form.commission" :placeholder="$t('infoussanhuser.例')+':0.1=10%'" />
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.附件')" prop="annex">
          <image-upload v-model="form.annex" :isShowTip="false"/>
          <!-- <image-preview style="margin-right: 20px;" v-for="item in form.annex" :src="item" width="100px" height="100px"/> -->
        </el-form-item>
        <el-form-item :label="$t('systemuser.身份证/护照照片')" prop="passportPhoto">
          <image-upload v-model="form.passportPhoto" :isShowTip="false"/>
          <!-- <image-preview style="margin-right: 20px;" v-for="item in form.BankPhoto" :src="item" width="100px" height="100px"/> -->
        </el-form-item>
        <el-form-item :label="$t('systemuser.银行信息照片')" prop="bankPhoto">
          <image-upload v-model="form.bankPhoto" :isShowTip="false"/>
          <!-- <image-preview style="margin-right: 20px;" v-for="item in form.BankPhoto" :src="item" width="100px" height="100px"/> -->
        </el-form-item>
        <el-form-item :label="$t('systemuser.商家介绍')" prop="merchantIntroduction">
          <editor v-model="form.merchantIntroduction" :min-height="192"/>
        </el-form-item>
        <el-form-item :label="$t('infoussanhuser.材料审核')" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio
              v-for="dict in dict.type.sys_user_audit"
              :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('infoussanhuser.确定')}}</el-button>
        <el-button @click="cancel">{{$t('infoussanhuser.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

  }
};
</script>
