<template>
  <div class="box-center">
    <div class="head">
      <div>智控管家管理系统</div>
      <div class="callback" @click="goback" style="cursor: pointer">返回</div>
    </div>
    <div class="hid">
      选择后台|{{
        flag == 1 ? "选择代理商" : flag == 2 ? "选择公司" : "选择楼盘"
      }}
    </div>
    <div class="content" v-if="flag == 1">
      <div class="search">
        <el-input
          placeholder="请输入内容"
          style="width: 200px; margin-left: 40px; margin-right: 30px"
          v-model="agentName"
          clearable
        >
        </el-input>
        <el-button
          style="height: 32px; line-height: 13px"
          type="primary"
          size="medium"
          @click="screengetAgentList"
          >搜索</el-button
        >
      </div>
      <div class="con">
        <div
          v-for="item in list"
          :key="item.id"
          class="tab"
          @click="chose(item)"
        >
          <div class="text-1">{{ item.agentName }}</div>
          <div class="text-2">
            服务状态:
            <span
              style="font-size: 12px"
              :style="item.residueTime > 0 ? 'color:green;' : 'color:red;'"
              >{{ item.residueTime > 0 ? "服务中" : "已过期" }}</span
            >
          </div>
          <div class="text-3">{{ (item.provinceName||'-')+'-'+(item.cityName|| "-") }}</div>
        </div>
      </div>
    </div>
    <!-- <div class="content">
      <div class="search">
        <el-input
          placeholder="请输入内容"
          style="width: 200px; margin-left: 40px; margin-right: 30px"
          v-model="agentName"
          clearable
        >
        </el-input>
        <el-button
          style="height: 32px; line-height: 13px"
          type="primary"
          size="medium"
          @click="getAgentList"
          >搜索</el-button
        >
      </div>
      <el-row :gutter="20">
        <el-col class="tab" v-for="item in list" :key="item.id">
          <div>
            <div>{{ item.agentName }}</div>
            <div>服务状态{{ item.residueTime > 0 ? "未过期" : "已过期" }}</div>
            <div>{{ item.address }}</div>
          </div>
        </el-col>
      </el-row>
    </div> -->
    <div class="content" v-if="flag == 2">
      <div class="search">
        <el-input
          placeholder="请输入内容"
          style="width: 200px; margin-left: 40px; margin-right: 30px"
          v-model="companyName"
          clearable
        >
        </el-input>
        <el-button
          style="height: 32px; line-height: 13px"
          type="primary"
          size="medium"
          @click="screengetCompanyList"
          >搜索</el-button
        >
      </div>
      <div class="con">
        <div
          v-for="item in list"
          :key="item.id"
          class="tab"
          style="height: 78px"
          @click="chose(item)"
        >
          <div class="text-1">{{ item.name }}</div>
          <!-- <div class="text-2">
            服务状态:
            <span
              style="font-size: 12px"
              :style="item.residueTime > 0 ? 'color:green;' : 'color:red;'"
              >{{ item.residueTime > 0 ? "服务中" : "已过期" }}</span
            >
          </div> -->
          <div class="text-3">区域位置:{{ (item.provinceName||'-')+'-'+(item.cityName|| "-") }}</div>
        </div>
      </div>
    </div>
    <div class="content" v-if="flag == 3">
      <div class="search">
        <span style="line-height: 32px; margin-right: 20px">地区：</span>
        <el-cascader
          :props="props"
          v-if="info.selectHouseType == 1"
          @change="locationsChange"
          placeholder="省/市"
          size="small"
          separator="/"
          v-model="provice"
          clearable
        ></el-cascader>
        <el-select
          v-model="area"
          v-if="info.selectHouseType == 2"
          clearable
          filterable
          placeholder="请选择"
          class="div-inp"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-input
          v-if="info.selectHouseType == 1"
          placeholder="请输入楼盘名称"
          style="width: 200px; margin-left: 40px; margin-right: 30px"
          v-model="propertyName"
          clearable
        >
        </el-input>
        <el-button
          style="height: 32px; line-height: 13px;margin-left:10px;"
          type="primary"
          size="medium"
          @click="screenzkhousePage"
          >搜索</el-button
        >
      </div>
      <div class="con">
        <div
          v-for="item in list"
          :key="item.id"
          class="tab"
          style="width:220px;height:130px;"
          @click="chose(item)"
        >
          <div class="text-1">{{ item.propertyName }}</div>
          <div class="text-2">楼盘类型:{{ item.houseType==0?'正式':item.houseType==1?'试用':item.houseType==2?'演示':'测试' }}</div>
          <div class="text-2">
            服务状态:
            <span
              style="font-size: 12px"
              :style="item.residueTime > 0 ? 'color:green;' : 'color:red;'"
              >{{ item.residueTime > 0 ? "服务中" : "已过期" }}</span
            >
          </div>
          <div class="text-3">{{ (item.provinceName||'-')+'-'+(item.cityName|| "-") }}</div>
        </div>
      </div>
    </div>
    <el-pagination
      style="margin: 20px auto"
      class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="10"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      hide-on-single-page
    >
    </el-pagination>
  </div>
