<!-- @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="outHandle" name="outHandle"></BaseIndexButton>
        <!--    出库撤回    -->
        <BaseIndexButton ref="outBack" name="outBack"></BaseIndexButton>
        <!--    作废    -->
        <BaseIndexButton ref="invalid" name="invalid"></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"
                    :search-content="searchContent"
                    @on-switch-change="changeShowCancel"></SearchCond>
      </div>
      <!--  表格部分  -->
      <DkSplit mode="vertical" v-model="split" :height="tableHeight">
        <div slot="top">
          <!-- 待出库/已出库Tab页-->
          <DkTabs v-model="tableModel" :options="tabsOptions" @on-click="changeTab(1)"></DkTabs>
          <!-- 列表总单  -->
          <!--待出库Tbale-->
          <DkTable :id="'table-'+$options.name"
                   ref="table-select"
                   :data="tableData"
                   :height="this.tableHeight * split - 66"
                   primaryKey="intoId"
                   :multiple="false"
                   :choose-flag="false"
                   v-if="this.tableModel === 'waitOut'"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   @current-change="currentChange($event)"
                   @pageChange="pageSizeChange">
            <!-- 采购出库单号-->
            <DkTableColumn field="intoNo" :title="$t('returnNo')" :type="$libaray.checkButtonHasRight('pur-return-edit')?'link':''"
                           @on-link="handleLink"></DkTableColumn>
            <!--部门-->
            <DkTableColumn field="orgName"></DkTableColumn>
            <!--员工-->
            <DkTableColumn field="staffName"></DkTableColumn>
            <!--供应商-->
            <DkTableColumn field="supName"></DkTableColumn>
            <!--联系人-->
            <DkTableColumn field="contactName"></DkTableColumn>
            <!--联系电话-->
            <DkTableColumn field="contactPhone"></DkTableColumn>
            <!--出库状态-->
            <DkTableColumn field="intoStatusName" :color-cards="['入库中','已入库']"></DkTableColumn>
            <!-- 出库中数量-->
            <DkTableColumn field="intoingQty" :digits="0" sum :title="$t('intoingQty')" dataType="number"></DkTableColumn>
            <!-- 出库中金额-->
            <DkTableColumn field="intoingAmt" :digits="2" sum :title="$t('intoingAmt')" dataType="number"></DkTableColumn>
            <!--制单人-->
            <DkTableColumn field="makeStaffName"></DkTableColumn>
            <!--制单时间-->
            <DkTableColumn field="makeTime"></DkTableColumn>
            <!--备注-->
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>

          <!--已出库Tbale-->
          <DkTable :id="'table-'+$options.name"
                   ref="table-select"
                   :data="tableData"
                   :height="this.tableHeight * split - 66"
                   primaryKey="intoId"
                   :multiple="false"
                   :choose-flag="false"
                   v-if="this.tableModel === 'ingOut'"
                   @current-change="currentChange($event)"
                   @pageChange="pageSizeChange">
            <!-- 出库单号-->
            <DkTableColumn field="intoNo" :title="$t('returnNo')" :type="$libaray.checkButtonHasRight('pur-return-edit')?'link':''"
                           @on-link="handleLink"></DkTableColumn>
            <!--部门-->
            <DkTableColumn field="orgName"></DkTableColumn>
            <!--员工-->
            <DkTableColumn field="staffName"></DkTableColumn>
            <!--供应商-->
            <DkTableColumn field="supName"></DkTableColumn>
            <!--联系人-->
            <DkTableColumn field="contactName"></DkTableColumn>
            <!--联系电话-->
            <DkTableColumn field="contactPhone"></DkTableColumn>
            <!--出库状态-->
            <DkTableColumn field="intoStatusName"></DkTableColumn>
            <!--已出库数量-->
            <DkTableColumn field="intoQty" :digits="0" sum :title="$t('intoQty')" dataType="number"></DkTableColumn>
            <!--已出库金额-->
            <DkTableColumn field="intoAmt" :digits="2" sum :title="$t('intoAmt')" dataType="number"></DkTableColumn>
            <!--制单人-->
            <DkTableColumn field="makeStaffName"></DkTableColumn>
            <!--制单时间-->
            <DkTableColumn field="makeTime"></DkTableColumn>
            <!--备注-->
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>
        </div>
        <div slot="bottom">
          <DkTabs v-model="tabsModel" :options="detailTabsOptions" @on-click="tabsClick"></DkTabs>
          <!-- 货物明细-->
          <div v-show="standardGoodsFlag">
            <DkTable ref="standardGoodsFlag"
                     primaryKey="itemId"
                     :showCheckboxHeader="false"
                     :choose-flag="false"
                     :id="'table-'+$options.name"
                     name="table2" :data="goodsDetailData"
                     :height="this.tableHeight * (1-split)-32"
                     showFooter
                     :select-flag="false"
                     :pageFlag="false"
                     :pageTotalFlag="false"
                     v-if="this.tableModel === 'waitOut'">
              <!-- 商品型号-->
              <DkTableColumn field="skuModel" :filter="false"></DkTableColumn>
              <!-- 商品名称-->
              <DkTableColumn field="skuName"></DkTableColumn>
              <!-- 唯一编码-->
              <DkTableColumn field="skuCode" :filter="false"></DkTableColumn>
              <!-- 品牌-->
              <DkTableColumn field="brandName" :filter="false"></DkTableColumn>
              <!--品牌简称-->
              <DkTableColumn field="shortName" :filter="false"></DkTableColumn>
              <!--非标号-->
              <DkTableColumn field="nonStandardCode"></DkTableColumn>
              <!--出库中数量-->
              <DkTableColumn field="intoingQty" :title="$t('intoingQty')" dataType="number"></DkTableColumn>
              <!--出库中金额-->
              <DkTableColumn field="intoingAmt" :digits="2" :title="$t('intoingAmt')" dataType="number"></DkTableColumn>
              <!-- 基本单位-->
              <DkTableColumn field="intoingBox" :title="$t('box')" dataType="number" :digits="0"/>
              <!-- 包装单位-->
              <DkTableColumn field="intoingPiece" :title="$t('piece')" dataType="number" :digits="0"/>
              <!-- 包装数量-->
              <DkTableColumn field="ingBoxPiece" />
              <!--备注-->
              <DkTableColumn field="remarks"></DkTableColumn>
            </DkTable>

            <DkTable ref="standardGoodsFlag"
                     primaryKey="itemId"
                     :showCheckboxHeader="false"
                     :choose-flag="false"
                     :id="'table-'+$options.name"
                     name="table2" :data="goodsDetailData"
                     :height="this.tableHeight * (1-split)-32"
                     showFooter
                     :select-flag="false"
                     :pageFlag="false"
                     :pageTotalFlag="false"
                     v-if="this.tableModel === 'ingOut'">
              <!-- 商品型号-->
              <DkTableColumn field="skuModel" :filter="false"></DkTableColumn>
              <!-- 商品名称-->
              <DkTableColumn field="skuName"></DkTableColumn>
              <!-- 唯一编码-->
              <DkTableColumn field="skuCode" :filter="false"></DkTableColumn>
              <!-- 品牌-->
              <DkTableColumn field="brandName" :filter="false"></DkTableColumn>
              <!--品牌简称-->
              <DkTableColumn field="shortName" :filter="false"></DkTableColumn>
              <!--非标号-->
              <DkTableColumn field="nonStandardCode"></DkTableColumn>
              <!--已出库数量-->
              <DkTableColumn field="intoQty" :title="$t('intoQty')" dataType="number"></DkTableColumn>
              <!--已出库金额-->
              <DkTableColumn field="intoAmt" :digits="2" :title="$t('intoAmt')" dataType="number"></DkTableColumn>
              <!-- 基本单位-->
              <DkTableColumn field="intoBox" :title="$t('box')" dataType="number" :digits="0"/>
              <!-- 包装单位-->
              <DkTableColumn field="intoPiece" :title="$t('piece')" dataType="number" :digits="0"/>
              <!-- 包装数量-->
              <DkTableColumn field="boxPiece" />
              <!--备注-->
              <DkTableColumn field="remarks"></DkTableColumn>
            </DkTable>
          </div>
          <!--  附件明细-->
          <div v-show="reviewResumeFlag">
            <!--                    <DkTable ref="reviewResumeFlag"-->
            <!--                             primaryKey="reviewResumeId"-->
            <!--                             :id="'table-'+$options.name"-->
            <!--                             name="table4" :data="reviewResumeData"-->
            <!--                             :height="this.tableHeight * (1-split)"-->
            <!--                             @pageChange="itemPageChange"-->
            <!--                             :select-flag="false">-->
            <!--                      <DkTableColumn field="remarks"></DkTableColumn>-->
            <!--                    </DkTable>-->
          </div>
        </div>
      </DkSplit>
    </div>

  </div>
