<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="false">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-form-staff-schedule-edit" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="人员姓名" prop="name">
            <StaffAutoComplete
              :value="formObj.staffName"
              placeholder="请选择员工"
              :down-data="staffList"
              @on-result-change="_staffChange"
            />
          </Form-item>
          <Form-item label="状态" prop="shift">
            <Select v-model="formObj.status" @on-change="_shiftChange">
              <Option v-for="item in shiftList" :key="item.value" :value="item.value">
                {{ item.name }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="状态时间" prop="workDate">
            <Date-picker
              v-model="formObj.workDate"
              type="datetime"
              placeholder="状态时间"
              format="yyyy-MM-dd HH:mm"
              style="width: 100%;"
              @on-change="_dateChange"
            />
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注" clearable />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { staffStatus, staffInfo } from '../../../api'
import StaffAutoComplete from './staff-select/StaffAutoComplete'
export default {
  components: { StaffAutoComplete },
  data() {
    return {
      id: '',
      modalTitle: '添加',
      staffList: [],
      staffListTemp: [],
      formObj: {
        staffId: '',
        staffName: '',
        status: '',
        workDate: '',
        remark: ''
      },
      shiftList: [
        { value: '0', name: '出差' },
        { value: '1', name: '在岗' },
        { value: '2', name: '休假' },
        { value: '3', name: '培训' }
      ],
      ruleValidate: {
        staffName: [
          { required: true, message: '人员不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },

    _getStaffList: async function() {
      const result = await staffInfo.list({ status: 1 })
      if (result) {
        this.staffList = result
        this.staffListTemp = result
      }
      // status:1 查状态为正常的
      // this.$store.dispatch('LmsStaffInfo/list', {status: 1}).then(() => {
      //   this.staffList = this.$store.state.LmsStaffInfo.list;
      //   this.staffListTemp = this.$store.state.LmsStaffInfo.list;
      // });
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          this._save(this.formObj)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await staffStatus.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await staffStatus.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _staffChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.staffId = data.id
          this.formObj.staffName = data.name
          break
        default:
          this._staffQuery(data)
      }
    },
    _shiftChange(data) {
      this.formObj.status = data
    },
    _staffQuery(query) {
      if (query === '') {
        this.staffList = this.staffListTemp
      } else {
        this.staffList = this.staffListTemp.filter(
          item => item.name.indexOf(query) > -1
        )
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _dateChange(data) {
      this.formObj.workDate = data
    },
    _open(formObj) {
      this.$refs.formObj.resetFields()
      this.showEditModal = true
      this._hideLoading()
      this._getStaffList()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加人员状态'
      } else {
        this.id = formObj.id
        this.formObj = formObj

        if (this.formObj.workDate === undefined) {
          this.formObj.workDate = ''
        } else {
          this.formObj.workDate = new Date(this.formObj.workDate)
        }
        this.modalTitle = '编辑人员状态'
      }
    }
  }
}
</script>
