<!-- @desc:销售订单  @auth:付斌  @time:2024/02/20 15:15 -->
<template>
  <div class="main-div">
    <BaseIndexButtonGroup id="BaseIndexButtonGroup">
      <template #left>
        <BaseIndexButton ref="search" name="search"></BaseIndexButton>
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
        <BaseIndexButton ref="edit" name="edit"></BaseIndexButton>
        <BaseIndexButton ref="orderOut" name="orderOut"></BaseIndexButton>
        <BaseIndexButton ref="orderOutReturn" name="orderOutReturn"></BaseIndexButton>
        <!--作废-->
        <BaseIndexButton ref="invalid" name="invalid"></BaseIndexButton>
        <!--销售跟踪-->
        <BaseIndexButton ref="orderTrack" name="orderTrack"/>
        <!--    打印    -->
        <BaseIndexButton ref="print" name="print"></BaseIndexButton>
      </template>
      <template #right>
        <BaseIndexButton ref="add" name="add"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>
    <!--  卡片区  -->
    <div class="index-card">
      <!--  查询条件区域  -->
      <div id="search-cond-div" ref="search-cond-div" class="search-cond-class">
        <SearchCond ref="searchCond" v-model="searchCond"
                    @collapse-change="collapseChange" :setFlag="false"
                    :searchContent="searchContent"></SearchCond>
      </div>

      <!--  表格部分  -->
      <DkSplit mode="vertical" v-model="split" :height="tableHeight">
        <div slot="top">
          <DkTable slot="top" :id="'table-'+$options.name"
                   ref="table-select" name="table" :data="tableData" primaryKey="orderId"
                   @current-change="currentChange($event)"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :page-size="pageInfo.pageSize"
                   :page-size-opts="pageSizeOpts"
                   :current-page="pageInfo.currentPage"
                   :choose-flag="false"
                   :height="this.tableHeight * split - 36" showFooter>
            <DkTableColumn field="orderNo" :type="$libaray.checkButtonHasRight('order-edit')?'link':''"
                           @on-link="handleLink"/>
            <DkTableColumn field="cusName" :type="$libaray.checkButtonHasRight('customer-detail')?'link':''"
                           @on-link="openCustomerMessage"/>
            <DkTableColumn field="cusPhone"/>
            <DkTableColumn field="contactName"/>
            <DkTableColumn field="orderStatusName"/>
            <DkTableColumn field="outStatusName" :color-cards="['待出库','出库中','已出库']"/>
            <DkTableColumn field="sumQuantity" dataType="number" sum/>
            <DkTableColumn field="sumStandard" :digits="2" dataType="number" sum/>
            <DkTableColumn field="sumAmount" :digits="2" dataType="number" sum/>
            <DkTableColumn field="saleDiscount" :digits="2" dataType="number"/>
            <DkTableColumn field="outingQty" dataType="number" sum/>
            <DkTableColumn field="outQty" dataType="number" sum/>
            <DkTableColumn field="returnQty" dataType="number" sum/>
            <DkTableColumn field="addressFull"></DkTableColumn>
            <DkTableColumn field="deliveryDate" data-type="date"/>
            <DkTableColumn field="orgName"/>
            <DkTableColumn field="staffName"/>
            <DkTableColumn field="cusFromName"/>
            <DkTableColumn field="channelName"/>
            <DkTableColumn field="makeStaffName"/>
            <DkTableColumn field="makeTime" data-type="dateTime"/>
            <DkTableColumn field="flgValid" type="switch"/>
            <DkTableColumn field="remarks"/>
          </DkTable>
        </div>
        <div slot="bottom">
        <DkTabs v-model="detailModel" :options="detailOptions" @on-click="detailTabsClick"></DkTabs>
        <!--订单明细-->
        <div v-show="visGoodsDetailFlag">
          <DkTable :id="'table-'+$options.name" ref="detail-select" :data="tableDataDetail_goodsDetail" showFooter
                   :height=" this.tableHeight * (1-split)-62" primaryKey="itemId" name="table">
            <DkTableColumn field="skuModel"/>
            <DkTableColumn field="skuName"/>
            <DkTableColumn field="skuCode"/>
            <DkTableColumn field="itemQty" dataType="number" sum/>
            <!-- 基本单位-->
            <DkTableColumn field="box" dataType="number" :digits="0"/>
            <!-- 包装单位-->
            <DkTableColumn field="piece" dataType="number" :digits="0"/>
            <!-- 包装数量-->
            <DkTableColumn field="boxPiece" />
            <DkTableColumn field="priceStd" :digits="2" dataType="number"/>
            <DkTableColumn field="amtStd" :digits="2" dataType="number" sum/>
            <DkTableColumn field="priceSale" :digits="2" dataType="number"/>
            <DkTableColumn field="itemAmt" :digits="2" dataType="number" sum/>
            <DkTableColumn field="priceDiscount" :digits="2" dataType="number"/>
            <DkTableColumn field="whName"/>
            <DkTableColumn field="outingQty" dataType="number" sum/>
            <DkTableColumn field="outingAmt" :digits="2" dataType="number" sum/>
            <DkTableColumn field="outQty" dataType="number" sum/>
            <DkTableColumn field="outAmt" :digits="2" dataType="number" sum/>
            <DkTableColumn field="returnQty" dataType="number" sum/>
            <DkTableColumn field="returnAmt" :digits="2" dataType="number" sum/>
            <DkTableColumn field="nonStdCode"/>
            <DkTableColumn field="remarks"/>
          </DkTable>
        </div>
        <!--收款明细-->
        <div v-show="visCollectionDetailFlag">
          <DkTable :id="'table-'+$options.name" ref="detail-select" :data="tableDataDetail_rpItem" showFooter
                   :height=" this.tableHeight * (1-split)-62" primaryKey="itemId" name="table">
            <DkTableColumn field="macName"/>
            <DkTableColumn field="amtRec" :digits="2" dataType="number" sum/>
            <DkTableColumn field="remarks"/>
          </DkTable>
        </div>
        <!--附件-->
        <div v-show="visFilesDetailFlag">
          <DkTable :pageFlag="false" :pageTotalFlag="false" :id="'table-'+$options.name"
                   name="table" ref="file-select" :data="tableDataDetail_files" :height=" this.tableHeight * (1-split)-62">
            <DkTableColumn field="name" :title="$t('fileName')"/>
            <DkTableColumn field="path" :imageUrl="$config.imgUrl" type="image" :title="$t('yuLan')"/>
          </DkTable>
        </div>

        <PrintModal ref="printModal" :doc-name="$config.docCode.order"
                    :postName="'order'"
                    :id="$refs?.['table-select']?.batchRows?.[0]?.orderId||null"/>
      </div>
    </DkSplit>
    </div>
  </div>
