<template>
  <div>
    <div>
      <el-form :inline="true" class="orderform" size="mini">
        <el-row>
          <el-form-item label="账号/账号名称:">
            <el-input v-model="tag" style="width: 250px" placeholder="请输入..." class="search_tag" clearable/>
          </el-form-item>
          <el-form-item label="用户状态:">
            <el-select v-model="select_value" clearable placeholder="请选择" size="mini">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <!--el-form-item label="用户角色:">
            <el-select v-model="select_role" clearable placeholder="请选择" size="mini">
              <el-option
                v-for="item in roles"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item-->
          <el-form-item>
            <el-button type="primary" @click="btn_search()"><i class="el-icon-search"/> 搜索</el-button>
            <el-button @click="btn_clear()"><i class="el-icon-remove-outline"/> 重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        :header-cell-style="{color:APP_CONFIG.TABLE_DOMINANT_COLOR}"
        :cell-style="{color:APP_CONFIG.TABLE_ASSISTANT_COLOR}"
        border>
        <el-table-column
          prop="id"
          label="序列号"/>
        <el-table-column
          prop="account"
          label="帐号"/>
        <el-table-column
          prop="name"
          label="账号名称"/>
        <el-table-column
          prop="role.roleName"
          label="角色"/>
        <el-table-column
          prop="phone"
          label="电话"/>
        <el-table-column
          prop="email"
          label="邮箱"/>
        <el-table-column
          :formatter="formatStatus"
          prop="status"
          label="状态"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="300"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleActivate(scope.row)">封号/解封</el-button>
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            <el-button type="text" icon="el-icon-lock" @click="initPwd(scope.row)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mydiv">
        <el-pagination
          :current-page.sync="pageNo"
          :page-sizes="[10, 50, 100, 300,1000]"
          :page-size="pageSize"
          :total="totalDataNumber"
          layout="sizes,total, prev, pager, next"
          class="table-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>

      <el-dialog
        :visible.sync="dialogFormVisible"
        title="提示"
        width="30%">
        <span v-if="status > 0">确定对选择用户进行 <b>封号</b> 处理? </span>
        <span v-if="status === 0">确定对选择用户进行 <b>解封</b> 处理? </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        :visible.sync="dialogDeleteFormVisible"
        title="提示"
        width="30%">
        <span>确定对选择用户进行删除处理? </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleDeleteSubmit">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog :visible.sync="editFormVisible" title="编辑用户">
        <el-form :model="editForm">
          <el-form-item :label-width="formLabelWidth" label="帐号">
            <el-input v-model="editForm.account" :disabled="true"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="用户状态">
            <el-select v-model="editForm.status" placeholder="请选择用户状态">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="用户角色">
            <el-select v-model="editForm.role" placeholder="请选择用户状态">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="账号名称">
            <el-input v-model="editForm.name"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="电话">
            <el-input v-model="editForm.mobilephone"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="邮箱">
            <el-input v-model="editForm.email"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="btn_update_cancel">取 消</el-button>
          <el-button type="primary" @click="btn_update_submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import moment from 'moment'
import md5 from 'js-md5'
import APP_CONFIG from "../../../public/config";

