<!-- @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="intoEnter" name="intoEnter"></BaseIndexButton>
        <!--采购退货-->
        <BaseIndexButton ref="purchaseReturn" name="purchaseReturn"></BaseIndexButton>
        <!--作废-->
        <BaseIndexButton ref="invalid" name="invalid"></BaseIndexButton>
        <!--采购跟踪-->
        <BaseIndexButton ref="purTrack" name="purTrack"></BaseIndexButton>
        <!--打印-->
        <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">
          <DkTabs v-model="tabsModel" :options="tabsOptions" @on-click="tabsClick"></DkTabs>
          <DkTable slot="top"
                   :id="'table-'+$options.name"
                   ref="table-select"
                   name="table"
                   :data="tableData"
                   primaryKey="purId"
                   @current-change="currentChange($event)"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   :choose-flag="false"
                   :current-page="pageInfo.currentPage"
                   :height="this.tableHeight * split - 66"
                    showFooter>
            <!--采购单号-->
            <DkTableColumn :type="$libaray.checkButtonHasRight('pur-order-edit')?'link':''" field="purNo"
                           @on-link="handleLink"></DkTableColumn>
            <!--部门-->
            <DkTableColumn field="orgName"></DkTableColumn>
            <!--员工-->
            <DkTableColumn field="staffName"></DkTableColumn>
            <!--供应商-->
            <DkTableColumn field="supplierName" :type="$libaray.checkButtonHasRight('supplier')?'link':''"
                           @on-link="openSupEdit"></DkTableColumn>
            <!--联系人-->
            <DkTableColumn field="contactName"></DkTableColumn>
            <!--联系电话-->
            <DkTableColumn field="contactPhone"></DkTableColumn>
            <!--取货日期-->
            <DkTableColumn field="pickupDate"></DkTableColumn>
            <!--商品总数量-->
            <DkTableColumn field="sumQuantity" sum data-type="number"></DkTableColumn>
            <!--标价合计-->
            <DkTableColumn field="sumStandard" :digits="2" sum data-type="number"></DkTableColumn>
            <!--合计金额-->
            <DkTableColumn field="sumAmount" :digits="2" sum data-type="number"></DkTableColumn>
            <!--采购折扣-->
            <DkTableColumn field="purDiscount" data-type="number"></DkTableColumn>
            <!--订单状态-->
            <DkTableColumn field="purStatusName"></DkTableColumn>
            <!--入库状态-->
            <DkTableColumn field="intoStatusName" :color-cards="['待入库','入库中','已入库']"></DkTableColumn>
            <!--入库中数量-->
            <DkTableColumn field="intoingQty" data-type="number"></DkTableColumn>
            <!--入库中金额-->
            <DkTableColumn field="intoingAmt" :digits="2" data-type="number"></DkTableColumn>
            <!--已入库数量-->
            <DkTableColumn field="intoQty" data-type="number"></DkTableColumn>
            <!--已入库金额-->
            <DkTableColumn field="intoAmt" :digits="2" data-type="number"></DkTableColumn>
            <!--已入库退货数量-->
            <DkTableColumn field="returnQty" :title="$t('returnIntoQty')" data-type="number"></DkTableColumn>
            <!--已入库退货金额-->
            <DkTableColumn field="returnAmt" :title="$t('returnIntoAmt')" :digits="2" data-type="number"></DkTableColumn>
            <!--应付金额-->
            <DkTableColumn field="amtPayable" :digits="2" data-type="number"></DkTableColumn>
            <!--还款金额-->
            <DkTableColumn field="amtHandle" :digits="2" data-type="number"></DkTableColumn>
            <!--剩余应付-->
            <DkTableColumn field="amtResidue" :title="$t('amteMeet')" :digits="2" data-type="number"></DkTableColumn>
            <!--制单人-->
            <DkTableColumn field="makeStaffName"></DkTableColumn>
            <!--制单时间-->
            <DkTableColumn field="makeTime"></DkTableColumn>
            <!--备注-->
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>
        </div>
        <div slot="bottom">
          <DkTabs v-model="detailModel" :options="detailOptions" @on-click="detailTabsClick"></DkTabs>
          <div v-show="standardGoodsFlag">
            <!--订单明细-->
            <DkTable :pageFlag="false"
                     :pageTotalFlag="false"
                     primaryKey="itemId"
                     :id="'table-'+$options.name"
                     :show-footer="true"
                     :choose-flag="false"
                     :export-url="$libaray.checkButtonHasRight('sale-export')?'/order-server/sale/order_item/export/'+orderId:''"
                     ref="detail-select"
                     :data="tableDataDetail_goodsDetail"
                     :height="this.tableHeight * (1-split)-32">
              <DkTableColumn
                v-for="(item, index) in tableDataTitle_goodsDetail"
                :key="detailModel+index"
                :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="reviewResumeFlag">
            <DkTable :pageFlag="false" :pageTotalFlag="false" :id="'table-'+$options.name"
                     name="table" ref="file-select" :data="tableDataDetail_files" :height="this.tableHeight * (1-split)-32">
              <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.purOrder"
                      :postName="'purchase_order'"
                      :id="$refs?.['table-select']?.batchRows?.[0]?.purId||null"/>

        </div>
      </DkSplit>
    </div>

  </div>
