<!-- @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="saleOut" name="saleOut"></BaseIndexButton>
        <BaseIndexButton ref="saleOutReturn" name="saleOutReturn"></BaseIndexButton>
        <BaseIndexButton ref="invalid" name="invalid"></BaseIndexButton>
      </template>
      <template #right>
        <BaseIndexButton ref="add" name="add"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>

    <!--  查询条件区域  -->
    <div id="search-cond-div" ref="search-cond-div" style="margin-top: 1px;padding: 0">
      <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" style="margin-left: 5px;">
        <DkTable slot="top"
                 :id="'table-'+$options.name"
                 ref="table-select"
                 :data="tableData"
                 multiple
                 :height="this.tableHeight * split - 35"
                 primaryKey="orderId"
                 :pageFlag="true"
                 @pageChange="pageSizeChange"
                 :page-total="pageInfo.total"
                 :current-page="pageInfo.currentPage"
                 :choose-flag="false"
                 @current-change="currentChange($event)"
                 name="table">
          <DkTableColumn :type="$libaray.checkButtonHasRight('order-edit')?'link':''" field="orderNo"
                         :tree-node="true" @on-link="edit"></DkTableColumn>
          <DkTableColumn field="cusName"></DkTableColumn>
          <DkTableColumn field="cusPhone"></DkTableColumn>
          <DkTableColumn field="contactName"></DkTableColumn>
          <DkTableColumn field="orderStatusName"></DkTableColumn>
          <DkTableColumn field="outStatusName"></DkTableColumn>
          <DkTableColumn field="sumQuantity" sum></DkTableColumn>
          <DkTableColumn field="sumStandard" :digits="2" sum></DkTableColumn>
          <DkTableColumn field="sumAmount" :digits="2" sum></DkTableColumn>
          <DkTableColumn field="saleDiscount" :digits="2" sum></DkTableColumn>
          <DkTableColumn field="amtReceivable" :digits="2" sum></DkTableColumn>
          <DkTableColumn field="amtHandle" :digits="2" sum></DkTableColumn>
          <DkTableColumn field="amtResidue" :digits="2" sum></DkTableColumn>
          <DkTableColumn field="addressFull" dataType="string"></DkTableColumn>
          <DkTableColumn field="deliveryDate" data-type="date"></DkTableColumn>
          <DkTableColumn field="orgName"></DkTableColumn>
          <DkTableColumn field="staffName"></DkTableColumn>
          <DkTableColumn field="cusFromName"></DkTableColumn>
          <DkTableColumn field="channelName"></DkTableColumn>
          <DkTableColumn field="makeStaffName"></DkTableColumn>
          <DkTableColumn field="makeTime" data-type="dateTime"></DkTableColumn>
          <DkTableColumn field="remarks"></DkTableColumn>
        </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"
                   :height="this.tableHeight * (1-split)" primaryKey="itemId" name="table">
            <DkTableColumn
              v-for="(item, index) in tableDataTitle_goodsDetail"
              :key="detailModel+index"
              align="center"
              :type="item.type"
              :field="item.field"
              :title="item.title"
              :sum="item.sum"
              :digits="item.digits"
              :width="item.width"
              :dataType="item.dataType"
              :tree-node="item.treeNode"
            >
            </DkTableColumn>
          </DkTable>
        </div>
        <!--收款明细-->
        <div v-show="visCollectionDetailFlag">
          <DkTable :pageFlag="false" :pageTotalFlag="false" primaryKey="receiptId" :id="'table-'+$options.name"
                   :show-footer="true"
                   name="table" ref="receipt-select" :data="tableDataDetail_collectionDetail"
                   :height="this.tableHeight * (1-split)">
            <DkTableColumn
              v-for="(item, index) in tableDataTitle_collectionDetail"
              :key="detailModel+index"
              align="center"
              :type="item.type"
              :field="item.field"
              :title="item.title"
              :sum="item.sum"
              :digits="item.digits"
              :width="item.width"
            >
            </DkTableColumn>
          </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)">
            <DkTableColumn field="docType" min-width="150" width="150"></DkTableColumn>
            <DkTableColumn field="name" min-width="300" width="300"></DkTableColumn>
            <vxe-column field="createTime" min-width="150" width="150"></vxe-column>
            <vxe-column :title="$t('picture')" class-name="img-column" width="100">
              <template #default="{row}">
                <img :src="$config.imgUrl + row.url" v-if="row.type == 'img' || row.type == 'image'"
                     style="width: 100px; height: 100px; cursor: pointer;"
                     @click="()=>{
              bigImgModal = true
              bigImgUrl = $config.imgUrl + row.url
              }"/>
              </template>
            </vxe-column>
            <vxe-column>
              <template #default="{row}">
                <DkButton @click="openFile(row)">
                  <Icon type="md-download"/>
                </DkButton>
              </template>
            </vxe-column>
          </DkTable>
        </div>

        <!--收定金  取定金-->
        <DkModal
          v-model="depositModal"
          :loading="loading"
          ref="modal_editAdd"
          @on-visible-change="handleReceiptVisible"
          @modalOk="depositSubmitForm"
          @modalCancel="depositClearForm"
          :saveFlag="true"
          :title=depositTitle
        >
          <DkForm ref="formInline" style="width:100%;" name="dk-form" v-model="depositFormData">
            <!--销售单号-->
            <DkFormItem style="margin-top: 10px;" prop="orderNo" :required="true">
              <InputPop ref="orderNo" :readonly="true" v-model="depositFormData.orderNo"/>
            </DkFormItem>
            <!--业务员-->
            <DkFormItem style="margin-top: 10px;" prop="staffName" :required="true">
              <InputPop ref="staffName" :readonly="true" v-model="depositFormData.staffName"/>
            </DkFormItem>
            <!--业务部门-->
            <DkFormItem style="margin-top: 10px;" prop="orgName" :required="true">
              <InputPop ref="orgName" :readonly="true" v-model="depositFormData.orgName"/>
            </DkFormItem>
            <!--客户电话-->
            <DkFormItem style="margin-top: 10px;" prop="customerName" :required="true">
              <InputPop ref="customerName" :readonly="true" v-model="depositFormData.customerName"/>
            </DkFormItem>
            <DkFormItem prop="settlementType" style="margin-top: 10px;" :label="$t('settlementTypesName')"
                        :required="true">
              <SelectPop v-model="depositFormData.settlementType"
                         :multiple="false" :options="settlementTypeList"
                         labelKey="typeName"
                         valueKey="typeId"/>
            </DkFormItem>
            <DkFormItem prop="sumAmount" style="margin-top: 10px;" :required="true" :label="$t('collectionRequired')">
              <InputNumberPop v-model="depositFormData.sumAmount" @on-blur="calculationItemAmount"
                              :negative=true :digits=2 :max="9999999"/>
            </DkFormItem>
            <DkFormItem style="margin-top: 10px;" prop="remarks">
              <InputPop v-model="depositFormData.remarks" textareaFlag/>
            </DkFormItem>
            <DkFormItem style="margin-top: 10px;" :label="$t('file')">
              <DkPicWall v-model="depositFormData.annexPaths" folder="t_receipt/annex_paths"
                         :accept="$config.uploadFileConfig.acceptPicType"
                         :format="['jpg','jpeg','png']"></DkPicWall>
            </DkFormItem>
          </DkForm>
        </DkModal>
      </div>
    </DkSplit>
  </div>
