<template>
  <div>
    <vxe-modal
      :z-index="zIndex"
      :value="isShow"
      show-footer
      title="补卡"
      show-confirm-button
      :width="defaultModalSize.sWidth"
      :height="defaultModalSize.sHeight"
      esc-closable
      transfer
      :fullscreen="$store.getters.device === 'mobile'"
      @input="e => $emit('update:isShow',e)"
    >
      <el-form ref="elForm" :model="form" :rules="rules" label-position="top" label-width="100">
        <el-form-item label="用户" prop="user_id">
          <el-select style="width: 100%;" v-model="form.user_id">
            <el-option v-for="i in users" :key="i.id" :label="i.name" :value="i.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="日期" prop="date">
          <el-date-picker style="width: 100%;" v-model="form.date" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>

        <el-form-item label="上班打卡时间" prop="sign_in_at">
          <el-date-picker type="datetime" style="width: 100%;" v-model="form.sign_in_at" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item label="下班打卡时间" prop="sign_out_at">
          <el-date-picker type="datetime" style="width: 100%;" v-model="form.sign_out_at" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item label="类型" prop="status">
          <el-select style="width: 100%;" v-model="form.status">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="补卡" :value="2"></el-option>
            <el-option label="外勤" :value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button type="primary" :loading="loading" @click="submit">确认</el-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { updateSign } from '@/api/attendance';
import {defaultModalSize} from "@/settings";
import {PopupManager} from "element-ui/lib/utils/popup";

export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    },
    users: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      zIndex: PopupManager.nextZIndex(),
      defaultModalSize,
      loading: false,
      form: {
        user_id: '',
        date: '',
        sign_in_at: '',
        sign_out_at: '',
        status: ''
      },
      rules: {
        user_id: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        date: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        sign_in_at: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        sign_out_at: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
      }
    }
  },
  computed: {},
  methods: {
    submit() {
      this.$refs['elForm'].validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await updateSign(this.form)
            this.$message.success('新增成功')
            this.$emit('refresh')
            this.$emit('update:isShow', false)
            this.loading = false
            this.$refs['elForm'].resetFields()
          } catch (err) {
            this.loading = false
          }
        }
      })
    }
  },
  watch: {
    isShow(newVal) {
      if(newVal) {
        this.zIndex = PopupManager.nextZIndex()
      }
    }
  },
}
</script>

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