<template v-if="visible">
  <basic-container>
    <el-container style="height: 700px; border: 1px solid #eee">
      <div style="position: fixed;right: 120px;top: 25px;">
        <el-button type="danger" v-if="permission.residential_add" icon="el-icon-plus" @click="batchVisible = true">批量新增</el-button>
      </div>
      <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
        <el-tree :props="treeOption" :data="treeData" :expand-on-click-node="false" @node-click="nodeClick" default-expand-all>
           <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ node.label }}</span>
            <!--<el-popover
              placement="right"
              width="200"
              trigger="hover"
            >
              <span slot="reference">
                <i class="el-icon-arrow-down"></i>
              </span>
              <div style="display: grid">
                <el-link :underline="false" type="primary">添加下一级</el-link>
                <el-link :underline="false" type="primary">修改</el-link>
                <el-link :underline="false" type="danger">删除</el-link>
              </div>
            </el-popover>-->
          </span>
        </el-tree>
      </el-aside>
      <el-main>
        <avue-crud
          :data="data"
          :option="option"
          :table-loading="loading"
          :permission="permissionDataList"
          ref="crud"
          v-model="form"
          :before-open="beforeOpen"
          @row-update="rowDataUpdate"
          @row-save="rowDataSave"
          @row-del="rowDataDel"
        >
        </avue-crud>
      </el-main>
    </el-container>

    <!-- <el-row>
       <el-col style="margin-right: 5px; padding-right: 10px; width: 25%;border: 1px solid #0073EB;">
         <el-tree :props="treeOption" :data="treeData" :expand-on-click-node="false"   @node-click="nodeClick"></el-tree>
       </el-col>
      <el-col style="width: 70%;">
         <avue-crud
           :data="data"
           :option="option"
           :table-loading="loading"
           :permission="permissionDataList"
           ref="crud"
           v-model="form"
           :before-open="beforeOpen"
           @row-update="rowDataUpdate"
           @row-save="rowDataSave"
           @row-del="rowDataDel"
         >
         </avue-crud>
       </el-col>
     </el-row> -->

    <el-dialog title="批量新增" :append-to-body="true" :close-on-click-modal="false" :modal-append-to-body="false" width="90%" :visible.sync="batchVisible" center>
      <el-form :inline="true" :rules="batchRules" ref="batchForm" :model="batchForm" label-width="100px">
        <el-row>
          <el-form-item label="楼栋区间" required>
            <el-col :span="11">
              <el-form-item prop="buildingBegin">
                <el-input v-model="batchForm.buildingBegin"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="buildingEnd">
                <el-input v-model="batchForm.buildingEnd"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="前后缀规则">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-input placeholder="前缀" v-model="batchForm.buildingPrefix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="date2">
                <el-input placeholder="后缀" v-model="batchForm.buildingSuffix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="单元区间" required>
            <el-col :span="11">
              <el-form-item prop="unitBegin">
                <el-input v-model="batchForm.unitBegin"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="unitEnd">
                <el-input v-model="batchForm.unitEnd"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="前后缀规则">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-input placeholder="前缀" v-model="batchForm.unitPrefix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="date2">
                <el-input placeholder="后缀" v-model="batchForm.unitSuffix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="楼层区间" required>
            <el-col :span="11">
              <el-form-item prop="floorBegin">
                <el-input v-model="batchForm.floorBegin"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="floorEnd">
                <el-input v-model="batchForm.floorEnd"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="前后缀规则">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-input placeholder="前缀" v-model="batchForm.floorPrefix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="date2">
                <el-input placeholder="后缀" v-model="batchForm.floorSuffix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="排除楼层" :span="2">
            <el-input style="width: 100%;" v-model="batchForm.floorExclude"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="房屋区间" required>
            <el-col :span="11">
              <el-form-item prop="roomBegin">
                <el-input v-model="batchForm.roomBegin"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="roomEnd">
                <el-input v-model="batchForm.roomEnd"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="前后缀规则">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-input placeholder="前缀" v-model="batchForm.roomPrefix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" style="width: 15px" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="date2">
                <el-input placeholder="后缀" v-model="batchForm.roomSuffix" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="排除房屋" :span="2">
            <el-input style="width: 100%;" v-model="batchForm.roomExclude"></el-input>
          </el-form-item>
        </el-row>
        <div style="text-align: center">
          <el-button type="primary" @click="batchAdd" :disabled="batchDisabled" :icon="batchIcon">{{batchText}}</el-button>
        </div>
      </el-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import { getList as getBuildingListByResidentialOldId,add as buildingAdd,getDetail as buildingGetDetail,update as buildingUpdate,remove as buildingRemove } from '../../api/community/building.js';
  import { getList as getUnitListByBuildingOldId } from "../../api/community/unit.js";
  import { getList as getFloorListByUnitOldId } from "../../api/community/floor.js";
  import { getList as getRoomListByFloorOldId } from "../../api/community/room.js";
  import { mapGetters } from 'vuex';
  import Log from '../../api/tool/MyLog.js';
  import ArrUtil from '../../api/tool/ArrUtil.js';

  import {batchAddSubmit} from "@/api/community/building";

  export default {
    props:{
      residentialId: "",
      residentialName: "",
      visible: false
    },
    data() {
      return {
        permissionDataList: [],
        loading: false,
        batchVisible: false,
        batchIcon: 'el-icon-circle-plus-outline',
        batchDisabled: false,
        batchText: '提交',
        typeParent: 'parent',
        typeBuilding: 'building',
        typeUnit: 'unit',
        typeFloor: 'floor',
        typeRoom: 'room',
        form: {},
        batchForm: {},
        batchRules: {
          buildingBegin: [
            { required: true, message: '请输入楼栋区间', trigger: 'blur' },
          ],
          buildingEnd: [
            { required: true, message: '请输入楼栋区间', trigger: 'blur' },
          ],
          unitBegin: [
            { required: true, message: '请输入单元区间', trigger: 'blur' },
          ],
          unitEnd: [
            { required: true, message: '请输入单元区间', trigger: 'blur' },
          ],
          floorBegin: [
            { required: true, message: '请输入楼层区间', trigger: 'blur' },
          ],
          floorEnd: [
            { required: true, message: '请输入楼层区间', trigger: 'blur' },
          ],
          roomBegin: [
            { required: true, message: '请输入房屋区间', trigger: 'blur' },
          ],
          roomEnd: [
            { required: true, message: '请输入房屋区间', trigger: 'blur' },
          ],
        },
        treeData: [],
        treeOption:{
          label:"name",
          value: "id",
          children: "children"
        },


        // treeOption: {
        //   nodeKey: 'id',
        //   addBtn: false,
        //   menu: false,
        //   props: {
        //     label: 'name',
        //     value: 'id',
        //     children: 'children'
        //   }
        // },
        optionBuilding: {
          labelWidth: 100,
          tip: false,
          viewBtn: true,
          selection: false,
          border: true,
          addBtn: false,
          menuWidth:350,
          column: [
            {
              label: '楼栋名称',
              prop: 'name',
              rules: [
                {
                  required: true,
                  message: '请输入楼房名称',
                  trigger: 'blur'
                }
              ]
            },
            {

              prop: 'residentialId',
              display: false,
              hide: true,
              rules: [
                {
                  required: true,
                  message: '请输入楼房名称',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: "图片",
              prop: "pic",
              type: 'upload',
              hide: true,
              propsHttp: {
                url: 'data'
              },
              listType: 'picture-img',
              span: 24,
              row: true,
              action: "/api/blade-resource/oss/endpoint/put-file",
              display: true,
              rules: [{
                required: false,
                message: "请输入封面图片",
                trigger: "blur"
              }]
            },
            {
              label: '单元总数',
              prop: 'unitCount',
              display: false,
              rules: [
                {
                  required: false,
                  message: '请输入楼房编号',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: '楼层总数',
              prop: 'floorCount',
              display: false,
              rules: [
                {
                  required: true,
                  message: '请输入楼房层数',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: '房间总数',
              prop: 'roomCount',
              display: false,
              rules: [
                {
                  required: true,
                  message: '请输入房间数量',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: '住户总数',
              prop: 'userCount',
              hide: true,
              display: false,
              rules: [
                {
                  required: false,
                  message: '请输入楼房地址',
                  trigger: 'blur'
                }
              ]
            },
            {
              prop: 'type',
              value: 'building',
              disabled: true,
              hide: true,
              display: false
            }
          ]
        },
        optionUnit: {
          labelWidth: 100,
          tip: false,
          // addBtn: true,
          viewBtn: true,
          selection: false,
          border: true,
          menuWidth:350,
          column: [
            {
              label: "所属社区",
              prop: "agencyId",
              hide: true,
              display: false,
              // type:"select",
              // dicUrl:'',
              rules: [{
                required: true,
                message: "请选择所属社区",
                trigger: "blur"
              }]
            },
            {
              label: "所属小区",
              prop: "residentialId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            },
            {
              label: '单元名称',
              prop: 'name',
              maxlength: 10,
              showWordLimit: true,
              rules: [
                {
                  required: true,
                  message: '请输入楼层名称',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: "图片",
              prop: "pic",
              type: 'upload',
              hide: true,
              propsHttp: {
                url: 'data'
              },
              listType: 'picture-img',
              span: 24,
              row: true,

              action: "/api/blade-resource/oss/endpoint/put-file",
              display: true,
              rules: [{
                required: false,
                message: "请输入封面图片",
                trigger: "blur"
              }]
            },
            {
              label: "负责人姓名",
              prop: "personName",
              labelWidth: 110,
              hide: true,
              rules: [{
                required: true,
                message: "请输入负责人姓名",
                trigger: "blur"
              }]
            }, {
              label: "负责人电话",
              prop: "personTel",
              labelWidth: 110,
              hide: true,
              rules: [{
                required: true,
                message: "请输入负责人电话",
                trigger: "blur"
              }]
            }, {
              label: "建筑年代",
              prop: "builtYear",
              hide: true,
            }, {
              label: "产权性质",
              prop: "propertyNature",
              hide: true,
            },
            // {
            //   label: "有无电梯",
            //   prop: "includingElevator",
            //   hide: true,
            //   type: "select",
            //   search: true,
            //   dicUrl: "/api/blade-system/dict-biz/dictionary?code=including_elevator",
            //   props: {
            //     label: "dictValue",
            //     value: "dictKey"
            //   },
            // },
            {
              label: "楼层数量",
              prop: "floorCount",
              display: false
            },
            {
              label: "房间数量",
              prop: "roomCount",
              display: false
            },
            {
              label: "住户数量",
              prop: "userCount",
              display: false,
              hide: true,
            },
            {
              prop: 'type',
              value: 'unit',
              disabled: true,
              hide: true,
              display: false,
            }
          ]
        },
        optionFloor: {
          labelWidth: 100,
          tip: false,
          viewBtn: true,
          selection: false,
          border: true,
          menuWidth:350,
          column: [
            {
              label: '所属单元',
              prop: 'unitId',
              type: 'select',
              dicData: [],
              disabled: true,
              hide: true,
              props: {
                label: 'name',
                value: 'id'
              },
              rules: [
                {
                  required: true,
                  message: '请选择所属楼房',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: "所属小区",
              prop: "residentialId",
              hide: true,
              // slot: true,
              cascaderItem: ['buildingId'],
              type: "select",
              dicUrl: "/api/cyzh-community/residential/list?size=9000000",
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              dicFormatter: (res) => {
                return res.data.records;
              },
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              // slot: true,
              type: "select",
              dicUrl: `/api/cyzh-community/building/list?size=500&residentialId={{key}}`,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              dicFormatter: (res) => {
                return res.data.records;
              },
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            },
            {
              label: '楼层名称',
              prop: 'name',
              rules: [
                {
                  required: true,
                  message: '请输入房间名称',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: '房间类型',
              prop: 'roomTypeId',
              type: 'select',
              hide: true,
              dicData: [],
              props: {
                label: 'name',
                value: 'id'
              },
              rules: [
                {
                  required: true,
                  message: '请输入房间类型',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: "负责人姓名",
              prop: "personName",
              hide: true,
              rules: [{
                required: true,
                message: "请输入负责人姓名",
                trigger: "blur"
              }]
            },
            {
              label: "负责人电话",
              prop: "personTel",
              hide: true,
              rules: [{
                required: true,
                message: "请输入负责人电话",
                trigger: "blur"
              }]
            },
            {
              label: "建筑年代",
              prop: "builtYear",
              hide: true,
              rules: [{
                required: true,
                message: "请输入建筑年代",
                trigger: "blur"
              }]
            },
            {
              label: "产权性质",
              prop: "propertyNature",
              hide: true,
              rules: [{
                required: true,
                message: "请输入产权性质",
                trigger: "blur"
              }]
            },
            {
              label: "有无电梯",
              prop: "includingElevator",
              hide: true,
              rules: [{
                required: true,
                message: "请输入有无电梯",
                trigger: "blur"
              }]
            },
            {
              label: "房间数量",
              prop: "roomCount",

              rules: [{
                required: true,
                message: "请输入房间数量",
                trigger: "blur"
              }]
            },
            {
              label: "住户数量",
              prop: "userCount",
              hide: true,
              rules: [{
                required: true,
                message: "请输入住户数量",
                trigger: "blur"
              }]
            },
            {
              prop: 'type',
              value: 'buildingRoom',
              disabled: true,
              hide: true,
              display: false,
            }
          ]
        },
        optionRoom: {
          labelWidth: 100,
          tip: false,
          viewBtn: true,
          selection: false,
          border: true,
          menuWidth:350,
          column: [
            {
              label: "门牌号 ",
              prop: "name",
              // search: true,
              rules: [{
                required: true,
                message: "请输入门牌号-限制数字，单元楼下唯一  ",
                trigger: "blur"
              }]
            },{
              label: "所属社区",
              prop: "agencyId",
              slot: true,
              hide: true,
              type: "select",
              searchFilterable: true,
              dicUrl: "/api/cyzh-community/agency/list",
              cascaderItem: ['residentialId','unitId'],
              props: {
                label: "name",
                value: "id"
              },
              dicFormatter: (res)=>{
                return res.data.records;
              },
              rules: [{
                required: true,
                message: "请选择所属社区",
                trigger: "blur"
              }]
            },{
              label: "所属小区",
              prop: "residentialId",
              // slot: true,
              type: "select",
              hide: true,
              cascaderIndex:0,
              searchFilterable:true,
              // dicUrl: "/grid/estate/getResidentialList",
              dicUrl: "/api/cyzh-community/residential/list?agencyId={{key}}",
              dicFormatter: (res)=>{
                return res.data.records;
              },
              dicFlag: false,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属小区名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              // slot: true,
              hide: true,
              type: "select",
              dicFlag: false,
              searchFilterable:true,
              cascaderIndex:1,
              dicUrl: "/api/cyzh-community/building/list?residentialId={{key}}",
              dicFormatter: (res)=>{
                return res.data.records;
              },
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属单元",
              prop: "unitId",
              // slot: true,
              type: "select",
              dicFlag: false,
              cascaderIndex:1,
              searchFilterable:true,
              // dicUrl: "/api/cyzh-community/residential/list?residentialId={{key}}",
              dicUrl: "/api/cyzh-community/residential/list?id={{key}}",
              dicFormatter: (res)=>{
                return res.data.records;
              },
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼层",
              prop: "floorId",
              // slot: true,
              hide: true,
              type: "select",
              dicFlag: false,
              cascaderIndex:1,
              dicUrl: "/api/cyzh-community/unit/list?residentialId={{key}}",
              dicFormatter: (res)=>{
                return res.data.records;
              },
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "房屋面积",
              prop: "area",
              hide: true,
            },{
              label: "入住用户数量",
              prop: "userCount",
              display: false,
            },
            {
              label: "户型",
              prop: "houseType",
              hide: true,
              type: "select",
              // search: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=house_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
            },
            {
              label: "楼层",
              prop: "floor",
              hide: true,
            },
            {
              label: "装饰",
              prop: "ornament",
              hide: true,
            },
            {
              label: "房屋朝向",
              prop: "orientation",
              hide: true,
            },{
              label: "序号",
              prop: "serial",
              value: 1,
              hide: true,
              display: false,
            }, {
              label: "备注",
              prop: "remark",
              hide: true,
            },
            {
              label: "创建时间",
              prop: "createTime",
              hide: true,
              type: "datetime",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              display: false,
            },
            {
              prop: 'type',
              valueDefault: 'bed',
              disabled: true,
              hide: true,
              display: false,
            }
          ]
        },
        data: [],
        option: {
          tip: false,
          viewBtn: true,
          selection: false,
          addBtn: false,
          column: []
        }
      };
    },

    mounted() {

    },
    computed: {
      ...mapGetters(['permission']),
      permissionBuildingList() {
        return {
          addBtn: this.vaildData(this.permission.building_add, false),
          viewBtn: this.vaildData(this.permission.building_view, false),
          delBtn: this.vaildData(this.permission.building_delete, false),
          editBtn: this.vaildData(this.permission.building_edit, false)
        };
      },
      permissionBuildinglayerList() {
        return {
          addBtn: this.vaildData(this.permission.buildinglayer_add, false),
          viewBtn: this.vaildData(this.permission.buildinglayer_view, false),
          delBtn: this.vaildData(this.permission.buildinglayer_delete, false),
          editBtn: this.vaildData(this.permission.buildinglayer_edit, false)
        };
      },
      permissionBuildingroomList() {
        return {
          addBtn: this.vaildData(this.permission.buildingroom_add, false),
          viewBtn: this.vaildData(this.permission.buildingroom_view, false),
          delBtn: this.vaildData(this.permission.buildingroom_delete, false),
          editBtn: this.vaildData(this.permission.buildingroom_edit, false)
        };
      },
      permissionBedList() {
        return {
          addBtn: this.vaildData(this.permission.bed_add, false),
          viewBtn: this.vaildData(this.permission.bed_view, false),
          delBtn: this.vaildData(this.permission.bed_delete, false),
          editBtn: this.vaildData(this.permission.bed_edit, false)
        };
      },
      permissionTreeList() {
        return {
          addBtn: this.vaildData(this.permission.building_add, false),
          viewBtn: this.vaildData(this.permission.building_view, false),
          delBtn: this.vaildData(this.permission.building_delete, false),
          editBtn: this.vaildData(this.permission.building_edit, false)
        };
      },
      permissionDataList() {
        return {
          addBtn: this.vaildData(this.permission.building_add, false),
          viewBtn: this.vaildData(this.permission.building_view, false),
          delBtn: this.vaildData(this.permission.building_delete, false),
          editBtn: this.vaildData(this.permission.building_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(',');
      }
    },
    methods: {
      init(){
        this.option = this.optionBuilding;
        this.treeData = [{ id: this.residentialId, type: this.typeParent, name: this.residentialName, children: [] }];
        this.onLoadBuilding(this.residentialId);
      },
      rowDataSave(row, loading, done) {
        row.residentialId = this.residentialId;
        row.residentialName = this.residentialName;
        if (row.type == this.typeBuilding) {
          buildingAdd(row).then(
            () => {
              loading();
              this.onLoadBuilding(this.residentialId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        } else if (row.type == this.typeBuildingLayer) {
          buildingLayerAdd(row).then(
            () => {
              loading();
              this.onLoadUnit(row.buildingId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        } else if (row.type == this.typeBuildingRoom) {
          buildingRoomAdd(row).then(
            () => {
              loading();
              this.onLoadFloor(row.buildingId, row.buildingLayerId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        } else if (row.type == this.typeBed) {
          bedAdd(row).then(
            () => {
              loading();
              this.onLoadRoom(row.buildingId, row.buildingLayerId, row.buildingRoomId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        }
      },
      rowDataUpdate(row, index, loading, done) {
        if (row.type == this.typeBuilding) {
          buildingUpdate(row).then(
            () => {
              loading();
              this.onLoadBuilding(this.residentialId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        } else if (row.type == this.typeBuildingLayer) {
          buildingLayerUpdate(row).then(
            () => {
              loading();
              this.onLoadUnit(row.buildingId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        } else if (row.type == this.typeBuildingRoom) {
          buildingRoomUpdate(row).then(
            () => {
              loading();
              this.onLoadFloor(row.buildingId, row.buildingLayerId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        } else if (row.type == this.typeBed) {
          bedUpdate(row).then(
            () => {
              loading();
              this.onLoadRoom(row.buildingId, row.buildingLayerId, row.buildingRoomId);
              this.$message({ type: 'success', message: '操作成功!' });
            },
            error => {
              done();
              Log.d(error);
            }
          );
        }
      },
      rowDataDel(row) {
        this.$confirm('确定将选择数据删除?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            Log.e(row);
            if (row.type == this.typeBuilding) {
              return buildingRemove(row.id);
            } else if (row.type == this.typeBuildingLayer) {
              return buildingLayerRemove(row.id);
            } else if (row.type == this.typeBuildingRoom) {
              return buildingRoomRemove(row.id);
            } else if (row.type == this.typeBed) {
              return bedRemove(row.id);
            }
          })
          .then(() => {
            if (row.type == this.typeBuilding) {
              this.onLoadBuilding(this.residentialId);
            } else if (row.type == this.typeBuildingLayer) {
              this.onLoadUnit(row.buildingId);
            } else if (row.type == this.typeBuildingRoom) {
              this.onLoadFloor(row.buildingId, row.buildingLayerId);
            } else if (row.type == this.typeBed) {
              this.onLoadRoom(row.buildingId, row.buildingLayerId, row.buildingRoomId);
            }
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
          });
      },
      nodeClick(data) {
        // Log.e('======data>>' + JSON.stringify(data));
        if (data.type == this.typeParent) {
          this.initBuildingDatas();
        } else if (data.type == this.typeBuilding) {
          console.log("获取单元树")
          this.initUnitDatas(data);
        } else if (data.type == this.typeUnit) {
          console.log("获取楼层树")
          console.log("所属单元名称:"+data.name)
          // debugger
          this.initFloorDatas(data);
        } else if (data.type == this.typeFloor) {
          console.log("获取房间树")
          console.log("所属楼层："+data.name)

          this.initRoomDatas(data);
        }
      },
      beforeOpen(done, type) {

        console.log('------------->>>' + type);
        console.log('------------->>>' + JSON.stringify(this.form));

        if (['edit', 'view'].includes(type)) {
          if (this.form.type == this.typeBuilding) {
            buildingGetDetail(this.form.id).then(res => {
              this.form = res.data.data;
              this.form.type = this.typeBuilding;
              this.$set(this, 'form', this.form);
            });
          } else if (this.form.type == this.typeBuildingLayer) {
            buildingLayerGetDetail(this.form.id).then(res => {
              this.form = res.data.data;
              this.form.type = this.typeBuildingLayer;
              this.$set(this, 'form', this.form);
            });
          } else if (this.form.type == this.typeBuildingRoom) {
            buildingRoomGetDetail(this.form.id).then(res => {
              this.form = res.data.data;
              this.form.type = this.typeBuildingRoom;
              this.$set(this, 'form', this.form);
            });
          } else if (this.form.type == this.typeBed) {
            bedGetDetail(this.form.id).then(res => {
              this.form = res.data.data;
              this.form.type = this.typeBed;
              this.$set(this, 'form', this.form);
            });
          }
        }else{
          this.form.pic = "/img/default/building.png"
        }
        done();
      },

      //加载右侧数据
      onLoadBuilding(id,params={}) {
        this.loading = true;

        params.residentialId = id;
        console.log("residential")
        getBuildingListByResidentialOldId(1,99999999,params).then(res => {
          this.data = res.data.data.records;
          this.initTreeDatasByBuilding(this.data);
          this.loading = false;
        });
      },
      onLoadUnit(buildingId,params={}) {
        this.loading = true;
        params.buildingId = buildingId;
        getUnitListByBuildingOldId(1,999999999,params).then(res => {
          this.data = res.data.data.records;
          this.initTreeDatasByUnit(this.data, buildingId);
          this.loading = false;
        });
      },
      onLoadFloor(buildingId, unitId,params={}) {
        this.loading = true;
        params.unitId = unitId;
        getFloorListByUnitOldId(1,999999999,params).then(res => {
          this.data = res.data.data.records;
          this.initTreeDatasByFloor(this.data, buildingId, unitId);
          this.loading = false;
        });
      },
      onLoadRoom(buildingId, unitId, floorId,params={}) {
        this.loading = true;
        params.floorId = floorId;
        console.log("楼层id:"+floorId)
        getRoomListByFloorOldId(1, 99999999, params).then(res => {
          this.data = res.data.data.records;
          // debugger
          for (let data of this.data) data.type = this.typeRoom;
          this.initTreeDatasByRoom(this.data, buildingId, unitId, floorId);
          this.loading = false;
        });
      },

      // 点击左侧树形Item后初始化右侧数据
      initBuildingDatas() {
        //初始化楼栋
        this.form = { type: this.typeBuilding };
        this.$set(this, 'form', this.form);
        this.$set(this, 'option', this.optionBuilding);
        this.onLoadBuilding(this.residentialId);
      },
      initUnitDatas(data) {
        //初始化单元
        this.form = {
          type: this.typeUnit,
          buildingOldId: data.id
        };
        this.initUnitDicData(data.id, this.optionUnit);
        this.$set(this, 'form', this.form);
        this.$set(this, 'option', this.optionUnit);
        this.onLoadUnit(data.id);
      },
      initFloorDatas(data) {
        //初始化楼层
        this.form = {
          type: this.typeFloor,
          buildingId: data.buildingId,
          unitId: data.id
        };
        this.initFloorDicDatas(data.buildingId, data.id, this.optionFloor);
        this.$set(this, 'form', this.form);
        this.$set(this, 'option', this.optionFloor);
        this.onLoadFloor(data.buildingId, data.id);
      },
      initRoomDatas(data) {
        //初始化房间
        this.form = {
          type: this.typeRoom,
          buildingId: data.buildingId,
          unitId: data.unitId,
          floorId: data.id
        };
        this.initRoomDicData(data.buildingId, data.unitId, data.id);
        this.$set(this, 'form', this.form);
        this.$set(this, 'option', this.optionRoom);

        this.onLoadRoom(data.buildingId, data.unitId, data.id);
      },

      // 点击左侧树形Item后初始化右侧选择项目数据
      initUnitDicData(buildingOldId, option) {
        if (option == null) option = this.optionUnit;
        //初始化上一级下拉数据
        for (let column of option.column) {
          if (column.prop == 'buildingId') {
            column.valueDefault = buildingOldId;
            column.dicData = this.treeData[0].children;
            break;
          }
        }
      },
      initFloorDicDatas(buildingId, unitId, option) {
        if (option == null) option = this.optionFloor;
        // this.initFloorDicData(buildingId, option);
        //初始化上一级下拉数据
        for (let column of option.column) {
          if (column.prop == 'unitId') {
            column.valueDefault = unitId;
            for (let building of this.treeData[0].children) {
              if (building.id == buildingId) {
                column.dicData = building.children;
                break;
              }
            }
            break;
          }
        }
      },
      initRoomDicData(buildingId, unitId, floorId) {
        // this.initFloorDicData(buildingId, this.optionRoom);
        this.initFloorDicDatas(buildingId, unitId, this.optionRoom);
        //初始化上一级下拉数据
        // for (let column of this.optionRoom.column) {
        //   if (column.prop == 'buildingRoomId') {
        //     column.valueDefault = roomId;
        //     for (let building of this.treeData[0].children) {
        //       if (building.id == buildingId) {
        //         for (let layer of building.children) {
        //           if (layer.id == layerId) {
        //             column.dicData = layer.children;
        //             break;
        //           }
        //         }
        //         break;
        //       }
        //     }
        //     break;
        //   }
        // }
      },

      // 初始化右侧数据后更新左侧树形数据
      initTreeDatasByBuilding(datas) {
        let treeData = [];
        if (!ArrUtil.isNullOrLength(datas)) {
          for (let data of datas) {
            data.type = this.typeBuilding;
            //初始化楼栋数据
            treeData = ArrUtil.add(treeData, {
              id: data.id,
              type: "building",
              leaf: false,
              name: data.name,
              children: [],
              tag: data
            });
          }
        }
        this.treeData[0].children = treeData;
      },
      initTreeDatasByUnit(datas, buildingId) {
        // debugger
        for (let children of this.treeData[0].children) {
          if (children.id == buildingId) {
            this.optionUnit.addBtn = children.tag.floorNum > datas.length;
            let treeData = [];
            for (let data of datas) {
              data.type = this.typeUnit;
              treeData = ArrUtil.add(treeData, {
                id: data.id,
                buildingId: buildingId,
                type: data.type,
                name: data.name,
                children: [],
                tag: data
              });
            }
            children.children = treeData;
            break;
          }
        }
      },
      initTreeDatasByFloor(datas, buildingId, unitId) {
        for (let building of this.treeData[0].children) {
          if (building.id == buildingId) {
            for (let layer of building.children) {
              if (layer.id == unitId) {
                this.optionFloor.addBtn = building.tag.roomNum > datas.length;
                let treeData = [];
                for (let data of datas) {
                  data.type = this.typeFloor;
                  treeData = ArrUtil.add(treeData, {
                    id: data.id,
                    buildingId: buildingId,
                    unitId: unitId,
                    type: data.type,
                    name: data.name,
                    children: [],
                    tag: data
                  });
                }
                layer.children = treeData;
                break;
              }
            }
            break;
          }
        }
      },
      initTreeDatasByRoom(datas, buildingId, unitId, floorId) {
        for (let building of this.treeData[0].children) {
          if (building.id == buildingId) {
            for (let unit of building.children) {
              if (unit.id == unitId) {
                for (let floor of unit.children) {
                  if (floor.id == floorId) {
                    this.optionRoom.addBtn = floor.tag.bedNum > datas.length;

                    let treeData = [];
                    for (let data of datas) {
                      data.type = this.typeRoom;
                      treeData = ArrUtil.add(treeData, {
                        id: data.id,
                        buildingId: buildingId,
                        unitId: unitId,
                        floorId: floorId,
                        type: data.type,
                        name: data.name,
                        children: [],
                        tag: data
                      });
                    }
                    floor.children = treeData;
                    break;
                  }
                }
                break;
              }
            }
            break;
          }
        }
      },
      batchAdd(){

        this.$refs['batchForm'].validate((valid) => {
          if (valid) {
            this.batchForm.residentialId = this.residentialId;
            this.batchDisabled = true;
            this.batchText = '正在创建中···';
            this.batchIcon = 'el-icon-loading';

            batchAddSubmit(this.batchForm).then(res => {
              if (res.data.code == 200){
                this.batchVisible = false;
                this.init();
              }else{
                this.batchIcon = 'el-icon-circle-plus-outline';
                this.batchText = '提交';
                this.batchDisabled = false;
              }
            });
          }
        });
      },
    }
  };
</script>
