<template>
  <div>
    <div>
      <el-form :inline="true" class="orderform" size="mini">
        <el-row>
          <el-form-item>
            <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-button type="primary" icon="el-icon-plus" @click="createUser">添加</el-button-->
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        border>
        <el-table-column
          prop="id"
          label="编号"/>
        <el-table-column
          prop="uid"
          label="uid"/>
        <el-table-column
          prop="phone"
          label="电话"/>
        <el-table-column
          prop="wechatName"
          label="微信昵称"/>
        <el-table-column
          prop="validityTime"
          label="到期时间"/>
        <el-table-column
          prop="createTime"
          label="创建时间"/>
        <el-table-column
          :formatter="formatStatus"
          prop="isvip"
          label="vip信息"/>
        <el-table-column
          prop="wechatIcon"
          label="头像">
          <template slot-scope="scope">
            <img :src="scope.row.wechatIcon" min-width="40" height="40">
          </template>
        </el-table-column>
        <!--el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.account !== 'admin'" type="text" @click="handleActivate(scope.row)">封号/解封</el-button>
            <el-button type="text" @click="handleEdit(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="addFormVisible" title="新增用户">
        <el-form :model="form" :rules="rules">
          <el-form-item :label-width="formLabelWidth" label="帐号" prop="account">
            <el-input v-model="form.account" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="密码" prop="password">
            <el-input v-model="form.password"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="姓名" prop="name">
            <el-input v-model="form.name"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="电话" prop="phone">
            <el-input v-model="form.phone"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="邮箱">
            <el-input v-model="form.email"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="备注">
            <el-input v-model="form.description"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="btn_create_cancel">取 消</el-button>
          <el-button type="primary" @click="btn_create_submit">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog :visible.sync="editFormVisible" title="编辑用户">
        <el-form :model="editForm" :rules="rules">
          <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="姓名" prop="name">
            <el-input v-model="editForm.name"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="电话" prop="phone">
            <el-input v-model="editForm.phone"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="邮箱">
            <el-input v-model="editForm.email"/>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="备注">
            <el-input v-model="editForm.description"/>
          </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'

export default {
  name: 'Index',
  data() {
    return {
      rules: {
        account: [{ required: true, message: '请输入帐号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      },
      status: '',
      account: '',
      tag: '',
      select_value: '', // 用户状态
      select_role: '', // 用户角色
      options: [{
        value: 1,
        label: '免费用户'
      }, {
        value: 2,
        label: '付费用户'
      }, {
        value: 3,
        label: 'VIP用户'
      }, {
        value: 4,
        label: '测试用户'
      }, {
        value: 0,
        label: '异常用户'
      }],
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      dialogFormVisible: false, // 封号/解封框
      addFormVisible: false, // 新增管理用户
      editFormVisible: false, // 编辑用户
      formLabelWidth: '100px', // 弹出框form宽
      form: {
        account: '',
        password: '',
        role: '',
        phone: '',
        name: '',
        email: '',
        description: ''
      },
      editForm: {
        id: '',
        account: '',
        phone: '',
        name: '',
        email: '',
        description: ''
      }
    }
  },
  created() {
    this.btn_search()
  },
  mounted() {
  },
  methods: {
    formatStatus(ob) {
      if (ob.isvip === 1) {
        return '包月用户'
      } else if (ob.isvip === 2) {
        return '包年用户'
      } else {
        return '未知'
      }
    },
    btn_search() {
      const vm = this
      axios.get(process.env.BASE_API + '/userinfo/listpage', {
        params: {
          tag: this.tag,
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }
      }).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.data.totalElements)
          if (vm.totalDataNumber > 0) {
            vm.allData = obj.data.content
            vm.tableData = obj.data.content // 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)
      this.btn_search()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.btn_search()
      // 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
    },
    handleEdit(row) {
      this.editFormVisible = true
      this.editForm = row
    },
    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 + '/userinfo/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
    },
    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.name === '' || this.form.name === undefined) {
        this.$message.error('请输入姓名')
        return
      }
      if (this.form.phone === '' || this.form.phone === undefined) {
        this.$message.error('请输入手机号码')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('account', vm.form.account)
      params.append('password', md5(vm.form.password))
      params.append('name', vm.form.name)
      params.append('phone', vm.form.phone)
      params.append('email', vm.form.email)
      params.append('description', vm.form.description)
      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.password === '' || this.editForm.password === undefined) {
        this.$message.error('请输入密码，密码不能为空')
        return
      }
      if (this.editForm.name === '' || this.editForm.name === undefined) {
        this.$message.error('请输入姓名')
        return
      }
      if (this.editForm.phone === '' || this.editForm.phone === undefined) {
        this.$message.error('请输入手机号码')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('id', vm.editForm.id)
      params.append('account', vm.editForm.account)
      params.append('name', vm.editForm.name)
      params.append('phone', vm.editForm.phone)
      params.append('email', vm.editForm.email)
      params.append('description', vm.editForm.description)
      axios.post(process.env.BASE_API + '/userinfo/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>
