<template>
  <el-dialog
    title="划拨"
    :visible.sync="dialogVisible"
    width="500px"
    @closed="closedHandler"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      size="small"
      label-width="150px"
      :rules="rules"
      v-loading="loading"
    >
      <el-form-item
        label="划拨车辆使用单位"
        prop="vehicleUsingUnit"
      >
        <el-select
          v-model="dataForm.vehicleUsingUnit"
          placeholder="请选择车辆使用单位"
          style="width: 100%"
          filterable
          remote
          :loading="selectLoading"
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="item in hospitalList"
            :label="item.hospitalName"
            :key="item.id"
            :value="item.hospitalName"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        type="primary"
        size="small"
        @click="submitForm()"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { queryTransferHospitalList } from '@/api/common'
import { transferHospitalCar } from '@/api/emergencyCar'
export default {
  data() {
    return {
      dialogVisible: false,
      loading: false,
      selectLoading: false,
      row: {},
      dataForm: {
        vehicleUsingUnit: '',
      },
      hospitalList: [],
      rules: {
        vehicleUsingUnit: [
          { required: true, message: '请选择车辆使用单位' },
        ],
      }
    }
  },
  mounted() {
    //获取可选择的医院
    queryTransferHospitalList().then(res => {
      this.hospitalList = res.respData
    })
  },
  methods: {
    init(row) {
      this.dialogVisible = true
      this.loading = true
      this.row = row
      this.$nextTick(() => {
        this.loading = false
        this.$refs.dataForm.clearValidate()
      })
    },
    submitForm() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          const params = {
            ...this.dataForm,
            id: this.row.id
          }
          transferHospitalCar(params).then(res => {
            if (res.msg === 201) {
              this.dialogVisible = false
              this.$message.success('操作成功')
              this.$emit('subsucc')
            } else {
              this.$message.error(res.content)
            }
          })
        }
      })
    },
    remoteMethod(query) {
      this.selectLoading = true;
      queryTransferHospitalList(query).then(res => {
        this.hospitalList = res.respData
        this.selectLoading = false;
      })
    },
    closedHandler() {
      this.$refs.dataForm.resetFields()
    }
  }
}
</script>

<style>
</style>