</template>

<script>
import { getAreaList } from "@/api/modules/api";
import { getStore, setStore } from "@/util/store";
export default {
  data() {
    return {
      props: {
        lazy: true,
        checkStrictly: true,
        expandTrigger:'hover',
        async lazyLoad(node, resolve) {
          const { level } = node;
          if (level == 0) {
            // console.log(23);
            const { data } = await getAreaList({ parentId: 0 }); //获取省接口
            var nodes = data.map((item) => {
              return {
                value: item.id,
                label: item.name,
                leaf: false,
              };
            });
            resolve(nodes);
          } else if (level == 1) {
            const { data } = await getAreaList({ parentId: node.data.value }); //获取市接口
            var nodes = data.map((item) => {
              return {
                value: item.id,
                label: item.name,
                leaf: true,
              };
            });
            resolve(nodes);
          }
        },
      },
      flag: 1,
      provice: [],
      area: "",
      provinceId: "", //省id
      cityId: "", //市id
      currentPage: 1,
      propertyName: "",
      agentName: "", //代理商名称
      companyName: "",
      total: 20,
      list: [],
      options: [],
      info: {},
    };
  },
  mounted() {
    // console.log(this.$route.query)
    // 给定一个标志做判断显隐
    this.flag = this.$route.query.flag;
    // 获取用户信息
    this.info = getStore({ name: "userInfo" });
    // this.info.selectHouseType=1
    // 获取数据
    if (this.flag == 1) {
      // 获取代理商
      // return
      this.getAgentList();
    } else if (this.flag == 2) {
      // 获取公司
      this.getCompanyList();
    } else {
      this.findByUserName();
    }
  },
  computed: {},
  methods: {
    // 获取代理商
    getAgentList() {
      this.$api.api
        .zkagentPage({
          current: this.currentPage,
          pageSize: 10,
          agentName: this.agentName,
        })
        .then((res) => {
          console.log(res);
          this.list = res.data.records;
          this.total = res.data.total;
        });
    },
    screengetAgentList(){
      this.currentPage=1
      this.getAgentList()
    },
    screengetCompanyList(){
    this.currentPage=1
    this.getCompanyList()
    },
    findByUserName() {
      this.$api.api
        .findByUserName({
          username: this.info.username,
        })
        .then((res) => {
          // console.log(res);
          this.info.selectHouseType=res.selectHouseType
          // 获取楼盘
          this.zkhousePage();
          // 获取区域列表
          this.findArea();
        });
    },
    screenzkhousePage(){
      this.currentPage=1
      this.zkhousePage();
    },
    getCompanyList() {
      this.$api.api
        .zkorg({
          current: this.currentPage,
          size: 10,
          name: this.companyName,
          orgType: localStorage.getItem("orgType"),
        })
        .then((res) => {
          console.log(res);
          this.list = res.data.records;
          this.total = res.data.total;
        });
    },
    findArea() {
      this.$api.api.findArea().then((res) => {
        console.log(res);
        this.options = res.data;
      });
    },
    zkhousePage() {
      let obj = {
        current: this.currentPage,
        size: this.size,
        orgType: localStorage.getItem("orgType"),
      };
      if (this.info.selectHouseType == 1) {
        this.$api.api.zkhousePage({
          propertyName: this.propertyName,
          provinceId :this.provinceId ,
          cityId:this.cityId,
            sortBy:1,
          ...obj
        }).then((res) => {
          // console.log(res)
          this.list = res.data.records;
          this.total = res.data.total;
        });
      }
      if (this.info.selectHouseType == 2) {
        this.$api.api
          .findHouseByArea({
            id: this.area,
            ...obj
          })
          .then((res) => {
            // console.log(res)
            this.list = res.data.records;
            this.total = res.data.total;
          });
      }
    },
    chose(item) {
      console.log(item);
      if(item.lockFlag!=0){
        this.$message.warning('您已经被禁用')
        return
      }
      // if(item.residueTime<=0){
      //   this.$message.warning('您已过期')
      //   return
      // }
      if (this.flag == 1) {
        localStorage.setItem("agentId", item.id);
        localStorage.setItem("orgCode", '');
        localStorage.setItem("topName", item.agentName);
        localStorage.setItem("houseId", '');
      }
      if (this.flag == 2) {
        localStorage.setItem("agentId", '');
        localStorage.setItem("orgCode", item.orgCode);
        localStorage.setItem("topName", item.name);
        localStorage.setItem("houseId", '');
      }
      if (this.flag == 3) {
        localStorage.setItem("orgCode", item.orgCode);
        localStorage.setItem("agentId", '');
        localStorage.setItem("topName", item.propertyName);
        localStorage.setItem("houseId", item.id);
      }
      this.$router.push({ path: "/wel" });
      localStorage.setItem('allClose',true)
    },
    goback() {
      this.$router.back();
      localStorage.setItem('allClose',false)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      // if (this.flag == 1) {
      //   // 获取代理商
      //   this.getAgentList();
      // } else if (this.flag == 2) {
      //   // 获取公司
      //   this.getCompanyList();
      // } else {
      //   // 获取楼盘
      // }
      if (this.flag == 1) {
      // 获取代理商
      // return
      this.getAgentList();
    } else if (this.flag == 2) {
      // 获取公司
      this.getCompanyList();
    } else {
      this.findByUserName();
    }
    },
    locationsChange(e) {
      console.log(e);
      if (!e) return;
      this.provinceId = e[0];
      this.cityId = e[1];
    },
  },
};
</script>

