/*
 *@description:收入台账
 *@author: hudw
 *@date: 2022-09-01 11:36:21
*/
<template>
  <div class="pages">
    <div class="pages">
      <pageOrderView
        v-show="detailsState === 1"
        :st="1"
        :url="{ 1:mainViewUrl,3:feedViewUrl}[mode]"
        :goodsExtendedField="childrenTableExtendedField"
        :record="currentMainDataView"
        :mode="mode"
        :mainId="mainId"
        :options="options"
        @back="onBack"
      />

      <inMoneyModal :visible.sync="inMoneyVisible" :mode="mode" :dataSource="selectMainArr" :options="options" @ok="ok" title="费用录入" />
      <div class="pages" v-show="detailsState === 0">
        <searchBar ref="searchBar" :options="options" :count="mode" @submit="searchSubmit"></searchBar>
        <p-card :bordered="false" :bodyStyle="{ minHeight: '300px' }">
          <a-space slot="title">
            订单/派车单信息信息
            <p-button :type="mode === 1 ? 'primary' : 'default'" @click="checkMode(1)">运单台账</p-button>
            <p-button :type="mode === 3 ? 'primary' : 'default'" @click="checkMode(3)">派车单台账</p-button>
          </a-space>
          <a-space slot="extra">
            <p-button @click="add_item" type="primary" :disabled="selectedRowKeys.length === 0">费用录入</p-button>
            <!-- <p-button v-if="mode === 3" @click="batch_sub(selectedRowKeys, selectedRowsData)" :disabled="selectedRowKeys.length === 0" type="primary">费用提交</p-button> -->
          </a-space>

          <mainTable
            v-if="mode === 1"
            ref="mainTable"
            :options="options"
            :tableExtendedField="tableExtendedField"
            :scroll="{ x: true }"
            :vScrollX="{enabled:false}"
            :vScrollY="{enabled:true,rows:10}"
            autoColWidth
            :activeID="mainId"
            @update="handleUpdate"
            @view="handleView"
            @clickRow="handleRowClick"
            :doneBefore="doneBefore"
            @selectChange="selectChange"
            :operation="{ width: 140 }"
          />
          <nextTable
            v-else-if="mode === 3"
            ref="mainTable"
            :options="options"
            :tableExtendedField="tableExtendedField"
            :activeID="mainId"
            @clickRow="handleRowClick"
            @view="handleView"
            :doneBefore="doneBefore"
            @selectChange="selectChange"
            @submit="tr_batch_sub"
            :operation="{ width: 180 }"
            @update="handleUpdate"
          />
        </p-card>
        <p-card :bordered="false" style="margin-top: 10px" :bodyStyle="{ minHeight: '300px' }">
          <a-tabs style="background-color: #fff" v-model="tabsKey">
            <a-button v-if="tabsKey === 4" slot="tabBarExtraContent" type="primary" @click.stop="submitFee(feeSelectedRowKeys)" :disabled="feeSelectedRowKeys.length === 0">提交 </a-button>
            <a-tab-pane :key="0" v-if="mode===3"  tab="运单信息">
              <shipbillTable :isShow="tabsKey === 0"  ref="childrenTable_0" :options="options" :tableExtendedField="childrenTableExtendedField" :mainRowKey="billId" :activeID="childrenId" />
            </a-tab-pane>
            <a-tab-pane :key="1" tab="商品信息">
              <goodsTable ref="childrenTable_1" :options="options" :isShow="tabsKey === 1" :tableExtendedField="childrenTableExtendedField" :mainRowKey="billId" :activeID="childrenId" />
            </a-tab-pane>
            <a-tab-pane :key="2" tab="包裹信息">
              <parcelTable ref="childrenTable_2" :options="options" :isShow="tabsKey === 2" :mainRowKey="billId" />
            </a-tab-pane>
            <a-tab-pane :key="3" tab="审批信息">
              <approveTable ref="childrenTable_3" :url="auditTableInfoUrl" :options="options" :isShow="tabsKey === 3" :mainRowKey="billId" />
            </a-tab-pane>
            <a-tab-pane :key="4" tab="费用信息">
              <adjustTable ref="childrenTable_4" :selectedRowKeys.sync="feeSelectedRowKeys" :options="options" :isShow="tabsKey === 4" :mainRowKey="billId" @submit="(data) => submitFee([data.id])">
              </adjustTable>
            </a-tab-pane>
            <a-tab-pane :key="5" tab="操作日志">
               <logTable ref="childrenTable_5" :options="options" :isShow="tabsKey === 5" :mainRowKey="billId" /></a-tab-pane>
          </a-tabs>
        </p-card>
      </div>
    </div>
  </div>
</template>
<script>
import mainTable from './model/mainTable/index.mainTable.vue'; /**主表 */
import nextTable from './model/mainTable/next.mainTable.vue'; /**主表 */
import goodsTable from './model/childrenTable/goodsTable.vue'; /**明细表 */
import parcelTable from './model/childrenTable/parcelTable.vue'; /**明细表 */
import adjustTable from './model/childrenTable/adjustTable.vue'; /**明细表 */
import approveTable from './model/childrenTable/approveTable.vue'; /**明细表 */
import logTable from './model/childrenTable/logTable.vue'; /**明细表 */
import shipbillTable from './model/childrenTable/shipbillTable.vue'; /**明细表 */

import inMoneyModal from './model/inMoneyModal/index.inMoneyModal.vue'; /**费用录入 */

