<template>
  <!-- 所在外包公司选择弹层 -->
  <SuperTableSelectorDialog
    v-on="$listeners"
    v-bind="$attrs"
    title="外包公司选择"
    selectKey="outCompanyId"
    selectLabel="outCompanyName"
    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/outsourcingCompanyMgmt'
import {refineRequestBody, setQueryValue} from '@/utils/requestWrappers'
import SuperTableSelectorDialog from '@/components/SuperTableSelectorDialog/index.vue'

export default {
  name: 'OutCompanyDialog',
  components: {SuperTableSelectorDialog},
  props: {
    isShow: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      orgData: [], // 组织树
      optionList: [
        {title: '公司全称或简称', prop: ['name', 'shortName'], type: 'input', default: true},
        {title: '公司营业执照编码', prop: 'code', type: 'input'},
        {title: '公司地址', prop: 'address', type: 'input'}
      ],
      tableOptions: [
        {label: '公司全称', minWidth: '40', prop: 'name'},
        {label: '公司简称', minWidth: '40', prop: 'shortName'},
        {label: '公司法人', minWidth: '40', prop: 'man'},
        {label: '所属组织', minWidth: '40', prop: 'orgName'},
        {label: '公司营业执照编码', minWidth: '40', prop: 'code'},
        {label: '公司联系电话', minWidth: '40', prop: 'tel'},
        {label: '公司地址', minWidth: '40', prop: 'address'}
      ],
      params: {
        pageBean: {pageSize: 20, page: 1, total: 0},
        querys: [
          {operation: 'LIKE', property: 'ORG_FULL_ID_'},
          {group: 'nameShort', operation: 'LIKE', property: 'name', relation: 'OR'},
          {group: 'nameShort', operation: 'LIKE', property: 'shortName', relation: 'OR'},
          {operation: 'LIKE', property: 'code'},
          {operation: 'LIKE', property: 'address'}
        ],
        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, 'ORG_FULL_ID_', this.treeItem?.path || '')
    },

    // 获取公司树
    async getOrgTreeData() {
      this.orgData = await orgTree()
      this.treeItem = this.orgData[0] || {}
      setQueryValue(this.params, 'ORG_FULL_ID_', 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,
        outCompanyId: i.id,
        outCompanyName: i.name
      }))
      this.tableLoading = false
    },

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

<style lang="scss" scoped>

</style>
