<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="600px"
    @closed="closedHandler"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      size="small"
      label-width="300px"
      :rules="rules"
      :disabled="formDisabled"
      v-loading="loading"
    >
      <el-form-item
        label="站点名称(请填写单位全称)"
        prop="siteName"
      >
        <el-input
          placeholder="请输入站点名称"
          v-model="dataForm.siteName"
        />
      </el-form-item>
      <el-form-item
        label="辖区（多个院区则填写主院区所属辖区）"
        prop="area"
      >
        <el-select
          v-model="dataForm.area"
          placeholder="请选择辖区"
          style="width: 100%"
        >
          <el-option
            v-for="item in areaList"
            :label="item.name"
            :key="item.id"
            :value="item.val"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="站点位置"
        prop="siteLocation"
      >
        <el-input
          placeholder="请输入站点位置"
          v-model="dataForm.siteLocation"
        />
      </el-form-item>
      <el-form-item
        label="站点经度"
        prop="siteLongitude"
      >
        <el-input
          placeholder="请输入站点经度"
          v-model="dataForm.siteLongitude"
        />
      </el-form-item>
      <el-form-item
        label="站点纬度"
        prop="siteLatitude"
      >
        <el-input
          placeholder="请输入站点纬度"
          v-model="dataForm.siteLatitude"
        />
      </el-form-item>
      <el-form-item
        label="上级医院名称"
        prop="hospitalCode"
      >
        <el-select
          v-model="dataForm.hospitalCode"
          placeholder="请选择上级医院"
          style="width: 100%"
        >
          <el-option
            v-for="item in hospitalList"
            :label="item.hospitalName"
            :key="item.id"
            :value="item.hospitalCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="站点状态"
        prop="siteStatus"
      >
        <el-radio-group v-model="dataForm.siteStatus">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="2">停用</el-radio>
        </el-radio-group>
      </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"
        :disabled="formDisabled"
        @click="submitForm()"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { queryEmergencySiteDtoById, saveOrUpdateEmergencySite } from '@/api/site'
import { queryEmergencyHospitalList } from '@/api/common'
import { getDic } from '@/util/dict'
export default {
  computed: {
    title: function() {
      let str = '武汉市医疗急救站点信息'
      if (this.type === 'add') {
        str += '填写'
      } else if (this.type === 'update') {
        str += '修改'
      } else if (this.type === 'detail') {
        str += '详情'
      }
      return str
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      formDisabled: false,
      type: 'add',
      dataForm: {
        siteName: '',
        area: '',
        siteLocation: '',
        siteLongitude: '',
        siteLatitude: '',
        hospitalCode: '',
        siteStatus: ''
      },
      areaList: [],
      hospitalList: [],
      rules: {
        siteName: [
          { required: true, message: '请输入站点名称' },
        ],
        area: [
          { required: true, message: '请选择辖区' },
        ],
        siteStatus: [
          { required: true, message: '请选择站点状态' },
        ]
      }
    }
  },
  mounted() {
    //获取可选择的医院
    queryEmergencyHospitalList().then(res => {
      this.hospitalList = res.respData
    })
    getDic(['area']).then(res => {
      this.areaList = res.area
    })
  },
  methods: {
    init(type, id) {
      this.dialogVisible = true
      this.loading = true
      this.type = type
      this.formDisabled = type === 'detail'
      if (type === 'update' || type === 'detail') {
        if (!id) {
          this.$message.error('救护车id不能为空')
          return
        }
        queryEmergencySiteDtoById({ id: id }).then(res => {
          if (res.msg === 201) {
            this.dataForm = res.respData || {}
            this.$nextTick(() => {
              this.loading = false
              this.$refs.dataForm.clearValidate()
            })
          } else {
            this.$message.error(res.content);
          }
        })
      } else {
        this.dataForm = {}
        this.$nextTick(() => {
          this.loading = false
          this.$refs.dataForm.clearValidate()
        })
      }
    },
    submitForm() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          const params = {
            ...this.dataForm,
            auditStatus: 1
          }
          const hospital = this.hospitalList.find(i => i.hospitalCode === this.dataForm.hospitalCode)
          if (!hospital) {
            this.$message.error('无效数据');
            return
          }
          const { hospitalName } = hospital
          params.hospitalName = hospitalName

          const area = this.areaList.find(i => i.id === this.dataForm.area)
          if (!area) {
            this.$message.error('无效数据');
            return
          }
          const { name } = area
          params.areaName = name

          saveOrUpdateEmergencySite(params).then(res => {
            if (res.msg === 201) {
              this.dialogVisible = false
              this.$message.success(this.type === 'add' ? '新增成功！' : '修改成功')
              this.$emit('subsucc')
            } else {
              this.$message.error(res.content)
            }
          })
        }
      })
    },
    closedHandler() {
      this.$refs.dataForm.resetFields()
    }
  }
}
</script>

<style>
</style>