<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-04 10:06:01
 * @Description:
-->
<template>
  <el-dialog
    :title="!staffData.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-row :gutter="15">
      <el-form
        ref="staffForm"
        :model="staffData"
        :rules="rules"
        size="medium"
        label-width="110px"
      >
        <el-col :span="10">
          <el-row>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.Name')" prop="name">
                <el-input v-model="staffData.name" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.Name')])" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.role')" prop="roleId">
                <el-select v-model="staffData.roleId">
                  <el-option v-for="item in roleList" :key="item.id" :value="item.id" :label="item.dataName" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.Gender')" prop="sex">
                <el-select v-model="staffData.sex" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.Gender')])" clearable :style="{width: '100%'}">
                  <el-option
                    v-for="(item, index) in sexOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                    :disabled="item.disabled"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.Telephone')" prop="telephone">
                <el-input
                  v-model="staffData.telephone"
                  :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.Telephone')])"
                  clearable
                  prefix-icon="el-icon-phone"
                  :style="{width: '100%'}"
                />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.Email')" prop="email">
                <el-input v-model="staffData.email" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.Email')])" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.visual.Remarks')" prop="description">
                <el-input v-model="staffData.description" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.Remarks')])" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="10">
          <el-row>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.EmployeeID')" prop="code">
                <el-input v-model="staffData.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.EmployeeID')])" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.Wechat')" prop="wechatCode">
                <el-input v-model="staffData.wechatCode" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.Wechat')])" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.EntryTime')" prop="entryTime">
                <el-date-picker
                  v-model="staffData.entryTime"
                  :style="{width: '100%'}"
                  :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.EntryTime')])"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.Workshop')" prop="workshop">
                <el-input v-model="staffData.workshop" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.staff.Workshop')])" clearable :style="{width: '100%'}" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item :label="$t('module.basicData.staff.onDuty')" prop="onDuty">
                <el-radio-group v-model="staffData.onDuty" size="medium">
                  <el-radio
                    v-for="(item, index) in onDutyOptions"
                    :key="index"
                    :label="item.value"
                    :disabled="item.disabled"
                  >{{ item.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" size="medium" @click="staffDataSubmit()"> {{ 'btn.save' | i18nFilter }} </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { staffDetail, staffUpdate, staffAdd, staffCode } from '@/api/basicData/GroupModule/staff'

export default {
  props: {
    roleList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      staffData: {
        name: undefined,
        sex: '',
        telephone: '',
        email: '',
        majorId: [],
        description: undefined,
        code: undefined,
        wechatCode: undefined,
        entryTime: null,
        workshop: undefined,
        onDuty: 1,
        roleId: null
      },
      rules: {
        name: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.staff.Name')]),
          trigger: 'blur'
        }],
        telephone: [{
          pattern: /(^1\d{10}$)|(^[0-9]\d{7}$)/,
          message: this.$t('module.basicData.staff.Telephone') + this.$t('module.basicData.customer.format'),
          trigger: 'blur'
        }],
        email: [{
          pattern: /^\w+@[a-z0-9]+\.[a-z]{2,4}$/,
          message: this.$t('module.basicData.staff.Email') + this.$t('module.basicData.customer.format'),
          trigger: 'blur'
        }],
        description: [],
        code: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.staff.EmployeeID')]),
          trigger: 'blur'
        }]
      },
      sexOptions: [{
        'label': '男',
        'value': 1
      }, {
        'label': '女',
        'value': 0
      }],
      onDutyOptions: [{
        'label': this.$t('module.basicData.visual.Yes'),
        'value': 1
      }, {
        'label': this.$t('module.basicData.visual.No'),
        'value': 0
      }]
    }
  },
  methods: {
    init(id) {
      staffCode().then(res => {
        this.staffData.code = res.data
      })
      this.staffData.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['staffForm'].resetFields()
        if (this.staffData.id) {
          staffDetail(this.staffData.id).then(res => {
            this.staffData = res.data
          })
        }
      })
    },
    // 表单提交
    staffDataSubmit() {
      this.$refs['staffForm'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.staffData.name,
            'sex': this.staffData.sex,
            'telephone': this.staffData.telephone,
            'description': this.staffData.description,
            'code': this.staffData.code,
            'email': this.staffData.email,
            'entryTime': this.staffData.entryTime,
            'majorId': this.staffData.majorId,
            'onDuty': this.staffData.onDuty,
            'wechatCode': this.staffData.wechatCode,
            'workshop': this.staffData.workshop,
            'roleId': this.staffData.roleId,
            'id': this.staffData.id
          }
          if (this.staffData.id) {
            staffUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            staffAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
