<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
      :title="!dataForm.id ? '添加' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <!-- 新增和修改表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectId">
            <el-select v-model="dataForm.projectId" placeholder="请选择项目" @change="handleProjectChange" style="width: 100%">
              <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.projectName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="楼栋名称" prop="buildingId">
            <el-select v-model="dataForm.buildingId" placeholder="请选择楼栋" style="width: 100%">
              <el-option
                v-for="item in buildingList"
                :key="item.id"
                :label="item.buildingNo"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设施名称" prop="facilityName">
            <el-input v-model="dataForm.facilityName" placeholder="设施名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设施类型" prop="facilityType">
            <el-select v-model="dataForm.facilityType" placeholder="请选择设施类型" style="width: 100%">
              <el-option
                v-for="item in dict.type.facility_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设施型号" prop="model">
            <el-input v-model="dataForm.model" placeholder="设施型号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装位置" prop="installLocation">
            <el-input v-model="dataForm.installLocation" placeholder="安装位置"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装日期" prop="installDate">
            <el-date-picker v-model="dataForm.installDate" type="date" placeholder="选择安装日期" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计使用寿命" prop="serviceLife">
            <el-input-number v-model="dataForm.serviceLife" placeholder="预计使用寿命" style="width: 100%"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设施状态" prop="status">
            <el-select v-model="dataForm.status" placeholder="请选择设施状态" style="width: 100%">
              <el-option
                v-for="item in dict.type.facility_status"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注" type="textarea"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as api from '@/api/pmfacility.js'
  import * as apiByBuilding from '@/api/pmbuilding.js'
  import * as apiByProject from '@/api/pmproject.js'
  export default {
    dicts: ['facility_type','facility_status'],
    data () {
      return {
        visible: false,
        projectList: [], // 项目列表
        buildingList: [], // 楼栋列表
        dataForm: {
          id: 0,
          buildingId: '' ,
          projectId: '' ,
          facilityName: '' ,
          facilityType: '' ,
          model: '' ,
          installLocation: '' ,
          installDate: '' ,
          serviceLife: '' ,
          status: '' ,
          remark: '' ,
        },
        dataRule: {
          buildingId: [
            { required: true, message: '楼栋名称  为必填项', trigger: 'blur' }
          ],
          projectId: [
            { required: true, message: '项目名称  为必填项', trigger: 'blur' }
          ],
          facilityName: [
            { required: true, message: '设施名称  为必填项', trigger: 'blur' }
          ],
          facilityType: [
            { required: true, message: '设施类型  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '设施状态  为必填项', trigger: 'blur' }
          ],
        }
      }
    },
    mounted() {
      // 获取项目列表和楼栋列表
      this.getProjectList()
      this.getBuildingList()
    },
    methods: {
      // 获取项目列表
      getProjectList() {
        apiByProject.pmprojectListByAllApi().then(res => {
          try {
            // 处理返回数据格式，确保是数组
            this.projectList = Array.isArray(res) ? res : (res.list || [])
          } catch (processingErr) {
            this.projectList = []
          }
        }).catch(err => {
          this.projectList = []
        })
      },
      // 根据项目ID获取楼栋列表
      getBuildingList(projectId) {
        if (!projectId) {
          this.buildingList = []
          return
        }
        apiByBuilding.pmbuildingListByAllApi({ projectId }).then(res => {
          try {
            // 处理返回数据格式，确保是数组
            this.buildingList = Array.isArray(res) ? res : (res.list || [])
          } catch (processingErr) {
            this.buildingList = []
          }
        }).catch(err => {
          this.buildingList = []
        })
      },
      // 项目选择变化处理
      handleProjectChange(projectId) {
        this.dataForm.buildingId = '' // 重置楼栋选择
        this.getBuildingList(projectId)
      },
      init (id) { // 初始化表单验证规则
        this.dataForm.id = id || 0
        this.visible = true
        // 获取项目列表
        this.getProjectList()
        this.$nextTick(function() {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            api.pmfacilityDetailApi(id).then(function(res) {
              this.dataForm = res;
              // 获取该项目下的楼栋列表
              this.getBuildingList(res.projectId)
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.pmfacilityUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.pmfacilityCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
