<template>
  <div class="default-main ">
    <div style="display: flex;justify-content: space-between;background: #ffffff;padding: 20px">
      <div style="display: flex;align-items: center">
        <!--        <span>区县：</span>-->
        <!--        <el-select v-model="districtId" placeholder="请选择区县" style="width: 150px;margin-right: 10px"-->
        <!--                   @change="selectDstrict"-->
        <!--                   :clearable="true">-->
        <!--          <el-option v-for="item in districtList" :label="item.districtName" :value="item.id"></el-option>-->
        <!--        </el-select>-->
        <!--        <span>小区：</span>-->
        <!--        <el-select v-model="communityId" placeholder="请选择小区" style="width: 150px;margin-right: 10px"-->
        <!--                   @change="selectCommunity" :clearable="true">-->
        <!--          <el-option v-for="item in communityList" :label="item.communityName" :value="item.id"></el-option>-->
        <!--        </el-select>-->
        <!--        <span>楼号：</span>-->
        <!--        <el-select v-model="buildingId" placeholder="请选择楼号" style="width: 150px;margin-right: 10px"-->
        <!--                   @change="selectBuilding"-->
        <!--                   :clearable="true">-->
        <!--          <el-option v-for="item in buildingList" :label="item.buildingNumber" :value="item.id"></el-option>-->
        <!--        </el-select>-->
        <el-button v-if="auth('add')" type="primary" :disabled="buildingId == ''"
                   @click="openDialog('add')">登记车位
        </el-button>
        <el-button v-if="auth('map')" type="primary" :disabled="buildingId == ''"
                   @click="parkingMapRef.open(buildingId)">车位绑定
        </el-button>
      </div>
      <div>
        <el-input v-model="quickSearch" placeholder="通过车位编号模糊检索" @change="getData" clearable></el-input>
      </div>
    </div>

    <div
      style="display: flex;background-color: #ffffff;width: 100%;padding: 0 20px 20px 20px;justify-content: space-between">
      <el-tree node-key="id" :expand-on-click-node="false" :data="treeData"
               style="width: 250px;border: #d0d0d0 1px solid" highlight-current @nodeClick="treeNodeClick">
        <template #default="{node,data}">
          <span>{{ data.label ? data.label : data.buildingNumber }}</span>
        </template>
      </el-tree>

      <div style="width: calc(100% - 270px);">
        <div style="color: #3f6ad8">
          <span>当前位置：</span>
          <span>{{ dir }}</span>
        </div>
        <el-table style="margin-top: 10px" :data="parkingData" border @row-dblclick="detailDialogRef.open($event)">
          <el-table-column prop="districtName" label="区县"></el-table-column>
          <el-table-column prop="communityName" label="小区"></el-table-column>
          <el-table-column prop="buildingNumber" label="楼号"></el-table-column>
          <el-table-column prop="parkingNumber" label="车位编号"></el-table-column>
          <el-table-column prop="parkingType" label="车位类型" width="100">
            <template #default="{ row }">
              <span>{{ row.parkingType == 1 ? "车库" : "车位" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="售卖类型">
            <template #default="{ row }">
              <el-tag v-if="row.isSale == 1" style="margin: 0 5px;">出售</el-tag>
              <el-tag v-if="row.isHire == 1" style="margin: 0 5px;">出租</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="销售金额">
            <template #default="{ row }">
              <span v-if="row.isSale == 1" style="margin: 0 5px;">{{ row.salePrice }}</span>
            </template>
          </el-table-column>
          <el-table-column label="出售状态">
            <template #default="{ row }">
              <el-tag v-if="row.isSale == 1" :type="row.saleStatus == 1||row.saleStatus == 2 ? 'info' : 'success'"
                      style="margin: 0 5px;">
                {{ ["待售", "已售", "预售"][row.saleStatus] }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="租金">
            <template #default="{ row }">
              <span v-if="row.isHire == 1" style="margin: 0 5px;">{{ row.hirePrice }}</span>
            </template>
          </el-table-column>
          <el-table-column label="出租状态">
            <template #default="{ row }">
              <el-tag v-if="row.isHire == 1" :type="row.hireStatus == 1||row.hireStatus == 2 ? 'info' : 'success'"
                      style="margin: 0 5px;">
                {{ ["待租", "已租", "预租"][row.hireStatus] }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="是否标记">
            <template #default="{ row }">
              <el-tag :type="row.mapId == 0 ? 'danger' : 'success'" style="margin: 0 5px;">{{ row.mapId
              == 0 ? "未标记" : "已标记" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" header-align="center">
            <template #default="{ row }">
              <div style="display: flex;justify-content: center;">
                <el-button v-if="auth('edit')" class="table-operate" size="small" type="warning"
                           @click="openDialog('edit', row)">
                  <icon name="el-icon-edit"></icon>
                </el-button>
                <el-popconfirm width="220" confirm-button-text="确定" cancel-button-text="取消" title="是否确定删除?"
                               @confirm="deleteRow(row)">
                  <template #reference>
                    <el-button v-if="auth('del')" class="table-operate" type="danger" size="small">
                      <icon name="el-icon-delete"></icon>
                    </el-button>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="background: #ffffff;padding: 20px 0px"
                       v-model:current-page="currentPage"
                       v-model:page-size="pageSize"
                       :page-sizes="[10, 20, 50, 100]"
                       :background="true"
                       layout="total,sizes, prev, pager, next, jumper"
                       :total="total"
                       @size-change="getData()"
                       @current-change="getData()"
        />
      </div>
    </div>

    <editDialog ref="editDialogRef" @close="getData"></editDialog>
    <markDialog ref="parkingMapRef" @close="getData"></markDialog>
    <detailDialog ref="detailDialogRef"></detailDialog>

  </div>
</template>

<script setup lang="ts">

import { auth } from "/@/utils/common";
import request from "/@/api/request";
import { building_list, buildingTree, community_list, district_list, parking } from "/@/api/controllerUrls";
import { computed, onMounted, ref } from "vue";
import editDialog from "./editDialog.vue";
import markDialog from "./markDialog.vue";
import detailDialog from "./detailDialog.vue";


onMounted(() => {
  getDistrictList();
  getData();
  getInitData();
});

let districtId = ref("");

let editDialogRef = ref();
let parkingMapRef = ref();
let detailDialogRef = ref();

let quickSearch = ref("");

let total = ref(0);
let currentPage = ref(1);
let pageSize = ref(10);

//区县列表
let districtList = ref([]);

function getDistrictList() {
  request.index(district_list, {}, "").then((res: any) => {
    if (res.code == 1) {
      districtList.value = res.data;
    }
  });
}

let communityId = ref("");

//小区列表
let communityList = ref([]);

//选择区县，获取小区列表
function selectDstrict(event: any) {
  request.index(community_list, {
    districtIds: event
  }, "").then((res: any) => {
    if (res.code == 1) {
      communityList.value = res.data;
      communityId.value = "";
      buildingId.value = "";
      getData();
    }
  });
}


let buildingId = ref("");
//楼号列表
let buildingList = ref([]);

//选择小区，获取楼号列表
function selectCommunity(event: any) {
  request.index(building_list, {
    communityIds: event
  }, "").then((res: any) => {
    if (res.code == 1) {
      buildingList.value = res.data;
      buildingId.value = "";
      getData();
    }
  });
}


let parkingData = ref([]);

function selectBuilding(event: any) {
  getData();
}


//树相关
let treeData = ref([]);
let dir = ref("");

function getInitData() {
  request.index(buildingTree, {}, "").then((res: any) => {
    if (res.code == 1) {
      treeData.value = res.data;
    }
  });
}

function treeNodeClick(node: any, data: any) {
  console.log(data);
  districtId.value = "";
  communityId.value = "";
  buildingId.value = "";
  if (data.level == 1) {
    districtId.value = data.data.id;
    dir.value = data.data.label;
  } else if (data.level == 2) {
    communityId.value = data.data.id;
    districtId.value = data.parent.data.id;
    dir.value = data.parent.data.label + " / " + data.data.label;
  } else {
    buildingId.value = data.data.id;
    communityId.value = data.parent.data.id;
    districtId.value = data.parent.parent.data.id;
    dir.value = data.parent.parent.data.label + " / " + data.parent.data.label + " / " + data.data.buildingNumber;
  }
  getData();
}

let defaultExpanded = computed(() => {
  let res = [];
  for (let row of treeData.value) {
    res.push(row.id);
  }
  return res;
});


function getData() {
  request.index(parking, {
    districtId: districtId.value,
    communityId: communityId.value,
    buildingId: buildingId.value,
    quickSearch: quickSearch.value,
    limit: pageSize.value,
    page: currentPage.value
  }).then((res: any) => {
    if (res.code == 1) {
      parkingData.value = res.data.list;
      total.value = +res.data.total;
    }
  });
}


function openDialog(type: string, data: any = {}) {
  editDialogRef.value.open(type, data, districtId, communityId, buildingId);
}


function deleteRow(row: any) {
  request.delete(parking, {
    ids: [row.id]
  }).then((res: any) => {
    if (res.code == 1) {
      getData();
    }
  });
}


</script>


<style scoped lang="scss">
.table-operate {
  padding: 4px 5px;
  height: auto;
}

.table-operate .icon {
  font-size: 14px !important;
  color: #ffffff !important;
}
</style>
