<template>
  <div>
    <Modal v-model="maintainerNamesModal" width="430">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <Row>
        <!--维护实际时间-->
        <Col span="24" />
        <Form
          id="edit-time-names-forms"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          inline
          onsubmit="return false"
          :label-width="130"
        >
          <Form-item label="维护实际保养人" prop="checkActName">
            <Select
              v-model="formObj.checkActName"
              filterable
              clearable
              placeholder="请选择实际保养人"
              style="width:245px"
              @on-result="_maintainerNamesChange"
            >
              <Option v-for="item in checkActNameList" :key="item.name" :value="item.name">
                {{ item.name }}
              </Option>
            </Select>
          </Form-item>
        </Form>
        </Col>
      </Row>
      <div slot="footer">
        <Button type="ghost" style="margin-left: 8px" @click="_cancel">
          取消
        </Button>
        <Button type="info" @click="_ok">
          确定
        </Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import Global from '../../../../api/config'

const defVal = {
  checkActName: ''
}
export default {
  name: 'LmsMaintainStartTimeAct',

  data() {
    return {
      maintainerNamesModal: false,
      modalTitle: '',
      types: '',
      selectIds: [],
      checkActNameList: [],
      formObj: defVal,
      checkId: '',
      ruleValidate: {
        maintainerNames: [
          {
            required: true,
            message: '维护实际保养人不能为空',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    _open(ids, checkId) {
      this.$refs.formObj.resetFields()
      this.checkId = checkId
      this.modalTitle = '维护实际保养人'
      this._openSearchEquipMaintainer()
      this.maintainerNamesModal = true
      this.selectIds = ids
    },
    _cancel() {
      this.maintainerNamesModal = false
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          this._resultChange('保存成功!')
        }
      })
    },
    _resultChange(msg) {
      this.maintainerNamesModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change', this.formObj.checkActName, 'checkActName')
    },
    _openSearchEquipMaintainer() {
      this.$store.dispatch('LmsMarshalManage/userPage').then(() => {
        const registrantList = this.$store.state.LmsMarshalManage.userPage
          .records
        const maintainerArray = []
        for (let i = 0; i < registrantList.length; i++) {
          maintainerArray.push({ name: registrantList[i].realname })
        }
        if (maintainerArray.indexOf(this.formObj.realname) === -1) {
          maintainerArray.push({ name: Global.getUserInfo().realname })
        }
        this.checkActNameList = maintainerArray
      })
    },
    _maintainerNamesChange(data) {
      this.formObj.checkActName = data
    }
  }
}
</script>

<style scoped>
</style>