</template>

<script>

import {indexMixin} from '@/mixins'

export default {
  name: 'order',
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      orderId: 0,//查询明细需要的订单id
      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,
      //总单导出条件
      exportCondition: null,
      totalTab: null,//存放总单tab切换到那个标签
      pageInfo: {
        total: 0,
        pageSize: 20,
        currentPage: 1
      },
      // 模式切换值
      tabsModel: 'orderAll',
      tabsOptions: [{label: self.$t('orderAll'), name: 'orderAll'}, //全部订单
        {label: self.$t('priceOrder'), name: 'priceOrder'}, //潜客报价
        {label: self.$t('dealOrder'), name: 'dealOrder'},//本月订单
        {label: self.$t('customDesign'), name: 'customDesign'},//我的订单
        {label: self.$t('followOrder'), name: 'followOrder'},//关注订单
        // { label: '异常订单', name: '6' },
      ],
      tableData: [],
      tableDataDetail_goodsDetail: [],  // 用于存放订单明细数据
      tableDataDetail_collectionDetail: [], // 用于存放订单收款数据
      tableDataDetail_files: [],  // 用于存放订单附件
      tableDataTitle_goodsDetail: [
        {field: 'skuModel', treeNode: true},
        {field: 'skuName'},
        {field: 'skuCode'},
        {field: 'itemQty', type: 'number', digits: 0, sum: true},
        {field: 'priceStd', digits: 2, type: 'number'},
        {field: 'amtStd', digits: 2, type: 'number'},
        {field: 'priceSale', digits: 2, type: 'number'},
        {field: 'itemAmt', type: 'number', digits: 2, sum: true},
        {field: 'priceDiscount', digits: 2, type: 'number'},
        {field: 'outingQty', type: 'number', digits: 0, dataType: 'number', sum: true},
        {field: 'outingAmt', type: 'number', digits: 2, dataType: 'number', sum: true},
        {field: 'outQty', type: 'number', digits: 0, dataType: 'number', sum: true},
        {field: 'outAmt', type: 'number', digits: 2, dataType: 'number', sum: true},
        {field: 'returnQty', type: 'number', digits: 0, dataType: 'number', sum: true},
        {field: 'returnAmt', type: 'number', digits: 2, dataType: 'number', sum: true},
        {field: 'nonStdCode'},
        {field: 'remarks'},
      ],
      tableDataTitle_collectionDetail: [
        {field: 'handleNo'},
        {field: 'sumHandleAmt', type: 'number', digits: 2, sum: true},
        {field: 'settlementTypeName'},
        {field: 'makeUserName'},
        {field: 'makeTime'},
      ],
      searchContent: [
        {
          itemCode: '订单单号-文本',
          itemName: 'orderNo',
          valueFormat: {code: 'orderNo'},
          valueKind: 'STR'
        },
        {
          itemCode: '客户电话-文本',
          itemName: 'cusPhone',
          valueFormat: {code: 'customerPhone'},
          valueKind: 'STR'
        },
        {
          itemCode: '客户名称-文本',
          itemName: 'cusName',
          valueFormat: {code: 'customerName'},
          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: 'addressFull',
          valueFormat: {code: 'addressFull'},
          valueKind: 'STR'
        },
        {
          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,
        },
        {
          itemCode: '送货时间-时间',
          itemName: 'deliveryDate',
          valueFormat: {code: 'deliveryDate'},
          valueKind: 'DATE_RANGE'
        },
      ],
      depositModal: false,//收取定金标识
      depositTitle: this.$t('deposit'),//收取定金弹窗名称
      depositFormData: {
        orderNo: null,
        staffId: null,
        staffName: null,
        orgId: null,
        orgName: null,
        customerId: null,
        customerName: null,
        sumAmount: 0,
        receivable: 0,
        settlementType: null,
        settlementTypeName: null,
        remarks: '',
        annexPaths: [],
      },
      settlementTypeList: [],
    }
  },

  methods: {
    /**
     * @desc   : 新建
     * @author : 付斌
     * @date   : 2024-02-19 9:59
     */
    add() {
      this.$router.push('/sale/order/add');
    },
    /**
     *   @desc   : 计算总和
     *   @date   : 2022/6/10 14:53
     *   @author : 付斌
     */
    calculationItemAmount(value) {

    },
    /**
     *   @desc   : 定金表单赋值
     *   @date   : 2022/7/21 8:48
     *   @author : 付斌
     */
    deposit() {

    },
    /**
     * @desc   : 定金表单提交
     * @author : 付斌
     * @date   : 2024-02-20 15:55
     */
    depositSubmitForm() {

    },
    /**
     * @desc   : 清空新建form
     * @author : 付斌
     * @date   : 2024-02-20 15:55
     */
    depositClearForm() {

    },
    /**
     * @desc   : 进入编辑页面
     * @author : 付斌
     * @date   : 2022/8/9 10:29
     */
    edit(rows) {
      if (rows && rows.length > 0) {
        this.$router.push('/sale/order/edit/' + rows[0].orderId)
      }
    },
    /**
     * @desc   : 销售出库
     * @author : 付斌
     * @date   : 2024-03-05 8:56
     */
    saleOut(rows) {
      if (rows && rows.length > 0) {
        this.$router.push('/sale/saleOut/add/' + rows[0].orderId)
      }
    },
    /**
     * @desc   : 销售出库退货
     * @author : 付斌
     * @date   : 2024-03-05 8:56
     */
    saleOutReturn(rows) {
      if (rows && rows.length > 0) {
        this.$router.push('/sale/saleReturn/add/' + rows[0].orderId)
      }
    },
    /**
     * @desc   : 作废
     * @author : 付斌
     * @date   : 2024-03-05 8:56
     */
    invalid(rows) {
      if (rows && rows.length > 0) {
      }
    },
    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
      //查询业务部门
      this.selectOrg()
      //查询业务员
      this.selectStaff()
    },

    /**
     * @desc   : 查询业务部门
     * @author : 付斌
     * @date   : 2024-02-26 14:27
     */
    selectOrg() {
      let params = {
        cpId: this.$store.state.user.cpId,
        flgValid: true
      }
      this.excute(this.$service.commonService, this.$service.commonService.getOrg, params).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
     */
    selectStaff() {
      let params = {
        cpId: this.$store.state.user.cpId,
        flgValid: true
      }
      this.excute(this.$service.commonService, this.$service.commonService.getStaff, params).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) {
      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.selectOrderItemById, [row.orderId], false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableDataDetail_goodsDetail = res.data.orderItem;
        }
        this.loading = false
      })
    },
    /**
     * @desc   : 关注订单
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    switchFollowOrderChange(value) {

    },
    /**
     *   @desc   : 冲正
     *   @date   : 2022/6/24 16:15
     *   @author : 付斌
     */
    revert() {

    },
    /***************************列表及其他事件************************************/

    /**
     * @desc   :分页
     * @author : 付斌
     * @date   : 2024-02-20 15:52
     */
    pageSizeChange(pageInfo) {
      this.pageInfo = pageInfo
      this.getData() // 查询数据
    },
    /**
     * @desc   : 总单行事件选择
     * @author : 付斌
     * @date   : 2024-02-20 15:52
     */
    selectChangeTable(e) {

    },
    /**
     * @desc   : 总单tab切换
     * @author : 付斌
     * @date   : 2024-02-20 15:51
     */
    tabsClick(val) {
      //如果切换的tab存在就去根据条件查询
      if (val) {
        this.totalTab = val
      }
    },

    /***************************设定参数函数************************************/

    /**
     * @desc   : 打开窗体，进行查询结算方式
     * @author : 付斌
     * @date   : 2024-02-20 15:51
     */
    handleReceiptVisible(e) {
    },

    /**
     * @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
      }
    },
    /***************************created调用数据接口函数************************************/

  },
  /**
   * @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>

