<template>
  <div class="padding-20">
    <div class="search-box">
      <el-form ref="form" :inline="true" :model="searchForm" clearable label-width="100px" class="mt-10">
        <el-form-item label="创建时间:">
          <el-date-picker
            :editable="false"
            v-model="time"
            @change="timearr => {timearr ? (searchForm.start_time = timearr[0] + ' 00:00:00', searchForm.end_time = timearr[1] + ' 23:59:59') : searchForm.start_time = searchForm.end_time = undefined}"
            type="daterange"
            value-format="yyyy-MM-dd"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="用户姓名:">
          <el-input v-model="searchForm.user_full_name" placeholder="请输入用户姓名" clearable></el-input>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-search" type="primary" @click="searchSubmit">查询</el-button>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-plus" type="primary" @click="add" v-permission="'user_list_add'">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" stripe v-loading="tableLoading" fit class="marginT-10 order-table" border>
      <el-table-column label="工号" prop="id" width="60"></el-table-column>
      <el-table-column label="账号" prop="user_name" width="140"></el-table-column>
      <el-table-column label="姓名" prop="user_full_name" width="140"></el-table-column>
      <el-table-column label="是否管理员" prop="is_admin"
                       :formatter="row => row.is_admin == 1 ? '管理员' : '普通账号'"
                       width="120"></el-table-column>
      <el-table-column label="创建时间" prop="created_at" width="160"></el-table-column>
      <el-table-column label="所属项目" prop="user_project_name" width="140"></el-table-column>
      <el-table-column label="所属部门" prop="user_department_name" width="140"></el-table-column>
      <el-table-column label="所属角色" prop="user_role_name" width="140"></el-table-column>
      <el-table-column label="手机号" prop="user_phone" width="110"></el-table-column>
      <el-table-column label="邮箱" prop="user_email" width="200"></el-table-column>
      <el-table-column label="操作" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)" v-permission="'user_list_edit'">编辑</el-button>
          <el-button type="text" @click="modPassword(scope.row)" v-permission="'user_list_del'">修改密码</el-button>
          <el-button type="text" @click="del(scope.row)" v-permission="'user_list_del'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="marginT-20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :hide-on-single-page="true"
      :current-page="page"
      :page-size="page_size"
      :page-sizes="[10, 20, 100, 200, 300, 400]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"/>
    <detail v-if="detailsDialog.show" v-model="detailsDialog.show" :exData="detailsDialog.exData"
            @success="init"></detail>
  </div>
</template>

<script>
import page from '@/mixin/page'
import detail from './details'

export default {
  mixins: [page],
  components: {
    detail,
  },
  data () {
    return {
      detailsDialog: {
        show: false,
        exData: {}
      },
      time: [],
      searchForm: {},
      tableData: [],
      tableUrl: '/user/list'
    }
  },
  methods: {
    add () {
      this.detailsDialog.exData = {}
      this.detailsDialog.show = true
    },
    edit (row) {
      this.detailsDialog.exData = row
      this.detailsDialog.show = true
    },
    modPassword (row) {
      this.$prompt('请输入新密码', '修改密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: (value) => {
          if (!value || value.replace(/ /g, '').length < 6) {
            return false
          } else {
            return true
          }
        },
        inputErrorMessage: '至少输入6位'
      }).then(async ({ value }) => {
        const data = await this.$fetch('/api/auth/user/set_user_password', { user_password: value, id: row.id })
        if (data.code == 200) {
          this.$message.success('修改成功')
        }
      }).catch(() => {})
    },
    del (row) {
      this.$confirm('确定要删除吗', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = await this.$fetch('/api/auth/user/del', { id: row.id })
        if (data.code == 200) {
          this.$message.success('删除成功')
          this.init()
        }
      }).catch(() => {})
    }
  },
  mounted () {
    this.init()
  },
}
</script>

<style lang="scss" scoped>
</style>