</template>

<script>

import {indexMixin} from '@/mixins'
import PrintModal from '_c/business/print/modal'

export default {
  name: 'order',
  mixins: [indexMixin],
  components: {PrintModal},
  data() {
    let self = this
    return {
      detailModel: 'goodsDetail',
      detailOptions: [{label: self.$t('goodsDetail'), name: 'goodsDetail'}, //货物明细
        {label: self.$t('collectionDetail'), name: 'collectionDetail'},//收款明细
        {label: self.$t('filesDetail'), name: 'filesDetail'}, //附件明细
      ],
      bigImgModal: false,
      bigImgUrl: null,
      visGoodsDetailFlag: true,
      visCollectionDetailFlag: false,
      visFilesDetailFlag: false,
      tableData: [],
      tableDataDetail_goodsDetail: [],  // 用于存放订单明细数据
      tableDataDetail_rpItem: [], // 用于存放订单收款数据
      tableDataDetail_files: [],  // 用于存放订单附件
      searchContent: [
        {
          itemCode: '订单单号-文本',
          itemName: 'orderNo',
          valueFormat: {code: 'orderNo'},
          valueKind: 'STR'
        },
        {
          itemCode: '客户名称-文本',
          itemName: 'cusName',
          valueFormat: {code: 'cusName'},
          valueKind: 'STR'
        },
        {
          itemCode: '客户电话-文本',
          itemName: 'cusPhone',
          valueFormat: {code: 'cusPhone'},
          valueKind: 'STR'
        },
        {
          itemCode: '详细地址-文本',
          itemName: 'addressFull',
          valueFormat: {code: 'addressFull'},
          valueKind: 'STR'
        },
        {
          itemCode: '业务部门-文本',
          itemName: 'orgId',
          valueFormat: {
            code: 'orgIdList',
            data: [],
            valueKey: 'orgId',
            labelKey: 'orgName',
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '业务员-文本',
          itemName: 'staffId',
          valueFormat: {
            code: 'staffIdList',
            data: [],
            valueKey: 'staffId',
            labelKey: 'staffCodeName',
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '出库状态-下拉',
          itemName: 'outStatus',
          valueFormat: {
            code: 'outStatusList',
            data: [
              {outStatus: '出库状态-待出库', outStatusName: '待出库'},
              {outStatus: '出库状态-出库中', outStatusName: '出库中'},
              {outStatus: '出库状态-已出库', outStatusName: '已出库'}],
            valueKey: 'outStatus',
            labelKey: 'outStatusName',
            default: []
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '送货日期-日期',
          itemName: 'deliveryDate',
          valueFormat: {code: 'deliveryDate'},
          valueKind: 'DATE_RANGE'
        },
        {
          itemCode: '制单时间-时间',
          itemName: 'makeTime',
          valueFormat: {
            code: 'makeTime',
            default: [new Date().addMonths(-1).format('yyyy-MM-dd'), new Date().format('yyyy-MM-dd')]
          },
          valueKind: 'DATE_RANGE',
          // required: true,
        },
        self.$config.flgValidSearch
      ],
    }
  },

  methods: {
    /**
    * @desc   : 进入客户页面
    * @author : 常皓宁
    * @date   : 2024/5/21 11:05
    */
    openCustomerMessage(row) {
      this.$router.push('/mst/customer/detail/' + row['cusId'])
    },
    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
      //查询业务部门
      this.getOrg()
      //查询业务员
      this.getStaff()
    },
    /**
     * @desc   : 查询业务部门
     * @author : 付斌
     * @date   : 2024-02-26 14:27
     */
    getOrg() {
      this.excute(this.$service.commonService, this.$service.commonService.getOrg, {}).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          let list = this.searchContent.filter(it => it.valueFormat.code == 'orgIdList')
          if (list.length > 0) {
            list[0].valueFormat.data = res.data
          }
        }
      })
    },
    /**
     * @desc   : 查询业务员
     * @author : 付斌
     * @date   : 2024-02-26 14:27
     */
    getStaff() {
      this.excute(this.$service.commonService, this.$service.commonService.getStaff, {}).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          let list = this.searchContent.filter(it => it.valueFormat.code == 'staffIdList')
          if (list.length > 0) {
            list[0].valueFormat.data = res.data
          }
        }
      })
    },
    /**
     * @desc   : 查询数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    getData(params) {
      this.tableData = [];
      this.tableDataDetail_goodsDetail = [];
      this.tableDataDetail_rpItem = [];
      return this.excute(this.$service.orderService, this.$service.orderService.selectByCond, params)
    },

    /**
     * @desc   : 行改变事件
     * @author : 付斌
     * @date   : 2024-02-28 11:06
     */
    currentChange({row}) {
      this.loading = true
      this.excuteNoParam(this.$service.orderService, this.$service.orderService.selectOrderInfoById, [row.orderId], false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableDataDetail_goodsDetail = res.data.orderItem;
          this.tableDataDetail_rpItem = res.data.receiptItem;
        }
        this.loading = false
      })
      //订单附件
      this.tableDataDetail_files = row.annexPaths
    },
    /**
     * @desc   : 作废
     * @author : 付斌
     * @date   : 2024-03-05 8:56
     */
    invalid(rows) {
      if (rows && rows.length > 0) {
        let row  = rows[0]
        //已出库不允许作废
        if(row.outStatus && row.outStatus ==this.$config.outStatus.yiChuKu){
          this.$Message.success(this.$t('W_130'))
          return
        }
        this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('invalid')}), title: this.$t('systemQuestion')},
          {
            ok: () => {
              this.loading = true
              this.excuteNoParam(this.$service.orderService, this.$service.orderService.invalid, [rows[0].orderId], false).then(res => {
                if (res.code === this.$config.SUCCESS_CODE) {
                  this.$Message.success(this.$t('I_001', {'param': this.$t('invalid')}))
                } else {
                  this.$Message.warning(res.message)
                }
                this.loading = false
              })
            },
          })
      }
    },
    /**
     * @desc   :校验按钮
     * @author : 付斌
     * @date   : 2024-03-09 16:58
     */
    openValidData(btnName, rows) {
      if (btnName === 'edit') {
        if (rows && rows.length > 0) {
          if (rows[0].outQty === rows[0].sumQuantity) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_128')}));
            return false;
          }
        }
      } else if (btnName === 'orderOut') {
        if (rows && rows.length > 0) {
          if (rows[0].outingQty === rows[0].sumQuantity) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_126')}));
            return false;
          }
        }
      } else if (btnName === 'orderOutReturn') {
        if (rows && rows.length > 0) {
          if (rows[0].outQty <= rows[0].returnQty) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_127')}));
            return false;
          }
        }
      } else if (btnName === 'invalid') {
        if (rows && rows.length > 0) {
          if (!rows[0].flgValid) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_122')}));
            return false;
          } else if (rows[0].outingQty != 0 || rows[0].outQty != 0) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_125')}));
            return false;
          }
        }
      }
      return true;
    },
    /**
     * @desc   : 点击Tab进行切换
     * @author : 付斌
     * @date   : 2024-02-19 11:33
     */
    detailTabsClick(val) {
      if (val === 'goodsDetail') {
        this.visGoodsDetailFlag = true
        this.visCollectionDetailFlag = false
        this.visFilesDetailFlag = false
      } else if (val === 'collectionDetail') {
        this.visGoodsDetailFlag = false
        this.visCollectionDetailFlag = true
        this.visFilesDetailFlag = false
      } else if (val === 'filesDetail') {
        this.visGoodsDetailFlag = false
        this.visCollectionDetailFlag = false
        this.visFilesDetailFlag = true
      }
    },
    /**
     * @desc   : 选择模板、打印机
     * @author : 洪旭东
     * @date   : 2023-04-10 13:45
     */
    print() {
      this.$refs.printModal.showChooseTemplate = true
    },
    /***************************设定参数函数************************************/
  },
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    // console.log('this.$store.state.user.cpId',this.$store.state.user)
    this.primaryKey = 'orderId'  // 设置主键Id
    this.routeObjName = 'order'  // 设置路由名称
  },
}
</script>

