<template>
  <!-- 所在路段选择弹层 -->
  <SuperTableSelectorDialog
    width="75%"
    v-on="$listeners"
    v-bind="$attrs"
    title="救援点选择"
    selectKey="roadId"
    selectLabel="roadName"
    select-title-name="常驻救援点"
    :treeTabList="['行政维度']"
    :isShow.sync="isShowDialog"
    hintLanguage="提示：请在列表中选择救援点后，点击确认。"
    :search-option="optionList"
    :treeData="orgData"
    :tree-item="treeItem"
    :defaultTreeExpandLevel="4"
    :table-data="tableData"
    :table-options="tableOptions"
    :table-loading="tableLoading"
    :tableParams="params"
    isSingleChoice
    :isShowSelect="false"
    @open="open"
    @closed="closed"
    @tableRefresh="getTableData"
    @treeChange="treeChange"
    ref="superTableSelectorDialog"
  />
</template>

<script>
import {orgTree} from '@/api/systemInterface'
import {tableData} from '@/api/patrolAndRescue/infoMgmtPatrolsRescues/rescueSiteMgmt'
import {refineRequestBody, setQueryValue} from '@/utils/requestWrappers'
import SuperTableSelectorDialog from '@/components/SuperTableSelectorDialog/index.vue'

export default {
  name: 'RoadChoiceDialog',
  components: {SuperTableSelectorDialog},
  props: {
    isShow: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      orgData: [], // 组织树
      optionList: [
        {title: '救援点名称', prop: 'name', type: 'input', default: true},
        {title: '所属路段', prop: 'roadName', type: 'input', default: true}
      ],
      tableOptions: [
        {label: '救援点名称', minWidth: '50', prop: 'name'},
        {label: '所属路段', minWidth: '40', prop: 'roadName'},
        {label: '所属管理中心', minWidth: '45', prop: 'manageUnitName'},
        {label: '所属救援队', minWidth: '40', prop: 'teamName'},
        {label: '负责人', minWidth: '50', prop: 'linkName'}
      ],
      params: {
        pageBean: {pageSize: 20, page: 1, total: 0},
        querys: [
          {operation: 'LIKE', property: 'name'},
          {operation: 'LIKE', property: 'roadName'}
        ],
        sorter: []
      },
      tableData: [], // 所在路段数据
      treeItem: {}, // 树选中
      tableLoading: false // 表格loading
    }
  },
  computed: {
    isShowDialog: {
      get() {
        return this.isShow
      },
      set(val) {
        this.$emit('update:isShow', val)
      }
    }
  },
  created() {
    this.getOrgTreeData()
  },
  methods: {
    // 打开弹层时触发
    open() {
      this.getTableData()
    },

    // 关闭弹层触发
    closed() {
      const {orgData} = this
      // 重置data中的数据
      Object.assign(this.$data, this.$options.data.call(this), {orgData})
      this.treeItem = this.orgData[0] || {}
      setQueryValue(this.params, 'COMPANY_IDS_', this.treeItem?.path || '')
    },

    // 获取公司树
    async getOrgTreeData() {
      this.orgData = await orgTree()
      this.treeItem = this.orgData[0] || {}
      setQueryValue(this.params, 'COMPANY_IDS_', this.treeItem?.path || '')
    },

    //获取所在路段列表数据
    async getTableData() {
      this.tableData = []
      this.tableLoading = true
      const {total = 0, rows = []} = await tableData(refineRequestBody(this.params))
      this.params.pageBean.total = total
      this.tableData = rows.map(i => {
        const { name, roadName, manageUnitName, linkName, id:rescueId  } = i
        return { name, roadName, manageUnitName, linkName, rescueId }
      })
      this.tableLoading = false
    },

    // 树改变
    treeChange(treeItem) {
      // 修改对应的字段值
      setQueryValue(this.params, 'COMPANY_IDS_', treeItem?.path || '')
      this.getTableData()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
