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

<script>
import {orgTree} from '@/api/systemInterface'
import {tableData} from '@/api/patrolAndRescue/infoMgmtPatrolsRescues/roadSectionInfo'
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', 'code'], type: 'input', default: true},
        {title: '路线名称', prop: 'routeName', type: 'input'}
      ],
      tableOptions: [
        {label: '路段编号', minWidth: '30', prop: 'roadCode'},
        {label: '路段名称', minWidth: '40', prop: 'roadName'},
        {label: '路线名称', minWidth: '45', prop: 'routeName'},
        {label: '所属公司', minWidth: '40', prop: 'companyName'},
        {label: '起点', minWidth: '50', prop: 'startingPoint'},
        {label: '终点', minWidth: '50', prop: 'terminalPoint'}
      ],
      params: {
        pageBean: {pageSize: 20, page: 1, total: 0},
        querys: [
          {operation: 'LIKE', property: 'COMPANY_IDS_'},
          {group: 'nameCode', operation: 'LIKE', property: 'name', relation: 'OR'},
          {group: 'nameCode', operation: 'LIKE', property: 'code', relation: 'OR'},
          {operation: 'LIKE', property: 'routeName'}
        ],
        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 => ({
        ...i,
        roadId: i.id,
        roadCode: i.code,
        roadName: i.name
      }))
      this.tableLoading = false
    },

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

<style lang="scss" scoped>

</style>