export default {
  name: 'Index',
  computed: {
    APP_CONFIG() {
      return APP_CONFIG
    }
  },
  data() {
    return {
      status: '',
      account: '',
      tag: '',
      select_value: '', // 用户状态
      select_role: '', // 用户角色
      options: [{
        value: 1,
        label: '有效'
      }, {
        value: 0,
        label: '停用'
      }, {
        value: 2,
        label: '禁用'
      }],
      roles: [],
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      dialogFormVisible: false, // 封号/解封框
      dialogDeleteFormVisible: false, // 删除
      addFormVisible: false, // 新增管理用户
      editFormVisible: false, // 编辑用户
      formLabelWidth: '100px', // 弹出框form宽
      form: {
        account: '',
        password: '',
        passwordconfirm: '',
        role: '',
        mobilephone: '',
        name: '',
        status: '',
        email: ''
      },
      editForm: {
        id: '',
        account: '',
        role: '',
        status: '',
        mobilephone: '',
        name: '',
        email: ''
      }
    }
  },
  created() {
    this.searchAllRoles()
    this.btn_search()
  },
  mounted() {
  },
  methods: {
    // 重置密码
    initPwd(row) {
      this.$confirm('是否将密码重置为 123456 ?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.password = md5('123456')
        const params = new URLSearchParams(row)
        axios.post(process.env.BASE_API + '/user/update', params).then((response) => {
          var obj = response.data
          if (obj.code !== 10000) {
            this.$message.error(obj.message)
          } else {
            this.$message.success('重置密码成功，密码为123456')
          }
        })
      })
    },
    searchAllRoles() {
      const vm = this
      axios.get(process.env.BASE_API + '/role/list', {
        params: {
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.roles = []
        } else {
          if (parseInt(obj.total) > 0) {
            vm.roles = obj.data
          } else {
            vm.roles = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    formatStatus(ob) {
      // 1=免费用户 0=异常用户 2=付费用户 3=VIP用户 4=测试用户)
      if (ob.status === 1) {
        return '有效'
      } else if (ob.status === 0) {
        return '停用'
      } else if (ob.status === 2) {
        return '禁用'
      } else {
        return '未知'
      }
    },
    btn_search() {
      const vm = this
      axios.get(process.env.BASE_API + '/user/list', {
        params: {
          tag: this.tag,
          status: this.select_value,
          roleType: this.select_role
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalDataNumber = 0
          vm.allData = []
          vm.tableData = []
        } else {
          // console.log(obj.data)
          vm.totalDataNumber = parseInt(obj.total)
          if (vm.totalDataNumber > 0) {
            vm.allData = obj.data
            vm.tableData = obj.data.slice(0, vm.pageSize)
          } else {
            vm.allData = []
            vm.tableData = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_clear() {
      this.tag = ''
      this.select_value = ''
      this.select_role = ''
      this.btn_search()
    },
    handleCurrentChange() {
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleActivate(row) {
      this.status = row.status
      this.account = row.account
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      this.status = row.status
      this.account = row.account
      this.dialogDeleteFormVisible = true
    },
    handleEdit(row) {
      this.editFormVisible = true
      this.editForm.id = row.id
      this.editForm.account = row.account
      this.editForm.status = row.status
      this.editForm.name = row.name
      this.editForm.mobilephone = row.phone
      this.editForm.email = row.email
      this.editForm.role = row.role.id
    },
    handleCancel() {
      this.dialogFormVisible = false
    },
    handleSubmit() {
      var vm = this
      var params = new URLSearchParams()
      params.append('account', vm.account)
      if (vm.status === 1) {
        params.append('status', 0)
      } else {
        params.append('status', 1)
      }
      axios.post(process.env.BASE_API + '/user/activteuser', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.btn_search()
        }
        vm.status = ''
      }).catch(function(error) {
        alert(error)
      })
      this.dialogFormVisible = false
    },
    handleDeleteSubmit() {
      var vm = this
      var params = new URLSearchParams()
      params.append('account', vm.account)
      params.append('status', 2)
      axios.post(process.env.BASE_API + '/user/activteuser', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.btn_search()
        }
        vm.status = ''
      }).catch(function(error) {
        alert(error)
      })
      this.dialogDeleteFormVisible = false
    },
    dateformatter(row, column) {
      var date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm')
    },
    createUser() {
      this.addFormVisible = true
    },
    btn_create_cancel() {
      this.addFormVisible = false
    },
    btn_create_submit() {
      if (this.form.account === '' || this.form.account === undefined) {
        this.$message.error('请输入账号，账号不能为空')
        return
      }
      if (this.form.password === '' || this.form.password === undefined) {
        this.$message.error('请输入密码，密码不能为空')
        return
      }
      if (this.form.passwordconfirm === '' || this.form.passwordconfirm === undefined) {
        this.$message.error('请输入密码，密码不能为空')
        return
      }
      if (this.form.password !== this.form.passwordconfirm) {
        this.$message.error('两次密码输入不一致，请重新输入')
        return
      }
      if (this.form.role === '' || this.form.role === undefined) {
        this.$message.error('请选择角色')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('account', vm.form.account)
      params.append('password', vm.form.password)
      params.append('roleId', vm.form.role)
      params.append('name', vm.form.name)
      params.append('phone', vm.form.mobilephone)
      axios.post(process.env.BASE_API + '/user/create', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.form = ''
          vm.btn_search()
        }
        vm.status = ''
      }).catch(function(error) {
        alert(error)
      })
      this.addFormVisible = false
    },
    btn_update_cancel() {
      this.editFormVisible = false
    },
    btn_update_submit() {
      if (this.editForm.account === '' || this.editForm.account === undefined) {
        this.$message.error('请输入账号，账号不能为空')
        return
      }
      if (this.editForm.status === '' || this.editForm.status === undefined) {
        this.$message.error('请选择用户状态')
        return
      }
      if (this.editForm.role === '' || this.editForm.role === undefined) {
        this.$message.error('请选择用户角色')
        return
      }
      var regEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
      if (this.editForm.email === '') {
        this.$message.info('请输入邮箱')
        return
      } else if (!regEmail.test(this.editForm.email)) {
        this.$message.info('邮箱格式不正确')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('id', vm.editForm.id)
      params.append('account', vm.editForm.account)
      params.append('email', vm.editForm.email)
      params.append('name', vm.editForm.name)
      params.append('status', vm.editForm.status)
      params.append('phone', vm.editForm.mobilephone)
      params.append('roleId', vm.editForm.role)
      axios.post(process.env.BASE_API + '/user/update', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.form = ''
          vm.btn_search()
        }
        vm.status = ''
      }).catch(function(error) {
        alert(error)
      })
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
  .orderform {
    padding-top: 20px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .mydiv {
    float: right;
    margin-right: 10px;
  }
</style>
