<!-- @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="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"
                    :search-content="searchContent"
                    @on-switch-change="changeShowItem"></SearchCond>
      </div>
      <!--  表格部分  -->
      <DkSplit mode="vertical" v-model="split" :height="tableHeight">
        <div slot="top">
          <!-- 待入库/已入库Tab页-->
          <DkTabs v-model="tableModel" :options="tabsOptions" @on-click="changeTab()"></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 === 'waitEntry'"
                   @current-change="currentChange($event)"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :page-size="pageInfo.pageSize"
                   :page-size-opts="pageSizeOpts">
            <!-- 采购入库单号-->
            <DkTableColumn field="intoNo" :type="$libaray.checkButtonHasRight('pur-enter-edit')?'link':''"
                           @on-link="handleLink"></DkTableColumn>
            <!-- 来源单号-->
            <DkTableColumn field="fromNo"></DkTableColumn>
            <!--部门-->
            <DkTableColumn field="orgName"></DkTableColumn>
            <!--员工-->
            <DkTableColumn field="staffName"></DkTableColumn>
            <!--供应商-->
            <DkTableColumn field="supplierName"></DkTableColumn>
            <!--入库状态-->
            <DkTableColumn field="intoStatusName" :color-cards="['入库中','已入库']"></DkTableColumn>
            <!-- 入库中数量-->
            <DkTableColumn field="intoingQty" :digits="0" sum :title="$t('intoingQty')" data-type="number"></DkTableColumn>
            <!-- 入库中金额-->
            <DkTableColumn field="intoingAmt" :digits="0" sum :title="$t('intoingAmt')" data-type="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 === 'ingEntry'"
                   @current-change="currentChange($event)"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :page-size="pageInfo.pageSize"
                   :page-size-opts="pageSizeOpts">
            <!-- 采购入库单号-->
            <DkTableColumn field="intoNo" :type="$libaray.checkButtonHasRight('pur-enter-edit')?'link':''"
                           @on-link="handleLink"></DkTableColumn>
            <!-- 来源单号-->
            <DkTableColumn field="fromNo"></DkTableColumn>
            <!--部门-->
            <DkTableColumn field="orgName"></DkTableColumn>
            <!--员工-->
            <DkTableColumn field="staffName"></DkTableColumn>
            <!--供应商-->
            <DkTableColumn field="supplierName"></DkTableColumn>
            <!--入库状态-->
            <DkTableColumn field="intoStatusName" :color-cards="['入库中','已入库']"></DkTableColumn>
            <!--已入库数量-->
            <DkTableColumn field="intoQty" :digits="0" sum :title="$t('intoQty')" data-type="number"></DkTableColumn>
            <!--已入库金额-->
            <DkTableColumn field="intoAmt" :digits="0" sum :title="$t('intoAmt')" data-type="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 === 'waitEntry'">
              <!-- 来源单号-->
              <DkTableColumn field="purNo" :title="$t('fromNo')"></DkTableColumn>
              <!-- 商品型号-->
              <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')"></DkTableColumn>
              <!--入库中金额-->
              <DkTableColumn field="intoingAmt" :title="$t('intoingAmt')"></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 === 'ingEntry'">
              <!-- 来源单号-->
              <DkTableColumn field="purNo" :title="$t('fromNo')"></DkTableColumn>
              <!-- 商品型号-->
              <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')"></DkTableColumn>
              <!--已入库金额-->
              <DkTableColumn field="intoAmt" :title="$t('intoAmt')"></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 :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>
        </div>
      </DkSplit>
    </div>

  </div>
</template>

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

export default {
  name: "pur-enter",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      standardGoodsFlag: true,
      reviewResumeFlag: false,
      tableModel: 'waitEntry',
      tabsModel: 'goodsDetail',
      tableDataDetail_files: [],  // 用于存放订单附件
      tabsOptions: [
        {
          label: this.$t('waitEntry'),
          name: 'waitEntry'
        },
        {
          label: this.$t('ingEntry'),
          name: 'ingEntry'
        }
      ],
      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: 'intoNo',
        },
        {
          itemCode: 'fromNo',
        },
        {
          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: 'showItem', 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-02-19 11:34
     */
    changeShowItem({code, e}) {

    },

    /**
     * @desc   : 查询参数设定
     * @author : 常皓宁
     * @date   : 2023/8/26 16:25
     */
    setSearchParams(params) {
      console.log('123123')
      params.flgValid = true
      params.intoType = this.$config.intoTypeBySql.purchase
      if (this.tableModel === 'waitEntry') {
        params.intoStatusList = [this.$config.intoStatus.ruKuZhong]
      } else if (this.tableModel === 'ingEntry') {
        params.intoStatusList = [this.$config.intoStatus.yiRuKu]
      }
      return params
    },

    /**
     * @desc   : 获取数据
     * @author : 付斌
     * @date   : 2024-02-19 11:34
     */
    getData(params) {
      // 查询数据
      this.excute(this.$service.purEnterService, this.$service.purEnterService.selectByCond, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableData = res.data.list
          this.pageInfo.total = res.data.total
          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)
        if (row.annexPaths) {
          row.annexPaths = row.annexPaths.map(m => {
            return {
              path: m.url,
              name: m.name,
              type: 'img'
            }
          })
        }
        //订单附件
        this.tableDataDetail_files = row.annexPaths
      }
    },

    /**
     * @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.purEnterItemService, this.$service.purEnterItemService.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 === 'entryHandle') {
        //已入库不允许进行入库办理
        if (this.tableModel == 'ingEntry') {
          this.$Message.warning(this.$t('W_007', {'param': this.$t('W_096')}));
          return false;
        }
      }
      //编辑
      else 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
     */
    entryBack() {
      //当前tab页是否在入库中，是可以进行入库撤回
      if (this.tableModel == 'waitEntry') {
        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],
            fromId: this.$refs['table-select'].batchRows[0].fromId,
            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.purEnterService, this.$service.purEnterService.purchaseInboundCancel, params).then(res => {
                  if (res.code === this.$config.SUCCESS_CODE) {
                    this.$Message.success(this.$t('I_001', {'param': this.$v('entryBack')}))
                    this.searchData()
                  } else {
                    this.$Message.error(res.message)
                  }
                })
              },
            })
        } else {
          this.$Message.error(this.$t('W_013'))
        }
      }
    },

    /**
     * @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.purEnterService, this.$service.purEnterService.purchaseInboundRepeal, 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
              })
            },
          })
      }
    },

  },
  created() {
    this.primaryKey = 'intoId'  // 设置主键Id
    this.routeObjName = 'purEnter'  // 设置路由名称
  },
}
</script>
