<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>
            <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
          prop="companyName"
          label="公司名称"/>
        <el-table-column
          prop="companyInfo"
          label="公司介绍"/>
        <el-table-column
          :formatter="formatStatus"
          prop="status"
          label="状态"
        />
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handleApprove(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>审核开发者</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="submitApprove(0)">拒 绝</el-button>
          <el-button type="primary" @click="submitApprove(1)">通 过</el-button>
        </span>
      </el-dialog>

    </div>
  </div>
</template>

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

export default {
  name: 'Index',
  computed: {
    APP_CONFIG() {
      return APP_CONFIG
    }
  },
  data() {
    return {
      status: '',
      account: '',
      tag: '',
      select_value: '', // 用户状态
      options: [{
        value: 1,
        label: '正常'
      }, {
        value: 0,
        label: '已封号'
      }],
      roles: [],
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      dialogFormVisible: false, // 封号/解封框
      addFormVisible: false, // 新增管理用户
      editFormVisible: false, // 编辑用户
      formLabelWidth: '100px', // 弹出框form宽
      form: {
        account: '',
        password: '',
        passwordconfirm: '',
        role: '',
        mobilephone: '',
        name: '',
        status: '',
        email: ''
      },
      editForm: {
        id: '',
        account: '',
        role: '',
        mobilephone: '',
        name: '',
        status: '',
        email: ''
      }
    }
  },
  created() {
    this.btn_search()
  },
  mounted() {
  },
  methods: {
    handleApprove(row) {
      this.id = row.id
      this.dialogFormVisible = true
    },
    submitApprove(status) {
      var vm = this
      var params = new URLSearchParams()
      params.append('id', vm.id)
      params.append('status', status)
      axios.post(process.env.BASE_API + '/user/approve', 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()
        }
      }).catch(function(error) {
        alert(error)
      })
      this.dialogFormVisible = false
    },
    formatStatus(ob) {
      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/listdev', {
        params: {
          tag: this.tag,
          status: 2
        }
      }).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.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)
    },
    handleEdit(row) {
      this.editFormVisible = true
      this.editForm.id = row.id
      this.editForm.account = row.account
      this.editForm.role = row.role.id
      this.editForm.name = row.name
      this.editForm.mobilephone = row.phone
      this.editForm.email = row.email
    },
    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
    },
    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_submit() {
      if (this.editForm.account === '' || this.editForm.account === undefined) {
        this.$message.error('请输入账号，账号不能为空')
        return
      }
      /* if (this.editForm.role === '' || this.editForm.role === 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('email', vm.editForm.email)
      params.append('name', vm.editForm.name)
      params.append('phone', vm.editForm.mobilephone)
      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>
