/*
 *@description:供应商管理
 *@author: hudw
 *@date: 2022-03-31 12:12:27
*/
<template>
  <div class="pages">
    <div class="pages" v-show="!addressAnalysisShow">
      <formDetails ref="formDetails" :formExtendedField="formExtendedField" :st="detailsState" :record="formRecord" :mainId="mainId" :options="options" :formName="formName" @back="onBack" @showMap="onShowMap" />
      <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">
            <p-button @click="add_item" type="primary" icon="plus" v-action:add>新增</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"
            @copy="handleCopy"
            @clickRow="handleRowClick"
          />
        </p-card>
        <p-card title="发货方信息" style="margin-top: 10px" :bodyStyle="{height:'300px'}">
          <a-space slot="extra">
            <p-button @click="children_add_item" :disabled="!currentMainData" type="primary" icon="plus" v-action:child_add>新增</p-button>
            <import-Button :importUrl="childrenImportUrl" :tmpUrl="childrenDownUrl" fileName="发货方模板" />
            <p-button @click="children_is_enable_item(1)" :disabled="children_selectedRowKeys.length === 0" type="primary" v-action:child_status>启用 </p-button>
            <p-button @click="children_is_enable_item(0)" :disabled="children_selectedRowKeys.length === 0" type="danger" v-action:child_status>停用 </p-button>
            <p-button @click="delete_items_children(children_selectedRowKeys)" :disabled="children_selectedRowKeys.length === 0" type="danger" v-action:child_del> 删除</p-button>
            <p-button @click="mapPase('shipper', children_selectedRowKeys)" type="primary" :disabled="children_selectedRowKeys.length === 0" v-action:child_mapParse>地址解析</p-button>
          </a-space>
          <childrenTable
            :clearAll="clearAll"
            ref="childrenTable"
            :options="options"
            :currentRow="currentChildrenData"
            :tableExtendedField="children_tableExtendedField"
            :selectedRowKeys.sync="children_selectedRowKeys"
            :mainId="mainId"
            :activeID="childrenId"
            @update="childrenHandleUpdate"
            @view="childrenHandleView"
          />
        </p-card>
      </div>
    </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 childrenTable from './model/childrenTable/index.childrenTable.vue' /**明细表 */
import formDetails from './model/formDetails/index.formDetails.vue' /**表单 */
import { deleteChildrenItem, mainIsEnable, deleteMainItem, paperIsEnable, mainImportUrl, mainDownUrl, childrenDownUrl, childrenImportUrl } from './api'
import { initDictionary,resultExtendedField } from '@/blocks/utils'
import addressAnalysis from '@/blocks/addressAnalysis/index.addressAnalysis.vue' // 地址解析
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, addressAnalysis },
  data() {
    return {
      childrenDownUrl,
      mainDownUrl,
      childrenImportUrl,
      mainImportUrl,
      options: {},
      //supplier=供应商,shipper=发货方
      addressType: 'supplier',
      loading: false,
      importFormItems: [
        { type: 'input', name: 'rcvNo', label: '收货单号' },
        { type: 'input', name: 'mtlOwnerName', label: '货主' },
      ],
      formName: 'main',
      activeKey: '1',
      selectedRowKeys: [],
      children_selectedRowKeys: [],
      confirmLoading: false,
      detailsState: 0,

            /**0=不显示,1=查看,2=新建,3=修改,4=复制 */
      // /**0=不显示, 1=新建，2=修改 ，3=查看 ，4=复制  */
      currentMainData: null,
      currentChildrenData: null,
      tableExtendedField: [],
      children_tableExtendedField:[],
      formExtendedField: [],
      addressAnalysisShow: 0,
      mainId: '',
      childrenId: '',
      clearAll: true,
      addressSearchData: {}, // 列表点击地址解析时的查询传参
      addressData: {}, // 新增修改时带过去的经纬度、地址信息   address: '越秀财富中心',longitude: 114.250019,latitude: 30.568682,
    }
  },

  mounted() {
    let _this = this
    getOrganization(this)
    initDictionary([
      { objectcode: 'SIGN_TIME_UNIT' },
      { objectcode: 'PICK_UP_SIGN_TIME' },
      { objectcode: 'CONTRACT_NATURE' },
      { objectcode: 'BANK' }]).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
      }
    },
    currentChildrenData(data) {
      if (data) {
        this.childrenId = data.id
      }
    },
  },
  computed: {
    formRecord() {
      let obj = {
        main: this.currentMainData,
        children: this.currentChildrenData,
      }

      return obj[this.formName]
    },
  },
  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'
    },
    children_add_item(data) {
      this.detailsState =2
      this.currentChildrenData = null
      this.formName = 'children'
    },

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

      return paperIsEnable(this, arr, number, this.mainId)
    },
    delete_items_children(ids) {
      deleteChildrenItem(this, ids)
    },
    delete_items(ids) {
      // let p = ids.map(id => {
      //   return { id }
      // })
      deleteMainItem(this, ids)
    },
    handleView(data) {

      this.detailsState = 1
      this.currentMainData = data
      this.formName = 'main'
    },
    childrenHandleView(data) {
      this.detailsState = 1
      this.currentChildrenData = data
      this.formName = 'children'
    },
    handleUpdate(data) {

      this.detailsState = 3
      this.currentMainData = data
      this.formName = 'main'
    },

    childrenHandleUpdate(data) {
      this.detailsState = 3
      this.currentChildrenData = data
      this.formName = 'children'
    },

    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
      }
    },
    mapPase(type, selectedRowKeys) {
      this.addressData = {}

      if (selectedRowKeys.length > 0) {
        // supplier, shipper

        if (type == 'supplier') {
          let providerIds = selectedRowKeys.map((str) => {
            return {
              providerId: str,
            }
          })

          this.addressSearchData = { providerIds, search: '' }
        } else if (type == 'shipper') {
          let shipperIds = selectedRowKeys.map((str) => {
            return {
              id: str,
            }
          })
          this.addressSearchData = { shipperIds, search: '' }
        }
        this.addressType = type
      }
      this.addressAnalysisShow = 2
    },
    export_item() {},
    addressBack() {
      let formName = ''
      this.addressAnalysisShow = 0
      if (this.addressType == 'supplier') {
        formName = 'main'
      } else if (this.addressType == 'shipper') {
        formName = 'children'
      } else {
        return
      }
      this.$nextTick(() => {
        this.$refs[formName + 'Table'].reload().then(() => {})
      })
      //supplier=供应商,shipper=发货方
    },
    onShowMap(form) {
      this.addressSearchData = {}
      this.addressData = {}
      this.addressAnalysisShow = 1
      let obj = form.getFieldsValue(['address', 'longitude', 'latitude'])

      if (obj.address) {
        this.addressData = obj
      } else {

        this.addressData = {
        }
      }
    },
    onMapReturnData(data) {
      let form = this.$refs.formDetails.form()
      form.setFieldsValue(data)
      this.addressAnalysisShow = 0
    },

    onBack(formName, id, isReload = false) {
      this.detailsState = 0
      isReload &&
        this.$nextTick(() => {
          this.$refs[formName + 'Table'].reload().then(() => {
            if (id) {
              if (formName == 'main') {
                this.mainId = id
              } else if (formName == 'children') {
                this.childrenId = id
              }
            }
          })
        })
    },
  },
}
</script>