</template>

<script>

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

export default {
  name: 'pur-order',
  mixins: [indexMixin],
  components: {PrintModal},
  data() {
    let self = this
    return {
      purId: null,//总单ID
      standardGoodsFlag: true,
      reviewResumeFlag: false,
      totalTab: null,//存放总单tab切换到那个标签
      // 模式切换值
      tabsModel: 'orderAll',
      tabsOptions: [{label: self.$t('orderAll'), name: 'orderAll'}, //全部订单
        {label: self.$t('purchaseOrder'), name: 'purchaseOrder'}, //采购暂存
        {label: self.$t('purchaseReal'), name: 'purchaseReal'},//采购成交
      ],
      detailModel: 'goodsDetail',
      detailOptions: [{label: self.$t('goodsDetail'), name: 'goodsDetail'}, //货物明细
        {label: self.$t('filesDetail'), name: 'filesDetail'}, //附件明细
      ],
      tableData: [],
      tableDataDetail_goodsDetail: [],  // 用于存放订单明细数据
      tableDataDetail_files: [],  // 用于存放订单附件
      tableDataTitle_goodsDetail: [
        {field: 'skuName'},
        {field: 'skuCode'},
        //采购数量
        {
          field: 'itemQty',
          title: self.$t('purchaseQuantity'),
          type: 'number',
          dataType: 'number',
          digits: 0,
          sum: true
        },
        //采购价
        {
          field: 'pricePur',
          title: self.$t('pricePur'),
          type: 'number',
          digits: 2,
          dataType: 'number',
          sum: true
        },
        //采购金额
        {
          field: 'itemAmt',
          title: self.$t('itemPurAmt'),
          type: 'number',
          dataType: 'number',
          digits: 2,
          sum: true
        },
        //折扣
        {field: 'priceDiscount', title: self.$t('priceDiscount')},
        //非标号
        {field: 'nonStdCode', title: self.$t('nonStandardCode')},
        //包装单位
        {field: 'box',title: self.$t('box')},
        //基本单位
        {field: 'piece',title: self.$t('piece')},
        //包装
        {field: 'packBox',title: self.$t('packBox')},
        //箱片规格
        {field: 'specsName',title: self.$t('boxPiece')},
        //入库状态
        {
          field: 'intoStatusName',
          type: 'text',
        },
        //入库中数量
        {
          field: 'intoingQty',
          title: self.$t('intoingQty'),
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true
        },
        //入库中金额
        {
          field: 'intoingAmt',
          title: self.$t('intoingAmt'),
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true
        },
        //已入库数量
        {
          field: 'intoQty',
          title: self.$t('intoQty'),
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true
        },
        //已入库金额
        {
          field: 'intoAmt',
          title: self.$t('intoAmt'),
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true
        },
        //已入库退货数量
        {
          field: 'returnQty',
          title: self.$t('returnIntoQty'),
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true
        },
        //已入库退货金额
        {
          field: 'returnAmt',
          title: self.$t('returnIntoAmt'),
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true
        },
        //备注
        {field: 'remarks'},
      ],
      //明细明细分页
      detailPageInfo: {
        total: 0,
        pageSize: this.$config.pageSize,
        currentPage: 1
      },
      searchContent: [
        {
          itemCode: '采购单号-文本',
          itemName: 'purNo',
          valueFormat: {code: 'purNo'},
          valueKind: 'STR'
        },
        {
          itemCode: '供应商-文本',
          itemName: 'supplierName',
          valueFormat: {code: 'supId'},
          valueKind: 'S-SEARCH',
          magnifierType: self.$config.MagnifierType.supplier,
        },
        {
          itemCode: '收货人-文本',
          itemName: 'contactName',
          valueFormat: {code: 'contactName'},
          valueKind: 'STR'
        },
        {
          itemCode: '收货电话-文本',
          itemName: 'telephone',
          valueFormat: {code: 'contactPhone'},
          valueKind: 'STR'
        },
        {
          itemCode: '入库状态-下拉',
          itemName: 'intoStatusName',
          valueFormat: {
            code: 'intoStatusList',
            data: [],
            valueKey: 'id',
            labelKey: 'kindName',
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '商品名称-文本',
          itemName: 'skuName',
          valueFormat: {code: 'skuName'},
          valueKind: 'STR'
        },
        {
          itemCode: '采购日期-时间',
          itemName: 'pickupDate',
          valueFormat: {
            code: 'pickupDate',
            default: [new Date().addMonths(-1).format('yyyy-MM-dd'), new Date().format('yyyy-MM-dd')]
          },
          valueKind: 'DATE_RANGE'
        },
      ],
    }
  },
  methods: {
    /**
    * @desc   : 进入供应商页面
    * @author : 常皓宁
    * @date   : 2024/5/21 10:33
    */
    openSupEdit(row) {
      this.$router.push('/mst/supplier/index/' + row['customerId'])
    },
    /**
     * @desc   : 选择模板、打印机
     * @author : 洪旭东
     * @date   : 2023-04-10 13:45
     */
    print() {
      this.$refs.printModal.showChooseTemplate = true
    },

    /**
     * @desc   : 查询数据
     * @author : 付斌
     * @date   : 2024-02-19 9:59
     */
    getData(params) {
      // 查询数据
      let excute = this.excute(this.$service.purOrderService, this.$service.purOrderService.selectByCond, params)
      excute.then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableDataDetail_goodsDetail = []
        }
      })
      return excute;
    },

    /**
    * @desc   : 订单跳转校验
    * @author : 常皓宁
    * @date   : 2024/4/19 13:06
    */
    openValidData(btnName, rows) {
      //采购退货
      if (rows && rows[0] && btnName === 'purchaseReturn') {
        if (rows.intoStatus == this.$config.intoStatus.daiRuKu) {
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_101')}));
          return false
        }
      }
      //订单转入库
      else if (rows && rows[0] && btnName === 'intoEnter') {
        if (rows.intoStatus == this.$config.intoStatus.yiRuKu) {
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_107')}));
          return false
        }
      }
      return true;
    },

    /**
     * @desc   : 采购退货
     * @author : 常皓宁
     * @date   : 2024/3/21 9:39
     */
    purchaseReturn() {
      if (this.$refs['table-select'].batchKeys.length == 1) {
        // 判断状态是否可以进行采购退货
        let row = this.$refs['table-select'].batchRows[0];
        if (row.intoStatus == this.$config.intoStatus.daiRuKu) {
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_101')}));
          return
        }
        this.$router.push('/pur/pur-return/add?fromId=' + this.$refs['table-select'].batchKeys[0])
      }
    },

    /**
    * @desc   : 订单转入库
    * @author : 常皓宁
    * @date   : 2024/3/27 13:32
    */
    intoEnter() {
      if (this.$refs['table-select'].batchKeys.length == 1) {
        // 判断状态是否可以进行采购入库
        let row = this.$refs['table-select'].batchRows[0];
        if (row.intoStatus == this.$config.intoStatus.yiRuKu) {
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_107')}));
          return
        }
        this.$router.push('/pur/pur-enter/add?purId=' + this.$refs['table-select'].batchKeys[0])
      }
    },

    /**
     * @desc   : 打开附件
     * @author : 付斌
     * @date   : 2024-02-19 10:00
     */
    openFile(row) {
      window.open(this.$config.imgUrl + row.url)
    },

    /**
     * @desc   : 作废
     * @author : 常皓宁
     * @date   : 2024-03-05 8:56
     */
    invalid(rows) {
      if (rows && rows.length > 0) {
        if (rows[0].intoStatus != this.$config.intoStatus.daiRuKu) {
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_106')}));
          return
        }
        this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('invalid')}), title: this.$t('systemQuestion')},
          {
            ok: () => {
              this.loading = true
              this.excuteNoParam(this.$service.purOrder, this.$service.purOrder.invalid, [rows[0].purId], false).then(res => {
                if (res.code === this.$config.SUCCESS_CODE) {
                  this.$Message.success(this.$t('I_001', {'param': this.$t('invalid')}))
                  this.searchData()
                } else {
                  this.$Message.warning(res.message)
                }
                this.loading = false
              })
            },
          })
      }
    },

    /**
     * @desc   : 行点击事件
     * @author : 常皓宁
     * @date   : 2024/2/28 15:01
     */
    currentChange({row}) {
      if (row != null) {
        this.purId = row.purId
        this.getDetail(1)
        //订单附件
        console.log('row',row)
        this.tableDataDetail_files = row.annexPaths
      }
    },

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

    /***************************列表及其他事件************************************/
    /**
     * @desc   : 总单tab切换
     * @author : 付斌
     * @date   : 2024-02-19 10:02
     */
    tabsClick(val) {
      //如果切换的tab存在就去根据条件查询
      if (val) {
        this.totalTab = val
      }
    },
    /**
     * @desc   : 点击Tab进行切换
     * @author : 付斌
     * @date   : 2024-02-19 11:33
     */
    detailTabsClick(val) {
      if (val === 'goodsDetail') {
        this.standardGoodsFlag = true
        this.reviewResumeFlag = false
      } else if (val === 'filesDetail') {
        this.standardGoodsFlag = false
        this.reviewResumeFlag = true
      }
    },

    /**
    * @desc   : 获取入库状态查询list
    * @author : 常皓宁
    * @date   : 2024/4/24 15:27
    */
    getIntoTypeList(){
      // 读取系统字典
      let dataKindList =this.$store.state.user.dataKindList
      let data = dataKindList.filter(it=>it.kindType == this.$config.kindType.intoStatus)
      let list = this.searchContent.filter(it =>  it.valueFormat && it.valueFormat.code == 'intoStatusList')
      if (list.length > 0) {
        list[0].valueFormat.data = data
      }
    },
    /***************************设定参数函数************************************/
    /***************************created调用数据接口函数************************************/

  },
  created() {
    this.primaryKey = 'purId'  // 设置主键Id
    this.routeObjName = 'purOrder'  // 设置路由名称
    //获取入库状态查询list
    this.getIntoTypeList()
  },
}
</script>

