<template>
  <div class='app-container'>
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="dialogVisible = true" v-hasPermi="['user:store:add']">新增</el-button>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="账号ID" align="center" prop="id" />
      <el-table-column label="账号名称" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormatter" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['user:store:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="新增" width="500px" :before-close="cancel">
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="100px">
        <el-form-item label="账号名称：" prop="name">
          <el-input v-model="dialogForm.name" placeholder="请输入账号名称" />
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input v-model="dialogForm.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="性别：" prop="sex">
          <el-radio-group v-model="dialogForm.sex">
            <el-radio v-for="item in sexOptions.slice(0, 2)" :key="item.value" :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, submit, remove } from '@/api/user/store'
import { sexMixin } from '@/mixin/index'
export default {
  mixins: [sexMixin],
  data() {
    const checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入手机号'))
      } else {
        const reg = /^1[3-9][0-9]\d{8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 弹窗
      dialogVisible: false,
      // 弹窗表单
      dialogForm: {},
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入账户名称', trigger: 'blur'
        }],
        phone: [{
          required: true, validator: checkPhone, trigger: 'blur'
        }],
        sex: [{
          required: true, message: '请选择性别', trigger: 'change'
        }]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 取消
    cancel() {
      this.$refs.dialogForm.resetFields()
      this.dialogForm = {}
      this.dialogVisible = false
    },

    // 提交
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          submit(this.dialogForm).then(res => {
            if (res.code === 0) {
              this.$message.success('提交成功！')
              this.cancel()
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 字典翻译
    sexFormatter(row) {
      return this.selectDictLabel(this.sexOptions, row.sex)
    }
  }
}
</script>