/*
 *@description:线路匹配策略
 *@author: hudw
 *@date: 2022-10-20 15:06:58
*/
<template>
  <div class="pages">
    <div class="pages" v-show="!addressAnalysisShow">
      <formDetails
        v-show="detailsState === 2"
        :formExtendedField="formExtendedField"
        :st="detailsState"
        ref="formDetails"
        @showMap="onShowMap"
        :record="currentMainData"
        :mainId="mainId"
        :options="options"
        @back="onBack"
      >
      </formDetails>
      <p-card v-show="detailsState === 0" class="full-card-panel full-body-flex" :alt="$route.meta.title" :bordered="false">
        <searchBar slot="title" ref="searchBar" @submit="submit" :options="options" />
        <a-space slot="extra">
          <p-button type="primary" @click="handle_add" icon="plus">新增</p-button>
          <!-- <p-button @click="handle_remove" type="danger" :disabled="selectedRowKeys.length === 0">删除</p-button> -->
          <p-button @click="is_enable_item(0,selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="primary">启用</p-button>
          <p-button @click="is_enable_item(10,selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="danger">停用</p-button>
          <!-- <p-button @click="mapPase('consignor', selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="primary">地址解析 </p-button> -->
        </a-space>
        <mainTable
          ref="mainTable"
          :options="options"
          :selectedRowKeys.sync="selectedRowKeys"
          :activeID="mainId"
          :tableExtendedField="tableExtendedField"
          @update="handleUpdate"
          @view="handleView"
          @copy="handleCopy"
          @clickRow="handleRowClick"
        />
      </p-card>
    </div>
    <addressAnalysis :showType="addressAnalysisShow" :type="addressType" :searchParams="addressSearchData" :addressData="addressData" @back="addressBack" @returnData="onMapReturnData" />
  </div>
</template>
<script>
import searchBar from './model/searchBar/index.searchBar.vue'
import mainTable from './model/mainTable/index.mainTable.vue' /**主表 */
import formDetails from './model/formDetails/index.formDetails.vue' /**表单 */
import { deleteMainItem, mainIsEnable } from './api'
import { resultExtendedField, initDictionary } from '@/blocks/utils'
import addressAnalysis from '@/blocks/addressAnalysis/index.addressAnalysis.vue' // 地址解析
import addressMixin from './mixins/addressMixin'
import { getOrganization } from '@/blocks/components/formConfig/departmenTree.js'



//5仓库 6客户 7供应商"

export default {
  mixins: [addressMixin],
  components: { addressAnalysis, searchBar, mainTable, formDetails },
  data() {
    return {
      tableExtendedField: [],
      formExtendedField: [],
      open: false,
      options: {},
      selectedRowKeys: [],
      mainId: '',
      currentMainData: null,
      detailsState: 0 /**0=不显示,1=查看,2=新建,3=修改,4=复制 */,
    }
  },
  mounted() {

    let _this = this
    getOrganization(this) //部门
    initDictionary([
      { objectcode: 'STRATEGY_CONDITION_TYPE' },
      { objectcode: 'GENERAL_STATUS' },
      { objectcode: 'CONSIGNEE_TYPE' },
      { objectcode: 'ORDER_TYPE' }, { objectcode: 'ORDER_BUSINESS_TYPE' }, { objectcode: 'DELIVERY_METHOD_TYPE' }, { objectcode: 'TRANSPORT_TYPE' }, { objectcode: 'TEMPERATURE_REQUIREMENT' }]).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: '3' }).then((data) => {
      if (data) {
        _this.tableExtendedField = data.table
        _this.formExtendedField = data.form
      }
    })
  },
  computed: {},
  methods: {
    is_enable_item(status,ids) {
      return mainIsEnable(this,status, ids)
    },
    handleCopy(data) {
      this.detailsState = 4
      this.currentMainData = data
      this.formName = 'main'
    },
    handleRowClick(event, key, data) {
      this.mainId = key
      this.childrenId = ''
      if (key) {
        this.currentMainData = data
      } else {
        this.currentMainData = null
      }
    },
    handleUpdate(data) {
      this.detailsState = 3
      this.currentMainData = data
      this.formName = 'main'
    },
    submit() {
      return this.$refs.mainTable.reload()
    },
    handle_remove() {
      deleteMainItem(this, this.selectedRowKeys)
    },
    handle_add(data) {
      this.open = true
      this.detailsState = 2
      this.currentMainData = null
    },
    handleView(data) {
      this.detailsState = 1
      this.currentMainData = data
      this.formName = 'main'
    },
    onBack(id, isReload = false) {
      this.detailsState = 0
      isReload &&
        this.$nextTick(() => {
          this.$refs.mainTable.reload().then(() => {
            if (id) {
              this.mainId = id
            }
          })
        })
    },
  },
}
</script>