import {mainViewUrl, feedViewUrl,auditTableInfoUrl, shipBillFeeSave, mainTableInfoUrl, dataSubmit, dataFeeSubmit, mainImportUrl, mainDownUrl } from './api.js';
import { resultExtendedField } from '@/blocks/utils';
let options = null;
import { UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js';
import useMixin from '../../$common/mixins/useMixin';
import dictionary from '../../$common/mixins/dictionary'; //@字典

export default {
  mixins: [useMixin, dictionary],

  components: {shipbillTable, inMoneyModal, mainTable, nextTable, goodsTable, parcelTable, adjustTable, approveTable, logTable },
  data() {
    return {
      mode: 1,
      auditTableInfoUrl,
      mainViewUrl,
      feedViewUrl,
      UPCLOUD_RDC_TMS_BUSINESS_SERVICE,
      mainPages: { pageNum: 1, pageSize: 15 },
      mainTableInfoUrl,
      mainImportUrl,
      mainDownUrl,
      options: options || {},
      loading: false,
      formName: 'main',
      selectedRowKeys: [],
      children_selectedRowKeys: [],
      confirmLoading: false,
      detailsState: 0,
      tabsKey: 1,
      inMoneyVisible: false,
      /**0=不显示,1=查看,2=新建,3=修改,4=复制 */
      currentMainData: null,
      currentMainDataView: null,
      selectMainArr: [],
      currentChildrenData: null,
      tableExtendedField: [],
      childrenTableExtendedField: [],
      formExtendedField: [],
      childrenFormExtendedField: [],
      mainId: '',
      childrenId: '',
      selectedRowsData: [],
      feeSelectedRowKeys: [],
    };
  },
  mounted() {
    let _this = this;
    // resultExtendedField({ extendedObjectCode: '15' }).then((data) => {
    //   if (data) {
    //     _this.tableExtendedField = data.table;
    //     _this.formExtendedField = data.form;
    //   }
    // });
    // resultExtendedField({ extendedObjectCode: '17' }).then((data) => {
    //   if (data) {
    //     _this.childrenTableExtendedField = data.table;
    //   }
    // });

    this.options.sysParams = {
      //@费用精度
      costPrecision: 4,
      //@费用取整
      costInteger: 1, //四舍五入
    };
  },
  watch: {
    mode(v) {
      this.currentMainData=null
      if (v === 3) {
        this.tabsKey=0
      } else {
        this.tabsKey=1

      }
    },
    currentChildrenData(data) {
      if (data) {
        this.childrenId = data.id;
      }
    },
    tabsKey(value) {
      this.childrenTableReload();
    },
  },
  computed: {
    billId() {
      return this.currentMainData?.id ?? null;
    },
  },
  methods: {
    childrenTableReload() {
      this.$nextTick(() => {

        let table = this.$refs['childrenTable_' + this.tabsKey];
        if (table) {
          table.dataSource = [];
          this.mainId && table.reload();
        }
      });
    },
    childrenTableClear() {
      this.$nextTick(() => {
        let table = this.$refs['childrenTable_' + this.tabsKey];
        if (table) {
          table.dataSource = [];
        }
      });
    },
    doneBefore(res) {
      if (res.data) {
        const { pageNum, pageSize } = res.data;
        this.mainPages = { pageNum, pageSize };
      }
    },
    checkMode(count) {
      let form = this.$refs.searchBar.$refs.gird.form;
      form.setFieldsValue({ expenseStatus: '' });
      if (count === 1) {
        form.hiddenItem('expenseStatus');
      } else {
        form.unhiddenItem('expenseStatus');
      }

      this.mode = count;
      this.mainId = '';
      this.childrenTableClear();
    },

    submit() {
      this.mainId = '';
      this.childrenTableClear();
      return this.$refs.mainTable.reload();
    },
    tr_batch_sub(selectedRowKeys) {
      dataSubmit(this, selectedRowKeys);
      this.childrenTableClear();
    },
    batch_sub(selectedRowKeys, selectedRowsData) {
      for (let index = 0; index < selectedRowsData.length; index++) {
        const element = selectedRowsData[index];
        if (element.expenseStatus != 0) {
          this.$message.error('第' + (index + 1) + '条费用无需提交');
          return false;
        }
      }
      dataSubmit(this, selectedRowKeys);
      this.childrenTableClear();
    },

    submitFee(arr) {
      dataFeeSubmit(this, arr);
    },
    add_item(arr) {
      this.inMoneyVisible = true;

      if (this.mode === 2) {
        this.selectMainArr = this.selectedRowsData.map((item) => item.shipbillNo);
      } else {
        this.selectMainArr = this.selectedRowsData;
      }
      this.formName = 'main';
    },

    import_item() {},

    handleView(data) {
      this.detailsState = 1;
      this.$nextTick(() => {
        this.currentMainDataView = data;
        this.formName = 'main';
      });
    },

    handleUpdate(arr) {
      //打开费用

      this.inMoneyVisible = true;
      this.formName = 'main';
      this.mainId = arr[0].id;
      this.selectMainArr = arr;
    },
    ok(data, close) {
      shipBillFeeSave(this, data).finally(() => close());
    },

    // 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=发货方
    // },

    handleRowClick(event, key, data) {
      this.mainId = key;
      this.childrenId = '';
      this.currentMainData = data;


    },
    selectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys=selectedRowKeys
      this.selectedRowsData = selectedRows;
    },

    onBack(formName, id, isReload) {
      this.detailsState = 0;
      this.currentMainData = null;
      this.currentMainDataView = null;
      if (isReload) {
        this.childrenTableClear();
        this.$refs[formName + 'Table'].reload().then(() => {
          if (id) {
            if (formName == 'min') {
              this.mainId = id;
            } else {
              this.childrenId = id;
            }
          }
        });
      }
    },
  },
};
</script>