</template>

<script>
import {indexMixin} from '@/mixins'

export default {
  name: "procure-return",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      //显示冲正
      isShowCancel:false,
      standardGoodsFlag: true,
      reviewResumeFlag: false,
      tableModel: 'waitOut',
      tabsModel: 'goodsDetail',
      tabsOptions: [
        {
          label: this.$t('waitOut'),
          name: 'waitOut'
        },
        {
          label: this.$t('ingOut'),
          name: 'ingOut'
        }
      ],
      detailTabsOptions: [
        {
          label: this.$t('goodsDetail'),
          name: 'goodsDetail'
        },
        {
          label: this.$t('filesDetail'),
          name: 'filesDetail'
        }
      ],
      tableData: [],
      goodsDetailData: [],//商品明细
      reviewResumeData: [],//附件明细
      intoId: null,//出库Id
      //明细明细分页
      detailPageInfo: {
        total: 0,
        pageSize: this.$config.pageSize,
        currentPage: 1
      },
      searchContent: [
        {
          itemCode: 'returnNo',
        },
        {
          itemCode: '供应商-文本',
          itemName: 'supplierName',
          valueFormat: {code: 'supId'},
          valueKind: 'S-SEARCH',
          magnifierType: self.$config.MagnifierType.supplier,
          otherCondition:{supplierType:this.$config.kindCode.supplierPur},
        },
        {
          itemCode: '商品型号-文本',
          itemName: 'skuModel',
          valueFormat: {code: 'skuModel'},
          valueKind: 'STR'
        },
        {
          itemCode: '商品名称-文本',
          itemName: 'skuName',
          valueFormat: {code: 'skuName'},
          valueKind: 'STR'
        },
        {
          itemCode: '商品编码-文本',
          itemName: 'skuCode',
          valueFormat: {code: 'skuCode'},
          valueKind: 'STR'
        },
        {
          itemCode: '查看冲正单据',
          itemName: '查看冲正单据',
          valueFormat: {code: 'showCancel', default: false},
          valueKind: 'SWITCH'
        },
      ],
    }
  },
  methods: {
    /**
     * @desc   : 点击Tab进行切换
     * @author : 付斌
     * @date   : 2024-02-19 11:33
     */
    tabsClick(val) {
      if (val === 'goodsDetail') {
        this.standardGoodsFlag = true
        this.reviewResumeFlag = false
      }
      if (val === 'filesDetail') {
        this.standardGoodsFlag = false
        this.reviewResumeFlag = true
      }
    },
    /**
     * @desc   : 查询
     * @author : 常皓宁
     * @date   : 2023/8/26 16:25
     */
    changeTab() {
      this.$nextTick(() => {
        //查询条件
        this.loading = true
        this.tableData = []
        this.pageInfo.currentPage = 1
        // 查询数据
        this.searchData()
      })

    },

    /**
    * @desc   : 查冲正单据
    * @author : 常皓宁
    * @date   : 2024/3/23 15:10
    */
    changeShowCancel({code, e}) {
      if ('showCancel' === code) {
        this.isShowCancel = e;
        // 查询数据
        this.getData();
      }
    },

    /**
    * @desc   : 作废
    * @author : 常皓宁
    * @date   : 2024/3/21 12:59
    */
    invalid() {
      let rows = this.$refs['table-select'].batchRows;
      if (rows && rows.length > 0) {
        //系统参数 是否自动入库
        if (this.$store.state.user.setting[this.$config.settingCode.IVT001] == 1) {
          //入库中不可以作废
          if (rows[0].intoStatus == this.$config.intoStatus.ruKuZhong) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_109')}));
            return
          }
          //已入库已付款不可以作废
          if (rows[0].intoStatus == this.$config.intoStatus.yiRuKu && rows[0].paymentFlag) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_112')}));
            return
          }
        }
        //无系统参数 手动办理
        else{
          //已入库的不能作废
          if (rows[0].intoStatus == this.$config.intoStatus.yiRuKu) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_105')}));
            return
          }
        }
        let params ={
          intoId:rows[0].intoId,
        }
        this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('invalid')}), title: this.$t('systemQuestion')},
          {
            ok: () => {
              this.loading = true
              this.excute(this.$service.purReturnService, this.$service.purReturnService.cancel, params, 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   : 2023/8/26 16:25
     */
    setSearchParams(params) {
      params.flgValid = true
      params.intoType = this.$config.intoTypeBySql.purchaseReturn
      if (this.isShowCancel) {
        params.isShowCancel = 1
      } else {
        params.isShowCancel = null
      }
      if (this.tableModel === 'waitOut') {
        params.intoStatusList = [this.$config.intoStatus.ruKuZhong]
      } else if (this.tableModel === 'ingOut') {
        params.intoStatusList = [this.$config.intoStatus.yiRuKu]
      }
      return params
    },

    /**
     * @desc   : 获取数据
     * @author : 付斌
     * @date   : 2024-02-19 11:34
     */
    getData(params) {
      // 查询数据
      this.excute(this.$service.purReturnService, this.$service.purReturnService.selectByCond, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableData = res.data.list
          if (this.tableData.length == 0) {
            this.goodsDetailData = []
          }
        }
      })
    },
    /**
     * @desc   : 行点击事件
     * @author : 常皓宁
     * @date   : 2024/2/28 15:01
     */
    currentChange({row}) {
      if (row != null) {
        this.intoId = row.intoId
        this.getDetail(1)
      }
    },

    /**
     * @desc   : 获取总单id对应明细
     * @author : 常皓宁
     * @date   : 2024/2/28 15:03
     */
    getDetail(currentPage) {
      if (currentPage) {
        this.detailPageInfo.currentPage = currentPage
      }
      let params = {
        intoId: this.intoId,
        pageSize: this.detailPageInfo.pageSize,
        currentPage: this.detailPageInfo.currentPage
      }
      // 查询数据
      return this.excute(this.$service.purReturnItemService, this.$service.purReturnItemService.selectByCond, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.goodsDetailData = res.data.list
        } else {
          this.goodsDetailData = []
        }
      })
    },

    /**
     * @desc   : 出库办理校验
     * @author : 常皓宁
     * @date   : 2024/3/18 13:27
     */
    openValidData(btnName, rows) {
      //出库办理
      if (rows && rows[0] && btnName === 'outHandle') {
        //已出库不允许进行出库办理
        if( this.tableModel=='ingOut' ){
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_103')}));
          return false;
        }
      }
      //编辑
      if (rows && rows[0] && btnName === 'edit') {
        //系统自动办理
        if (this.$store.state.user.setting[this.$config.settingCode.IVT001] == 1) {
          //入库中的不能编辑
          if (rows[0].intoStatus == this.$config.intoStatus.ruKuZhong) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_110')}));
            return false;
          }
        }
      }
      return true;
    },

    /**
     * @desc   : 出库撤回
     * @author : 常皓宁
     * @date   : 2024/3/18 14:16
     */
    outBack(){
      //当前tab页是否在出库中，是可以进行出库撤回
      if (this.tableModel== 'waitOut'){
        this.$Message.error(this.$t('W_007', {'param': this.$t('W_095')}))
      }else {
        if (this.$refs['table-select'].batchKeys.length === 1){
          let rows = this.$refs['table-select'].batchRows
          //系统自动办理不能撤回
          if (this.$store.state.user.setting[this.$config.settingCode.IVT001] == 1) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_108')}));
            return
          }
          //无系统参数
          else {
            //已付款单据不能撤回
            if (rows[0].paymentFlag) {
              this.$Message.warning(this.$t('W_007', {'param': this.$t('W_111')}));
              return
            }
          }
          let params = {
            intoId : this.$refs['table-select'].batchKeys[0],
            cpId : this.$store.state.user.cpId
          }
          this.$IBMessage({
              content: this.$t('Q_002', {'param': this.$v('outBack')}),
              title: this.$t('systemQuestion')
            },
            {
              // 弹出是否删除对话框-是
              ok: () => {
                //出库撤销
                this.excute(this.$service.purReturnService, this.$service.purReturnService.inboundReturnCancel, params).then(res => {
                  if (res.code === this.$config.SUCCESS_CODE) {
                    this.$Message.success(this.$t('I_001', {'param': this.$v('outBack')}))
                    this.searchData()
                  }else{
                    this.$Message.error(res.message)
                  }
                })
              },
            })
        }else {
          this.$Message.error(this.$t('W_013'))
        }
      }
    },
  },
  created() {
    this.primaryKey = 'intoId'  // 设置主键Id
    this.routeObjName = 'purReturn'  // 设置路由名称
  },
}
</script>
