<template>
  <div class="peopleScreen">
    <el-form :model="peopleScreenForm" :inline="true" size="small" ref="peopleScreenFormRef">
      <!-- 代理商显示 userType==1标识代理商 -->

      <template v-if="userType == 1">
        <el-form-item label="代理商" prop="agentId">
          <el-select v-model="peopleScreenForm.agentId" filterable remote reserve-keyword placeholder="请输入关键词" clearable
            @change="fetchPropertys">
            <el-option :label="item.name" :value="item.id" v-for="item in agents" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物业" prop="propertyId">
          <el-select v-model="peopleScreenForm.propertyId" filterable remote reserve-keyword placeholder="请输入关键词"
            clearable>
            <el-option :label="item.name" :value="item.id" v-for="item in propertys" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </template>

      <el-form-item label="姓名" prop="name">
        <el-input v-model="peopleScreenForm.name" placeholder clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="peopleScreenForm.mobile" placeholder clearable></el-input>
      </el-form-item>
      <el-form-item label="系统权限类型" prop="roleType">
        <div class="screen-item">
          <el-select v-model="peopleScreenForm.roleType" clearable>
            <template v-for="(item, index) in selectItems['roleType']" :key="index">
              <el-option :label="item.value" :value="item.key"></el-option>
            </template>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="账号角色类型" prop="approle">
        <div class="screen-item">
          <el-select v-model="peopleScreenForm.approle" clearable>
            <template v-for="(item, index) in selectItems['approle']" :key="index">
              <el-option :label="item.value" :value="item.key"></el-option>
            </template>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type @click="clearScreen(peopleScreenFormRef)">清空</el-button>
        <el-button type="primary" @click="restSearch">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="button-group" ref="buttonGroupRef"></div>
  <div class="main-table" v-if="showPeople">
    <div class="left">
      <people-list :peopleScreenForm="peopleScreenForm" @selectUser="selectUser" ref="peopleListRef"
        :tableHeader="tableHeader"></people-list>
    </div>
    <div class="right">
      <selected-people :peopleScreenForm="peopleScreenForm" :groupId="groupId" ref="selectedPeopleRef">
      </selected-people>
    </div>
  </div>
</template>
<script>
export default {
  name: "set-people",
};
</script>
<script setup>
import { ref, reactive, nextTick, onMounted } from "vue";
import templateHooks from "@/Hooks/templateHooks.js";
import { authorService } from "@/ApiService/authorService";
import { pageService } from "@/ApiService/pageService.js";
import { dropdownService } from "@/ApiService/dropdownService.js";
import selectedPeople from "@/Components/authManage/selectedPeopleCom.vue";
import peopleList from "@/Components/authManage/peopleListCom.vue";
const { $message, $common } = templateHooks();
const props = defineProps({
  groupId: {
    type: Number || String,
  },
  showPeople: {
    type: Boolean,
    default: false,
  },
  buttonId: {
    type: Number || String,
  },
});
const userType = $common.getCompany().enterpriseType;
const peopleScreenForm = reactive({
  agentId: "",
  roleType: "",
  mobile: "",
  name: "",
  authorityId: "",
});

const buttonGroupRef = ref();
const peopleLoading = ref(false);
const peopleScreenFormRef = ref();
const agents = ref([]);
const propertys = ref([]);
const peopleListRef = ref();
const selectedPeopleRef = ref();
const tableHeader = ref([]);
const selectItems = ref([]);

const selectUser = (row) => {
  if (row.propertyAuthorityId > 0) {
    $message.error(row.name + "已加入其它权限组");
    return;
  }
  if (!props.groupId) {
    $message.error("缺少权限组Id");
    return;
  }
  let postData = {
    authorityId: props.groupId,
    userIds: row.id,
  };
  authorService.setAuthorityUser(postData).then((res) => {
    if (res.code == 0) {
      //   触发重新获取已选中人员列表
      peopleListRef.value.fetchUsers();
      selectedPeopleRef.value.pageProps.page = 1;
      selectedPeopleRef.value.fetchSelectUsers();
      $message.success("添加成功");
    } else {
      $message.error(res.err);
    }
  });
};

const clearScreen = (domRef) => {
  console.log(domRef);
  domRef.resetFields();
  nextTick(() => {
    restSearch();
  });
};
const restSearch = () => {
  peopleListRef.value.pageProps.page = 1;
  peopleListRef.value.fetchUsers();
  selectedPeopleRef.value.pageProps.page = 1;
  selectedPeopleRef.value.fetchSelectUsers();
};
const fetchPeopleListHeader = () => {
  const { buttonId } = props;
  console.log(props);
  if (!buttonId) {
    $message.error("缺少按钮ID,获取子页面表格表头字段失败");
    return;
  }
  pageService.queryButtonFieldByButton({ buttonId }).then((res) => {
    if (res.code == 0) {
      tableHeader.value = res.data;
      selectItems.value = $common.selectItems(tableHeader.value);
    } else {
      $message.error(res.err);
    }
  });
};
const fetchAgents = () => {
  dropdownService.selectAllAgent().then((res) => {
    if (res.code == 0) {
      agents.value = res.data;
    } else {
      this.$message.error(res.err);
    }
  });
};
const fetchPropertys = () => {
  if (!peopleScreenForm.agentId) {
    propertys.value = []
    peopleScreenForm.propertyId = ''
    return
  }
  dropdownService
    .selectPropertyByAgentId({ agentId: peopleScreenForm.agentId })
    .then((res) => {
      if (res.code == 0) {
        propertys.value = res.data;
      } else {
        this.$message.error(res.err);
      }
    });
};
onMounted(() => {
  fetchPeopleListHeader();
  if (userType == 1) {
    fetchAgents();
  }
});
</script>
<style lang="scss" scoped>
.main-table {
  width: 100%;
  display: flex;
  justify-content: space-between;

  .left,
  .right {
    padding: 10px 15px;
    box-sizing: border-box;
  }

  .left {
    width: 65%;
  }

  .right {
    flex: 1;
  }

  .left::before {
    content: "所有用户";
  }

  .right::before {
    content: "已选择的用户";
  }
}
</style>
