<!-- @desc:入库成本核对  @auth:常皓宁  @time:2024年4月17日09:01:32 -->
<template>
  <div class="main-div" ref="mainDiv">
    <loading :loading="loading" v-if="!modalVisible"></loading>
    <!--按钮区-->
    <BaseIndexButtonGroup ref="BaseIndexButtonGroup" id="BaseIndexButtonGroup">
      <template #left>
        <!--    查询    -->
        <BaseIndexButton ref="search" name="search"></BaseIndexButton>
        <!--    清空条件    -->
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
        <!--    核对    -->
        <BaseIndexButton ref="costCheck" name="costCheck" :isMustChooseFlag="false"></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"></SearchCond>
      </div>

      <!--  表格部分  -->
      <DkTable
        ref="checkTable"
        :data="formData"
        :height="tableHeight"
        :page-flag="false"
        :page-total-flag="false"
        :show-setting-flag="false"
        :freeze="false">
        <DkTableColumn field="intoNo"></DkTableColumn>
        <vxe-column show-overflow="ellipsis" field="costPrice" width="100" height="100" min-width="80"
                    class-name="setting-value-class"
                    :title="$t('costPrice')">
          <template #default="{ row ,rowIndex}">
            <!--数字类型-->
            <vxe-input size="mini"
                       type="integer"
                       @blur="blursSettingValue(row,rowIndex,'costPrice')"
                       v-model="row.costPrice"></vxe-input>
          </template>
        </vxe-column>
        <vxe-column show-overflow="ellipsis" field="costAmt" width="100" height="100" min-width="80"
                    class-name="setting-value-class"
                    :title="$t('costAmt')">
          <template #default="{ row,rowIndex }">
            <!--数字类型-->
            <vxe-input size="mini"
                       type="integer"
                       @blur="blursSettingValue(row,rowIndex,'costAmt')"
                       v-model="row.costAmt"></vxe-input>
          </template>
        </vxe-column>
        <DkTableColumn field="skuModel" width="220"></DkTableColumn>
        <DkTableColumn field="skuName" width="auto"></DkTableColumn>
        <DkTableColumn field="supName" width="auto"></DkTableColumn>
        <DkTableColumn field="intoTypeName" width="auto"></DkTableColumn>
        <DkTableColumn field="intoQty" width="auto"></DkTableColumn>
        <DkTableColumn field="priceInto" width="auto"></DkTableColumn>
        <DkTableColumn field="intoAmt" width="auto"></DkTableColumn>
        <DkTableColumn field="flgCostCheckName" width="auto"></DkTableColumn>

      </DkTable>
    </div>

  </div>
</template>

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

export default {
  name: "cost-check",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      // 查询条件
      searchContent: [
        {
          itemCode: 'intoNo',
        },
        {
          itemCode: '商品名称-文本',
          itemName: 'skuName',
          valueFormat: {code: 'skuName'},
          valueKind: 'STR'
        },
        {
          itemCode: '商品型号-文本',
          itemName: 'skuModel',
          valueFormat: {code: 'skuModel'},
          valueKind: 'STR'
        },
        {
          itemCode: '供应商-文本',
          itemName: 'supplierName',
          valueFormat: {code: 'supId'},
          valueKind: 'S-SEARCH',
          magnifierType: self.$config.MagnifierType.supplier,
        },
        {
          itemCode: '入库类型-下拉',
          itemName: 'intoTypeName',
          valueFormat: {
            code: 'intoTypeName',
            data: [
              {intoTypeName: '采购入库'},
              {intoTypeName: '采退入库'},
              {intoTypeName: '其他入库'},
              {intoTypeName: '调整入库'},
              {intoTypeName: '盘盈入库'},
              {intoTypeName: '调整入库'},
            ],
            valueKey: 'intoTypeName',
            labelKey: 'intoTypeName',
          },
          valueKind: 'S-CHOICE'
        },
        {
          itemCode: '入库日期-时间',
          itemName: 'intoDate',
          valueFormat: {
            code: 'intoDate',
            // default:[new Date().format('yyyy-MM')]
          },
          valueKind: 'MONTH'
        },
        {
          itemCode: '仅查询未核对',
          itemName: '仅查询未核对',
          valueFormat: {code: 'viewNoCostDocuments', default: false},
          valueKind: 'SWITCH'
        },
      ],
    }
  },
  methods: {
    /**
    * @desc   : 文焦点离开事件
    * @author : 常皓宁
    * @date   : 2024/4/20 13:36
    */
    blursSettingValue(row ,rowIndex,field) {
      if(!this.formData || !this.formData[rowIndex]){
        return;
      }
      if ((row.oldCostPrice != row.costPrice) || (row.oldCostAmt != row.costAmt)) {
        // 价格
        if(field == 'costPrice'){
          this.formData[rowIndex].costAmt = row.costPrice * row.intoQty
        } else if(field == 'costAmt'){
          this.formData[rowIndex].costPrice = row.costAmt / row.intoQty
        }
        this.$nextTick(()=>{
          this.$refs['checkTable'].setCheckboxRow(row);
        })
      }
    },
    /**
    * @desc   : 核对
    * @author : 常皓宁
    * @date   : 2024/4/17 13:25
    */
    costCheck(){
      let changeTable = this.$refs['checkTable'].getCheckRows();
      console.log('changeTable',changeTable)
      this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('入库成本核对')}), title: this.$t('systemQuestion')},
        {
          ok: () => {
            this.loading = true
            let params = changeTable
            this.excute(this.$service.costCheckService, this.$service.costCheckService.costCheckOk, params, false).then(res => {
              if (res.code === this.$config.SUCCESS_CODE) {
                this.$Message.success(this.$t('I_001', {'param': this.$t('入库成本核对')}))
                this.searchData()
              } else {
                this.$Message.warning(res.message)
              }
              this.loading = false
            })
          }
        })
    },
    /**
    * @desc   : 查询数据
    * @author : 常皓宁
    * @date   : 2024/4/17 9:19
    */
    getData(params) {
      delete params.currentPage
      delete params.pageSize
      if(params.intoDate){
        params.intoDateStart = new Date(params.intoDate).getFirstDateStr()
        params.intoDateEnd = new Date (new Date(params.intoDate).getLastDateStr()).format('yyyy-MM-dd')
      }
      this.excute(this.$service.costCheckService, this.$service.costCheckService.selectCostCheck, params).then(res =>{
        if (res.code === this.$config.SUCCESS_CODE) {
          let data = []
          res.data.list.forEach(it =>{
            it.oldCostPrice = it.costPrice
            it.oldCostAmt = it.costAmt
            data.push(it)
          })
          this.formData = data
        }else {
          this.$Message.warning(res.message)
        }
        this.loading = false;
      })
    },
  },
  created() {
    this.primaryKey = 'itemId'  // 设置主键Id
    this.routeObjName = 'costCheck'  // 设置路由名称
  }
}
</script>


