<!-- @desc:销售退货  @auth:付斌  @time:2023-09-13 10:28 -->
<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="entryHandle" name="entryHandle"></BaseIndexButton>
        <BaseIndexButton ref="entryBack" name="entryBack"></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"
                    @on-switch-change="changeShowItem"></SearchCond>
      </div>
      <!--  表格部分  -->
      <DkSplit mode="vertical" v-model="split" :height="tableHeight">
        <div slot="top">
          <DkTabs v-model="tableModel" :options="tabsOptions" @on-click="tabsClick"></DkTabs>
          <DkTable slot="top"
                   :id="'table-'+$options.name"
                   ref="table-select"
                   :data="tableData"
                   multiple 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-return-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="contactName"></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="outDate" data-type="date"></DkTableColumn>
            <DkTableColumn field="outStatusName" :color-cards="['待出库','出库中','已出库']"></DkTableColumn>
            <DkTableColumn field="orgName"></DkTableColumn>
            <DkTableColumn field="staffName"></DkTableColumn>
            <DkTableColumn field="makeTime" :dataType="$config.columnType.dateTime"></DkTableColumn>
            <DkTableColumn field="makeStaffName"></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="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="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-return',
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      // 模式切换值
      tableModel: 'all',
      tabsOptions: [
        {label: self.$t('all'), name: 'all'},       //全部
        {label: self.$t('waitIn'), name: 'waitIn'}, //待入库
        {label: self.$t('ingIn'), name: 'ingIn'},   //已入库
      ],
      detailModel: 'goodsDetail',
      detailOptions: [
        {label: self.$t('goodsDetail'), name: 'goodsDetail'}, //货物明细
        {label: this.$t('filesDetail'), name: 'filesDetail'}
      ],
      visGoodsDetailFlag: true,
      visFilesDetailFlag: false,
      tableData: [],
      tableDataDetail_goodsDetail: [],
      tableDataDetail_files: [],
      searchContent: [
        {
          itemCode: '退货单号-文本',
          itemName: 'returnNo',
          valueFormat: {code: 'returnNo'},
          valueKind: 'STR'
        },
        {
          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: 'pickupDate',
          valueFormat: {code: 'outDate'},
          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'])
    },

    edit(rows) {
      if (!rows || rows.length == 0) {
        return;
      }
      let routeUrl = this.$config.routeUrl.orderOutReturn.editForm
      if (!rows[0]['fromId']) {
        routeUrl = this.$config.routeUrl.orderOutReturn.editOutReturnForm
      }
      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.saleReturn
      return params;
    },
    /**
     * @desc   : 查询数据
     * @author : 付斌
     * @date   : 2024-03-07 16:01
     */
    getData(params) {
      this.tableData = [];
      this.tableDataDetail_goodsDetail = [];
      return this.excute(this.$service.orderOutReturnService, this.$service.orderOutReturnService.selectByCond, params)
    },
    /**
     * @desc   : 行改变事件
     * @author : 付斌
     * @date   : 2024-02-28 11:06
     */
    currentChange({row}) {
      this.loading = true
      this.excuteNoParam(this.$service.orderOutReturnService, this.$service.orderOutReturnService.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   : 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.orderOutReturnService, this.$service.orderOutReturnService.invalid, [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   : 2024-02-19 11:33
     */
    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-02-21 16:14
     */
    entryHandle(row) {
      if (row && row[0] && row[0].intoId) {
        this.$router.push('/sale/order-return/handle/' + row[0].intoId)
      } else {
        this.$Message.error(this.$t('W_013'))
      }
    },

    /**
     * @desc   : 不查冲正单据
     * @author : 付斌
     * @date   : 2023/12/15 15:56
     */
    changeShowItem({code, e}) {

    },

    /**
     * @desc   : 入库撤销
     * @author : 付斌
     * @date   : 2023/10/31 16:31
     */
    entryBack() {
      //当前退货单不存在入库数据
      if (this.$refs['table-select'].batchRows.length === 1) {
        if (!this.$refs['table-select'].batchRows[0].intoId) {
          this.$Message.error(this.$t('W_098'))
          return
        }
        //入库中数据不允许进行入库撤回
        if (this.$refs['table-select'].batchRows[0].intoStatus &&
          this.$refs['table-select'].batchRows[0].intoStatus == this.$config.intoStatus.ruKuZhong) {
          this.$Message.error(this.$t('W_097'))
          return
        }
        let params = {
          intoId: this.$refs['table-select'].batchRows[0].intoId,
          cpId: this.$store.state.user.cpId
        }
        this.$IBMessage({
            content: this.$t('Q_002', {'param': this.$v('entryBack')}),
            title: this.$t('systemQuestion')
          },
          {
            // 弹出是否删除对话框-是
            ok: () => {
              //其他入库撤销
              this.excute(this.$service.saleReturnInboundService, this.$service.saleReturnInboundService.saleReturnInboundCancel, params).then(res => {
                if (res.code === this.$config.SUCCESS_CODE) {
                  this.$Message.success(this.$t('I_001', {'param': this.$v('entryBack')}))
                  this.getData()
                } else {
                  this.$Message.error(res.message)
                }
              })
            },
          })
      } else {
        this.$Message.error(this.$t('W_013'))
      }
    },
    /**
     * @desc   :校验按钮
     * @author : 付斌
     * @date   : 2024-03-09 16:58
     */
    openValidData(btnName, rows) {
      if (btnName === 'invalid') {
        if (rows && rows.length > 0) {
          if (!rows[0].flgValid) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_123')}));
            return false;
          }
        }
      }
      return true;
    },
    /***************************列表及其他事件************************************/

    /***************************created调用数据接口函数************************************/

  },

  /**
   * @desc   : 实例被挂载后调用
   * @author : 付斌
   * @date   : 2022/3/3 10:31
   */
  mounted() {
  },

  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.primaryKey = 'outId'  // 设置主键Id
    this.routeObjName = 'orderOutReturn'  // 设置路由名称
  },
}
</script>
