<!-- @desc:销售出库 @auth:付斌 @time : 2023/9/25 14:45 -->
<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="outHandle" name="outHandle"></BaseIndexButton>
        <BaseIndexButton ref="outBack" name="outBack"></BaseIndexButton>
        <BaseIndexButton ref="outReturn" name="outReturn"></BaseIndexButton>
        <BaseIndexButton ref="invalid" name="invalid"></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" style="width: 100%;height: 100%;top: 0;">
          <DkTabs v-model="tableModel" :options="tabsOptions" @on-click="tabsClick(1)"></DkTabs>
          <DkTable slot="top"
                   :id="'table-'+$options.name"
                   ref="table-select"
                   :data="tableData"
                   showFooter
                   :height="this.tableHeight * split -  66"
                   primaryKey="outId"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :current-page="pageInfo.currentPage"
                   :choose-flag="false"
                   @current-change="currentChange($event)"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   name="table">
            <DkTableColumn field="outNo" :type="$libaray.checkButtonHasRight('order-out-edit')?'link':''"
                           @on-link="handleLink"/>
            <DkTableColumn field="biznisNo"></DkTableColumn>
            <DkTableColumn field="cusName" :type="$libaray.checkButtonHasRight('customer-detail')?'link':''"
                           @on-link="openCustomerMessage"></DkTableColumn>
            <DkTableColumn field="cusPhone"></DkTableColumn>
            <DkTableColumn field="contactPhone"></DkTableColumn>
            <DkTableColumn field="addressFull"></DkTableColumn>
            <DkTableColumn field="outingQty" dataType="number" sum></DkTableColumn>
            <DkTableColumn field="outingAmt" :digits="2" dataType="number" sum></DkTableColumn>
            <DkTableColumn field="outQty" dataType="number" sum></DkTableColumn>
            <DkTableColumn field="outAmt" :digits="2" dataType="number" sum></DkTableColumn>
            <DkTableColumn field="returnQty" dataType="number" sum></DkTableColumn>
            <DkTableColumn field="returnAmt" :digits="2" dataType="number" sum></DkTableColumn>
            <DkTableColumn field="outDate" data-type="date"></DkTableColumn>
            <DkTableColumn field="outStatusName" :color-cards="['待出库','出库中','已出库']"></DkTableColumn>
            <DkTableColumn field="orgName"></DkTableColumn>
            <DkTableColumn field="staffName"></DkTableColumn>
            <DkTableColumn field="makeStaffName"></DkTableColumn>
            <DkTableColumn field="makeTime" data-type="dateTime"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"/>
            <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" showFooter
                     :height="this.tableHeight * (1-split)-62" primaryKey="itemId" name="table">
              <DkTableColumn field="skuModel"></DkTableColumn>
              <DkTableColumn field="skuName"></DkTableColumn>
              <DkTableColumn field="skuCode"></DkTableColumn>
              <DkTableColumn field="outStatusName"></DkTableColumn>
              <DkTableColumn field="outingQty" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outingBox" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outingPiece" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outingBoxPiece"/>
              <DkTableColumn field="outingAmt" :digits="2" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outQty" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outBox" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outPiece" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="outBoxPiece"/>
              <DkTableColumn field="outAmt" :digits="2" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="returnQty" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="returnAmt" :digits="2" dataType="number" sum></DkTableColumn>
              <DkTableColumn field="priceOut"></DkTableColumn>
              <DkTableColumn field="whName"></DkTableColumn>
              <DkTableColumn field="nonStdCode"></DkTableColumn>
              <DkTableColumn field="remarks"></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)-62">
              <DkTableColumn field="name" :title="$t('fileName')"/>
              <DkTableColumn field="path" :imageUrl="$config.imgUrl" type="image" :title="$t('yuLan')"/>
            </DkTable>
          </div>
        </div>
      </DkSplit>
    </div>

  </div>
</template>
<script>
import {indexMixin} from "@/mixins";

