<template>
  <view>
    <view class="searcher">
      <view>
        <u-search
          v-model="searchForm.keyword"
          placeholder="请输入关键字"
          shape="round"
          bg-color="#f7f7f8"
          action-text="搜索"
          @search="fnResetPaging"
          @custom="fnResetPaging"
        />
      </view>
      <view class="mainer">
        <view class="area" @click="fnDepartmentShow">
          <view class="name u-line-1">
            {{ searchForm.departmentName || "请选择部门" }}
          </view>
          <view class="more">
            <u-icon name="arrow-down"></u-icon>
          </view>
        </view>
        <view class="radio_main">
          <u-radio-group
            v-model="searchForm.divideStates"
            style="width: 100%; display: flex; justify-content: space-between"
            @change="fnResetPaging"
          >
            <u-radio
              v-for="(item, index) in radioList"
              :key="index"
              :name="item.value"
              :label="item.name"
            />
          </u-radio-group>
        </view>
      </view>
    </view>

    <view class="info_mainer">
      <view v-if="list.length > 0">
        <u-list @scrolltolower="scrolltolower">
          <u-list-item
            v-for="item in list"
            :key="item.CORPINFO_ID"
            class="wrap"
          >
            <view class="tit u-line-2">{{ item.CORP_NAME }}</view>
            <view class="text">
              <text>安全监管类型:</text>
              {{ item.INDUSTRY_SPSIC }}
            </view>
            <view class="text">
              <text>管理单位:</text>
              {{ item.ALL_REGULATORY_DEPARTMENT_NAME }}
            </view>
          </u-list-item>
        </u-list>
      </view>
      <empty v-else />
    </view>
    <tki-tree
      ref="departmentTreeRef"
      :range="department"
      id-key="id"
      range-key="name"
      children-name="nodes"
      select-parent
      @confirm="fnDepartmentTreeConfirm"
    />
  </view>
</template>

<script>
import { getRegulatoryDivisionList } from "@/api";
import TkiTree from "@/components/tki-tree/tki-tree.vue";

export default {
  components: {
    TkiTree,
  },
  props: {
    department: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      list: [],
      pageSize: 10,
      currentPage: 1,
      totalPage: 0,
      radioList: [
        { name: "全部", value: "" },
        { name: "已划分", value: "1" },
        { name: "未划分", value: "2" },
      ],
      searchForm: {
        keyword: "",
        divideStates: "",
        departmentName: "",
        departmentId: "",
      },
    };
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo;
    },
  },
  created() {
    this.fnGetData();
  },
  methods: {
    async fnGetData() {
      const resData = await getRegulatoryDivisionList({
        showCount: this.pageSize,
        currentPage: this.currentPage,
        KEYWORDS: this.searchForm.keyword,
        DIVIDE_STATES: this.searchForm.divideStates,
        REGULATORY_DEPARTMENT_ID: this.searchForm.departmentId,
      });
      this.list = [...this.list, ...resData.varList];
      this.totalPage = resData.page.totalPage;
    },
    scrolltolower() {
      this.currentPage++;
      if (this.totalPage >= this.currentPage) this.fnGetData();
    },
    fnResetPaging() {
      this.list = [];
      this.currentPage = 1;
      this.fnGetData();
    },
    fnDepartmentShow() {
      this.$refs.departmentTreeRef._show();
    },
    fnDepartmentTreeConfirm(event) {
      this.searchForm.departmentName = event[0].name;
      this.searchForm.departmentId = event[0].id;
      this.fnResetPaging();
    },
  },
};
</script>

<style scoped lang="scss">
.searcher {
  width: 100%;
  background: #ffffff;
  padding: 20upx;
  box-sizing: border-box;
  margin-top: 20upx;
  border-bottom: 1px solid #eeeeee;

  .mainer {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 20upx;
    align-items: center;
    font-size: 28upx;

    .area {
      width: 40%;
      background: #f7f7f8;
      border-radius: 4upx;
      height: 72upx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10upx;

      .name {
        width: 90%;
      }
    }

    .radio_main {
      width: 60%;
      padding-left: 10upx;
    }
  }
}

.info_mainer {
  width: 100%;
  margin-top: 20upx;

  .wrap {
    width: 100%;
    background: #ffffff;
    margin-bottom: 20upx;
    padding: 20upx;
    line-height: 2;
    box-sizing: border-box;

    .info_main {
      width: 100%;
      display: flex;
      flex-wrap: wrap;

      .list {
        width: 50%;
        display: flex;

        .lable {
          color: #999999;
          margin-right: 10upx;
        }
      }
    }

    .tit {
      font-weight: bold;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .text text {
      color: #999999;
      margin-right: 10upx;
    }
  }
}
</style>
