<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="900px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('员工名称')" prop="chain_tn_nickname">
        <el-input
          v-model="form.chain_tn_nickname"
          :placeholder="__('员工名称')"
        />
      </el-form-item>
      <el-form-item :label="__('职位')" prop="chain_tn_nickname">
        <el-input v-model="form.chain_tn_position" :placeholder="__('职位')" />
      </el-form-item>
      <el-form-item :label="__('服务数量')" prop="chain_tn_nickname">
        <el-input
          v-model="form.chain_service_num"
          :placeholder="__('服务数量')"
        />
      </el-form-item>
      <el-form-item :label="__('好评率')" prop="chain_tn_nickname">
        <el-input
          v-model="form.chain_praise_rate"
          :placeholder="__('好评率')"
        />
      </el-form-item>
      <el-form-item :label="__('员工头像')" prop="chain_tn_avatar">
        <upload
          height="100px"
          :image="form.chain_tn_avatar"
          width="170px"
          @upImage="
            (image) => {
              form.chain_tn_avatar = image
            }
          "
        />
      </el-form-item>
      <el-form-item :label="__('手机号')" prop="chain_tn_phone">
        <el-input v-model="form.chain_tn_phone" :placeholder="__('手机号')" />
      </el-form-item>
      <el-form-item :label="__('激活状态')" prop="enable">
        <el-radio-group v-model="form.enable">
          <el-radio :label="1">{{ __('启用') }}</el-radio>
          <el-radio :label="0">{{ __('关闭') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="__('排序')" prop="chain_tn_sort">
        <el-input v-model="form.chain_tn_sort" :placeholder="__('排序')" />
      </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 Upload from '@/components/upload'
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/chain/tn/base'
  import { isPhone } from '@/utils/validate'

  export default {
    name: 'ChainTnEdit',
    components: { Upload },
    data() {
      const validatePhone = (rule, value, callback) => {
        if (!this.form.chain_tn_phone || !isPhone(this.form.chain_tn_phone)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }
      return {
        form: {
          enable: 1,
          chain_tn_avatar: '',
        },
        rules: {
          chain_tn_avatar: [
            { required: true, trigger: 'blur', message: '员工头像' },
          ],
          chain_tn_nickname: [
            { required: true, trigger: 'blur', message: '员工名称' },
          ],
          chain_tn_position: [
            { required: true, trigger: 'blur', message: '职位' },
          ],
          chain_service_num: [
            { required: true, trigger: 'blur', message: '服务数量' },
          ],
          chain_praise_rate: [
            { required: true, trigger: 'blur', message: '好评率' },
          ],
          enable: [{ required: true, trigger: 'blur', message: '激活状态' }],
          chain_tn_sort: [{ required: true, trigger: 'blur', message: '排序' }],
          chain_tn_phone: [
            {
              trigger: 'blur',
              message: '手机号码',
            },
            { validator: validatePhone, trigger: 'blur' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form.chain_tn_avatar = ''
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (status == 200) {
              this.$baseMessage(msg, 'success')
              this.$emit('fetch-data')
              this.close()
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