export default {
  name: 'order-out',
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      tableModel: 'waitOut',
      tabsOptions: [
        {label: this.$t('waitOut'), name: 'waitOut'},//待出库
        {label: this.$t('ingOut'), name: 'ingOut'}//已出库
      ],
      detailModel: 'goodsDetail',
      detailOptions: [
        {label: this.$t('goodsDetail'), name: 'goodsDetail'},
        {label: this.$t('filesDetail'), name: 'filesDetail'}
      ],
      visGoodsDetailFlag: true,
      visFilesDetailFlag: false,
      tableData: [],
      tableDataDetail_goodsDetail: [],//出库明细
      tableDataDetail_files: [],  // 用于存放订单附件
      searchContent: [
        {
          itemCode: 'outNo',
          valueFormat: {code: 'outNo'},
          valueKind: 'STR'
        },
        {
          itemCode: 'orderNo',
          valueFormat: {code: 'orderNo'},
          valueKind: 'STR'
        },
        {
          itemCode: 'cusName',
          valueFormat: {code: 'cusName'},
          valueKind: 'STR'
        },
        {
          itemCode: 'cusPhone',
          valueFormat: {code: 'cusPhone'},
          valueKind: 'STR'
        },
        {
          itemCode: 'orgId',
          valueFormat: {
            code: 'orgIdList',
            data: [],
            valueKey: 'orgId',
            labelKey: 'orgName',
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '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: 'outDate',
          valueFormat: {code: 'outDate'},
          valueKind: 'DATE_RANGE',
        },
        {
          itemCode: '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'])
    },

    edit(rows) {
      if (!rows || rows.length == 0) {
        return;
      }
      let routeUrl = this.$config.routeUrl.orderOut.editForm
      if (!rows[0]['fromId']) {
        routeUrl = this.$config.routeUrl.orderOut.editOrderOutForm
      }
      this.open(routeUrl, 'table-select', true);
    },
    /**
     * @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-03-08 14:48
     */
    setSearchParams(params) {
      params.outType = this.$config.outTypeBySql.order
      return params;
    },
    /**
     * @desc   :查询数据
     * @author : 付斌
     * @date   : 2024-03-07 16:01
     */
    getData(params) {
      this.tableData = [];
      this.tableDataDetail_goodsDetail = [];

      return this.excute(this.$service.orderOutService, this.$service.orderOutService.selectByCond, params)
    },
    /**
     * @desc   : 行改变事件
     * @author : 付斌
     * @date   : 2024-02-28 11:06
     */
    currentChange({row}) {
      this.loading = true
      this.excuteNoParam(this.$service.orderOutService, this.$service.orderOutService.selectOutboundInfoById, [row.outId], false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableDataDetail_goodsDetail = res.data.outboundItem;
          this.loading = false
        }
      })
      this.tableDataDetail_files = row.annexPaths
    },
    /**
     * @desc   : 出库撤回
     * @author : 付斌
     * @date   : 2023/9/01 14:02
     */
    outBack(rows) {
      if (rows && rows.length > 0) {
        this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('outBack')}), title: this.$t('systemQuestion')},
          {
            ok: () => {
              this.loading = true
              let params = {
                outId: rows[0].outId
              }
              this.excute(this.$service.orderOutService, this.$service.orderOutService.saleOrderOutboundCancel, params, false).then(res => {
                if (res.code === this.$config.SUCCESS_CODE) {
                  this.$Message.success(this.$t('I_001', {'param': this.$t('outBack')}))
                } else {
                  this.$Message.warning(res.message)
                }
                this.loading = false
              })
            },
          })
      }
    },
    /**
     * @desc   : 作废
     * @author : 付斌
     * @date   : 2024-03-05 8:56
     */
    invalid(rows) {
      if (rows && rows.length > 0) {
        this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('invalid')}), title: this.$t('systemQuestion')},
          {
            ok: () => {
              this.loading = true
              this.excuteNoParam(this.$service.orderOutService, this.$service.orderOutService.repeal, [rows[0].outId], 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   : 总单tab切换
     * @author : 付斌
     * @date   : 2024-03-07 16:06
     */
    tabsClick(val) {

    },
    /**
     * @desc   : 明细tab切换事件
     * @author : 付斌
     * @date   : 2023/8/31 14:58
     */
    detailTabsClick(val) {
      if (val === 'goodsDetail') {
        this.visGoodsDetailFlag = true
        this.visFilesDetailFlag = false
      } else if (val === 'filesDetail') {
        this.visGoodsDetailFlag = false
        this.visFilesDetailFlag = true
      }
    },
    /**
     * @desc   :校验按钮
     * @author : 付斌
     * @date   : 2024-03-09 16:58
     */
    openValidData(btnName, rows) {
      if (btnName === 'outReturn') {
        if (rows && rows.length > 0) {
          if (!rows[0].flgValid) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_121')}));
            return false;
          }
          if (rows[0].fromId) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_123')}));
            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_121')}));
            return false;
          }
        }
      }
      return true;
    },
  },
  created() {
    this.primaryKey = 'outId'  // 设置主键Id
    this.routeObjName = 'orderOut'  // 设置路由名称
  },
}
</script>
