/*
 *@description:货主
 *@author: hudw
 *@date: 2022-04-27 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-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" v-action:add>新增</p-button>
          <p-button @click="handle_remove" type="danger" :disabled="selectedRowKeys.length === 0" v-action:del>删除</p-button>
          <p-button @click="is_enable_item(0)" :disabled="selectedRowKeys.length === 0" type="primary" v-action:status>启用</p-button>
          <p-button @click="is_enable_item(10)" :disabled="selectedRowKeys.length === 0" type="danger" v-action:status>停用</p-button>
          <p-button @click="mapPase('consignor', selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="primary" v-action:mapParse>地址解析 </p-button>
        </a-space>
        <mainTable
          ref="mainTable"
          :options="options"
          :selectedRowKeys.sync="selectedRowKeys"
          :activeID="mainId"
          :tableExtendedField="tableExtendedField"
          @update="handleUpdate"
          @view="handleView"
          @copy="handleCopy"
          @clickRow="handleRowClick"
        />

        <a-divider orientation="left" v-if="$auth('bms')">基础费用</a-divider>
        <transfer-table
          v-if="$auth('bms')"
          style="margin-top:10px"
          :talbeTitle="['待分配费用', '已分配费用']"
          :columns="bindBMSColumns"
          :transferData="transferBMSData"
          :targetKeys="targetBMSKeys"
          :loading="confirmLoading"
          @change="handleBMSAutoChange"
        />
      </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 ,costList,costFeeSave} 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'
import transferTable from '@/views/TMS/basic/driverManagement/model/transfer-modal.vue'
 const bindBMSColumns = (self)=>[
  {
    dataIndex: 'expenseCode',
    title: '费用编码',
    ellipsis: true,
  },
  {
    dataIndex: 'expenseName',
    title: '费用名称',
    ellipsis: true,
  },
  {
    dataIndex: 'expenseAccountName',
    title: '费用科目',
    ellipsis: true,
  },
];
export default {
  mixins: [addressMixin],
  components: {transferTable, addressAnalysis, searchBar, mainTable, formDetails },
  data() {
    return {
      confirmLoading:false,
      transferBMSData: [],
      targetBMSKeys: [],
      bindBMSColumns:bindBMSColumns(this),//绑定基础费用,
      tableExtendedField: [],
      formExtendedField:[],
      open: false,
      options: {},
      selectedRowKeys: [],
      mainId: '',
      currentMainData: null,
      detailsState: 0 /**0=不显示,1=查看,2=新建,3=修改,4=复制 */,
    }
  },
  mounted() {

    costList(this, { code: '', dataType: 1 }).then((res) => {
        if (res.success) {
          this.transferBMSData = res.data.map((item) => {
            return {
              key: item.expenseCode,
              title: item.expenseName,
              description: item.id,
              ...item,
            };
          });
        }
      });
    let _this = this
    getOrganization(this)//部门
    initDictionary([{ objectcode: 'MAINDATA_STATUS' }, { 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: "3" }).then((data) => {
      if (data) {
        _this.tableExtendedField = data.table
        _this.formExtendedField = data.form
      }
    })
  },
  computed: {},
  methods: {
    handleBMSAutoChange(targetKeys, direction, moveKeys) {
      if (!this.currentMainData) return
      return costFeeSave(this, {
        code: this.currentMainData.cargoOwnerCode,
        list: this.transferBMSData.filter((item) => {
          item.dataType = 1;
          return targetKeys.includes(item.expenseCode);
        }),
      }).then((data) => {
          if (data) {
            this.targetBMSKeys = targetKeys;
          }
        }).finally(() => {
          this.confirmLoading = false;
        });
    },
    is_enable_item(number) {
      let arr = this.selectedRowKeys.map((id) => {
        return { cargoOwnerId: id }
      })
      return mainIsEnable(this, arr, number)
    },
    handleCopy(data) {
      this.detailsState = 4
      this.currentMainData = data
      this.formName = 'main'
    },
    handleRowClick(event, key, data) {
      this.mainId = key
      this.childrenId = ''
      this.targetBMSKeys = [];
      if (key) {
        this.currentMainData = data
        this.confirmLoading = true;
        return costList(this, { dataType: 1, code: data.cargoOwnerCode})
          .then((res) => {
            if (res.success && res.data.length > 0) {
              this.targetBMSKeys = res.data.map((item) => {
                return item.expenseCode;
              });
            }
          }).finally(() => {
            this.confirmLoading = false;
          });



      } 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>
<style lang='less' space>
</style>