<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="40%"
    @close="close"
  >
    <el-form
      ref="form"
      v-loading="loading"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('用户名')" prop="user_account">
        <el-input
          v-model="form.user_account"
          clearable
          :placeholder="__('请输入用户名')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('密码')" prop="user_password">
        <el-input
          v-model="form.user_password"
          clearable
          :placeholder="__('请输入密码')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('用户昵称')" prop="user_nickname">
        <el-input
          v-model="form.user_nickname"
          clearable
          :placeholder="__('请输入用户昵称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="user_state">
        <el-radio-group v-model="form.user_state" size="medium">
          <el-radio
            v-for="(item, index) in user_stateOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="__('头像')" prop="user_avatar" required>
        <upload
          height="50px"
          :image="form.user_avatar"
          width="50px"
          @upImage="upVoucherImage"
        />
      </el-form-item>
      <el-form-item label="" prop="user_gender">
        <el-radio-group v-model="form.user_gender" size="medium">
          <el-radio
            v-for="(item, index) in user_genderOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="__('手机号码')" prop="user_mobile">
        <el-input
          v-model="form.user_mobile"
          clearable
          :placeholder="__('请输入手机号码')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import Upload from '@/components/upload'
  import { doEdit } from '@/api/chain/user/base'

  export default {
    name: 'ChainUserEdit',
    components: { Upload },
    data() {
      return {
        loading: false,
        form: {
          user_avatar: '',
          user_state: 2,
          user_gender: 1,
        },
        rules: {
          user_account: [
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur',
            },
          ],
          user_password: [
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur',
            },
          ],
          user_nickname: [
            {
              required: true,
              message: '请输入用户昵称',
              trigger: 'blur',
            },
          ],
          user_state: [
            {
              required: true,
              message: '不能为空',
              trigger: 'change',
            },
          ],
          user_gender: [
            {
              required: true,
              message: '不能为空',
              trigger: 'change',
            },
          ],
          user_mobile: [
            {
              required: true,
              message: '请输入手机号码',
              trigger: 'blur',
            },
          ],
        },
        user_stateOptions: [
          {
            label: this.__('锁定'),
            value: 0,
          },
          {
            label: this.__('已激活'),
            value: 2,
          },
        ],
        user_genderOptions: [
          {
            label: this.__('男'),
            value: 1,
          },
          {
            label: this.__('女'),
            value: 2,
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          user_avatar: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.loading = true
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
          this.loading = false
        })
      },
      upVoucherImage(image) {
        this.form.user_avatar = image
      },
    },
  }
</script>
