<template>
  <div class="default-main ">
    <div style="display: flex;justify-content: space-between;background: #ffffff;padding: 20px 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>


    <el-table :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="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="销售金额" width="100">
        <template #default="{ row }">
          <span v-if="row.isSale == 1" style="margin: 0 5px;">{{ row.salePrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出售状态" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.isSale == 1" :type="row.saleStatus == 1 ? 'info' : 'success'" style="margin: 0 5px;">{{ row.saleStatus
            == 1 ? '已售' : '待售' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="租金" width="100">
        <template #default="{ row }">
          <span v-if="row.isHire == 1" style="margin: 0 5px;">{{ row.hirePrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出租状态" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.isHire == 1" :type="row.hireStatus == 1 ? 'info' : 'success'" style="margin: 0 5px;">{{ row.hireStatus
            == 1 ? '已租' : '待租' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否标记" width="100">
        <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 20px"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :background = "true"
      layout="sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <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, community_list, district_list, parking } from "/@/api/controllerUrls";
import { onMounted, ref } from "vue";
import editDialog from "./editDialog.vue"
import markDialog from "./markDialog.vue"
import detailDialog from "./detailDialog.vue"


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

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()
}


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
    }
  });
}

const handleSizeChange = (val: number) => {
  getData()
}
const handleCurrentChange = (val: number) => {
  getData()
}

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>
