/*
 *@description:站点管理
 *@author: hudw
 *@date: 2023-03-27 09:37:32
*/


<template>
  <div class="pages">
      <formDetails ref="formDetails" :formExtendedField="formExtendedField" :st="detailsState" :record="formDetailsData" :mainId="mainId" :options="options"  @back="onBack" />
      <div v-show="detailsState === 0">
        <p-card style="margin-bottom: 10px">
          <template slot="title">
            <searchBar ref="searchBar" @submit="submit" />
          </template>
        </p-card>
        <p-card title="站点信息">
          <a-space slot="extra">
            <!-- v-action:add -->
            <p-button @click="add_item" type="primary" icon="plus">新增</p-button>
            <!-- <import-Button :importUrl="mainImportUrl" :tmpUrl="mainDownUrl" fileName="站点模板" /> -->
            <p-button @click="is_enable_item(1)" :disabled="selectedRowKeys.length === 0" type="primary" v-action:status>启用</p-button>
            <p-button @click="is_enable_item(0)" :disabled="selectedRowKeys.length === 0" type="danger" v-action:status>停用</p-button>
            <!-- <p-button @click="delete_items(selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="danger" v-action:del>删除</p-button> -->
            <!-- <p-button @click="mapPase('supplier', selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="primary" v-action:mapParse>地址解析</p-button> -->
          </a-space>
          <mainTable
            ref="mainTable"
            :options="options"
            :tableExtendedField="tableExtendedField"
            :selectedRowKeys.sync="selectedRowKeys"
            :activeID="mainId"
            @update="handleUpdate"
            @view="handleView"
            @clickRow="handleRowClick"
          />
          </p-card>
          <childrenTable
            style="margin-top: 10px"
            :st="0"
            :record="currentMainData"
            ref="childrenTable"
          />
    </div>
  </div>
</template>
<script>
import searchBar from './model/searchBar/index.searchBar.vue'
import mainTable from './model/mainTable/index.mainTable.vue' /**主表 */
import childrenTable from './model/childrenTable/index.childrenTable.vue' /**明细表 */
import formDetails from './model/formDetails/index.formDetails.vue' /**表单 */
import { mainIsEnable, deleteMainItem, paperIsEnable, mainImportUrl, mainDownUrl } from './api'
import { initDictionary,resultExtendedField } from '@/blocks/utils'
import importButton from '@/blocks/components/importButton/index.importButton.vue'
import {getOrganization } from '@/blocks/components/formConfig/departmenTree.js'

export default {
  components: { importButton, searchBar, mainTable, childrenTable, formDetails,  },
  data() {
    return {
      mainDownUrl,
      mainImportUrl,
      options: {},
      //supplier=站点,shipper=发货方
      loading: false,

      formName: 'main',
      activeKey: '1',
      selectedRowKeys: [],
      children_selectedRowKeys: [],
      confirmLoading: false,
      detailsState: 0,

            /**0=不显示,1=查看,2=新建,3=修改,4=复制 */
      // /**0=不显示, 1=新建，2=修改 ，3=查看 ，4=复制  */
      currentMainData: null,
      tableExtendedField: [],
      formExtendedField: [],
      mainId: '',
      childrenId: '',
      clearAll: true,
      formDetailsData:null,
    }
  },

  mounted() {
    let _this = this
    getOrganization(this)
    initDictionary([
      { objectcode: 'STATION_LEVEL' },
      { objectcode: 'GENERAL_STATUS' },
      { objectcode: 'STATION_TYPE' },
      { objectcode: 'STATION_AREAS' },
     ]).then((res) => {
     for (let key in res) {
        res[key].forEach((item) => {
          if (!isNaN(item.value)) {
            item.value = ~~item.value
          }
        })
      }
      _this.options = { ..._this.options, ...res }
    })

    //  resultExtendedField({ extendedObjectCode: "16" }).then((data) => {
    //    if (data) {
    //     _this.tableExtendedField = data.table
    //     _this.formExtendedField = data.form
    //   }
    // })
  },
  watch: {
    currentMainData(data) {
      if (data) {
        this.mainId = data.id
      }
    },

  },
  computed: {


  },
  methods: {
    handleButtonClick() {},
    handleMenuClick() {},
    submit() {
      this.clearAll = true
      return this.$refs.mainTable.reload().then(() => {
        this.clearAll = false
      })
    },
    add_item(data) {
      this.detailsState =2
      this.currentMainData = null
      this.formName = 'main'
    },


    is_enable_item(number) {
      let arr = this.selectedRowKeys.map((id) => {
        return id
      })
      return mainIsEnable(this, arr, number)
    },
    children_is_enable_item(number) {
      let arr = this.children_selectedRowKeys.map((id) => {
        return id
      })

      return paperIsEnable(this, arr, number, this.mainId)
    },

    delete_items(ids) {

      deleteMainItem(this, ids)
    },
    handleView(data) {
      this.detailsState = 1
      this.formDetailsData = data
      this.formName = 'main'
    },

    handleUpdate(data) {
      this.detailsState = 3
      this.formDetailsData = data
      this.formName = 'main'
    },


    handleRowClick(event, key, data) {
      this.mainId = key
      this.childrenId = ''
      if (key) {
        this.currentMainData = data
      } else {
        this.currentMainData = null
      }
    },

    export_item() {},


    onBack(formName, id, isReload = false) {
      this.detailsState = 0
      this.$refs.childrenTable.clearAll()
      this.currentMainData = null
      this.formDetailsData=null
      isReload &&this.$nextTick(() => {
          this.$refs.mainTable.reload().then(() => {
            if (id) {
              this.mainId = id
              this.currentMainData={id}
            }
          })
        })
    },
  },
}
</script>
