<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form ref="searchFrom" :model="searchFrom" label-width="60px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="用户名" prop="KEYWORDS">
                <el-input v-model="searchFrom.KEYWORDS" placeholder="请输入用户名"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="10px">
                <el-button v-waves type="primary" icon="el-icon-search" class="ml-10" @click="getQuery">
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" @click="goKeyReset">
                  重置
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="app-tools">
        <div class="app-function">
          <el-button type="primary" icon="el-icon-plus" @click="fnAdd('')">新建</el-button>
        </div>
      </div>
      <div class="app-container">
        <el-table
          v-loading="listLoading"
          ref="multipleTable"
          :data="varList"
          :row-key="getRowKey"
          :header-cell-style="{
            'font-weight': 'bold',
            'color': '#000'
          }"
          tooltip-effect="dark"
          border
          fit
          highlight-current-row>
          <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
          <el-table-column type="index" label="序号" width="50" align="center"/>
          <el-table-column show-overflow-tooltip prop="USERNAME" label="用户名"/>
          <el-table-column show-overflow-tooltip prop="NAME" label="姓名"/>
          <el-table-column show-overflow-tooltip prop="PHONE" label="手机号"/>
          <el-table-column show-overflow-tooltip prop="EMAIL" label="邮箱"/>
          <el-table-column show-overflow-tooltip prop="BZ" label="备注"/>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="{row}">
              <el-button type="text" @click="fnResetPassword(row.USER_ID)">重置密码</el-button>
              <el-divider direction="vertical"/>
              <el-button type="text" @click="fnAdd(row.USER_ID)">编辑</el-button>
              <el-divider direction="vertical"/>
              <el-button type="text" @click="fnDelete(row.USER_ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page-btn-group">
          <div/>
          <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"/>
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="dialog.visible" :title="dialog.USER_ID ? '编辑' : '添加'">
      <el-form ref="from" :model="dialog.form" :rules="dialog.rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="角色" prop="ROLE_ID">
              <el-select v-model="dialog.form.ROLE_ID" style="width: 100%;">
                <el-option v-for="item in roleList" :key="item.role_ID" :label="item.role_NAME" :value="item.role_ID"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名" prop="USERNAME">
              <el-input v-model="dialog.form.USERNAME" placeholder="请输入用户名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="NAME">
              <el-input v-model="dialog.form.NAME" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="PHONE">
              <el-input v-model="dialog.form.PHONE" placeholder="请输入手机号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="EMAIL">
              <el-input v-model="dialog.form.EMAIL" placeholder="请输入邮箱"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="BZ">
              <el-input v-model="dialog.form.BZ" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.visible = false;dialog.USER_ID = ''">取消</el-button>
        <el-button type="primary" @click="fnSubmitUser">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      varList: [],
      searchFrom: {
        KEYWORDS: ''
      },
      roleList: [],
      dialog: {
        USER_ID: '',
        visible: false,
        form: {
          ROLE_ID: '',
          USERNAME: '',
          NAME: '',
          PHONE: '',
          EMAIL: '',
          BZ: ''
        },
        rules: {
          ROLE_ID: [
            { required: true, message: '请选择角色', trigger: 'blur' }
          ],
          USERNAME: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          NAME: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          PHONE: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            {
              pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/,
              message: '请输入正确的手机号',
              trigger: 'blur'
            }
          ],
          EMAIL: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入邮箱', trigger: 'blur' }
          ],
          BZ: [
            { required: true, message: '请输入备注', trigger: 'blur' }
          ]
        }
      }
    }
  },
  created() {
    this.listLoading = false
    this.getList()
  },
  methods: {
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.$refs.searchFrom.resetFields()
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    },
    getRowKey(row) {
      return row.USER_ID
    },
    getList() {
      requestFN(`/user/list?showCount=${this.listQuery.limit}&currentPage=${this.listQuery.page}`, {
        ...this.searchFrom, PERSONNELTYPE: '1'
      }).then(data => {
        this.varList = data.userList
        this.total = data.page.totalResult
        this.listLoading = false
      })
    },
    fnResetPassword(USER_ID) {
      this.$confirm('确认重置密码吗？', '提示', {
        type: 'warning'
      }).then(() => {
        requestFN(`/user/resetPwd`, {
          USER_ID
        }).then(data => {
          this.$message({
            type: 'success',
            message: '重置成功!'
          })
          this.goKeyReset()
        })
      }).catch()
    },
    fnDelete(USER_ID) {
      this.$confirm('确认删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        requestFN(`/user/deleteUser`, {
          USER_ID
        }).then(data => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.goKeyReset()
        })
      }).catch()
    },
    fnAdd(USER_ID) {
      this.dialog.visible = true
      this.$nextTick(() => {
        this.$refs.from && this.$refs.from.resetFields()
      })
      if (USER_ID) {
        requestFN(`/user/goEditUser`, {
          USER_ID
        }).then(data => {
          this.dialog.USER_ID = USER_ID
          this.dialog.form = data.pd
        })
      }
      this.fnGetRoleList()
    },
    fnGetRoleList() {
      requestFN(`/role/getRoleList`, {
      }).then(data => {
        this.roleList = data.roleList
      })
    },
    fnSubmitUser() {
      this.$refs.from.validate(valid => {
        if (valid) {
          requestFN(`${this.dialog.USER_ID != '' ? '/user/editUser' : '/user/saveUser'}`, {
            ...this.dialog.form, PERSONNELTYPE: '1'
          }).then(data => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.dialog.visible = false
            this.goKeyReset()
          })
        } else {
          this.$message.warning('请补全必填项！')
        }
      })
    }
  }
}

</script>
<style>
</style>