<style lang="scss" scoped >
.box-center {
  min-width: 1200px;
   height: 100vh;
  background: #ffffff;
}
.head {
  height: 64px;
  background: #409eff;
  display: flex;
  padding: 0 20%;
  box-sizing: border-box;
  justify-content: space-between;
  line-height: 64px;
  color: #fff;
  font-size: 24px;
  .callback {
    font-size: 14px;
  }
}
.hid {
  margin-top: 25px;
  height: 50px;
  background: #f8f8f8;
  border-radius: 4px;
  text-align: center;
  line-height: 50px;
  font-size: 18px;
  color: #32363d;
}
.content {
  margin-top: 30px;
  padding: 0 20%;
  // height: 680px;
  min-height: 500px;
  .con {
    display: flex;
    flex-wrap: wrap;
  }
  .search {
    display: flex;
  }
  .tab {
    width: 200px;
    height: 98px;
    border-radius: 4px;
    border: 1px solid #e0e0e0;
    box-sizing: border-box;
    padding-left: 20px;
    margin-left: 40px;
    margin-top: 20px;
    cursor: pointer;
    .text-1 {
      font-size: 16px;
      line-height: 30px;
      margin-top: 10px;
       white-space: nowrap;
      overflow:hidden; /*超出部分省略号显示*/
      text-overflow:ellipsis; /*省略号显示*/
    }
    .text-2 {
      font-size: 14px;
      line-height: 30px;
    }
    .text-3 {
      font-size: 14px;
      line-height: 20px;
      margin-bottom: 10px;
      white-space: nowrap;
      overflow:hidden; /*超出部分省略号显示*/
      text-overflow:ellipsis; /*省略号显示*/
    }
  }
  .tab:hover{
    // color: white;
    // background: #ccc;
    border: 1px solid #409eff;
  }
}
.page {
  // position: absolute;
  // bottom: 0;
  // left: 30%;
  text-align: center;
}
</style>
