<!-- @desc:库存盘点  @auth:宋扬  @time:2024/2/19 12:59 -->
<template>
  <div class="main-div">
    <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 v-show="flgEdit" ref="edit" name="edit"></BaseIndexButton>
        <!--关闭-->
        <BaseIndexButton ref="close" name="close"></BaseIndexButton>
        <!--导入-->
        <BaseIndexButton ref="import" name="import"></BaseIndexButton>
      </template>
      <template #right>
        <!--   新建     -->
        <BaseIndexButton ref="add" name="add"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>

    <!--  查询条件区域  -->
    <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" style="margin-left: 5px;">
        <DkTable slot="top"
                 :id="'table-'+$options.name"
                 ref="table-select"
                 :data="tableData"
                 showFooter
                 :height="this.tableHeight * split - 35"
                 primaryKey="checkId"
                 @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 :type="$libaray.checkButtonHasRight('order-edit')?'link':''" field="checkNo" @on-link="handleLink"/>
          <DkTableColumn field="checkNo"></DkTableColumn>
          <DkTableColumn field="checkDate"></DkTableColumn>
          <DkTableColumn field="checkStatusName"></DkTableColumn>
          <DkTableColumn field="checkOrg"></DkTableColumn>
          <DkTableColumn field="checkStaff"></DkTableColumn>
          <DkTableColumn field="flgLock" type="switch"></DkTableColumn>
          <DkTableColumn field="makeStaffName"></DkTableColumn>
          <DkTableColumn field="flgValid" type="switch"/>
          <DkTableColumn field="remarks"/>
    </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="goodsDetailData" showFooter
                 :height="this.tableHeight * split" primaryKey="itemId" name="table">
          <!-- 商品型号-->
          <DkTableColumn field="skuModel"/>
          <!-- 商品名称-->
          <DkTableColumn field="skuName"/>
          <!-- 唯一编码-->
          <DkTableColumn field="skuCode"/>
          <!-- 品牌-->
          <DkTableColumn field="shortName"/>
          <!-- 仓库-->
          <DkTableColumn field="whName" />
          <!-- 原库存量-->
          <DkTableColumn field="oinvQty" dataType="number"/>
          <!-- 库存量-->
          <DkTableColumn field="invQty" dataType="number"/>
          <!-- 盘点量-->
          <DkTableColumn field="checkQty" dataType="number"/>
          <!-- 差异数量-->
          <DkTableColumn field="balanceQty" dataType="number"/>
          <!-- 盘盈单价-->
          <DkTableColumn field="priceInto" dataType="number" :title="$t('checkPrice')"/>
          <!-- 盘盈金额-->
          <DkTableColumn field="intoingAmt" dataType="number" :title="$t('checkAmt')"/>
          <!-- 责任人-->
          <DkTableColumn field="dutyStaff"/>
          <!-- 盘亏原因-->
          <DkTableColumn field="lossReasonName"/>
          <!--盘点日期-->
          <DkTableColumn field="checkDate"/>
          <DkTableColumn field="flgValid" type="switch"/>
          <DkTableColumn field="remarks"/>
        </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)">
            <DkTableColumn field="name" :title="$t('fileName')" />
            <DkTableColumn field="path" :imageUrl="$config.imgUrl" type="image" :title="$t('yuLan')"/>
          </DkTable>
        </div>
      </div>
    </DkSplit>>
  </div>
</template>

<script>
import {indexMixin} from "@/mixins";
import Config from "@/config";
export default {
  name: 'ivt-check',
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      detailModel: 'goodsDetailData',
      detailOptions: [{label: self.$t('checkGoodsDetail'), name: 'checkGoodsDetail'}, //货物明细
        {label: self.$t('filesDetail'), name: 'filesDetail'}, //附件明细
      ],
      tableData: [],
      goodsDetailData: [],
      tableDataDetail_files: [],  // 用于存放附件
      // 是否显示编辑按钮
      flgEdit: true,
      visGoodsDetailFlag: true,
      visFilesDetailFlag: false,
      searchContent:
        [
          {
            itemCode: 'checkNo',
          },
          {
            itemCode: 'checkStatusList',
            itemName: self.$t('checkStatusName'),
            valueFormat: {
              code: 'checkStatusList',
              data: [],
              valueKey: 'kindCode',
              labelKey: 'kindName',
            },
            valueKind: 'M-CHOICE'
          },
          {
            itemCode: 'checkDate',
            valueFormat: {
              code: 'checkDate',
              default: [new Date().addMonths(-1).format('yyyy-MM-dd'), new Date().format('yyyy-MM-dd')]
            },
            valueKind: 'DATE_RANGE'
          },
          // 业务部门
          {
            itemCode: 'checkOrg',
            valueFormat: {
              code: 'orgIdList',
            },
            magnifierType: Config.MagnifierType.org,
            valueKind: 'M-SEARCH'
          },
          // 业务员
          {
            itemCode: 'checkStaff',
            valueFormat: {
              code: 'staffIdList',
            },
            magnifierType: Config.MagnifierType.staff,
            valueKind: 'M-SEARCH'
          },
          self.$config.flgValidSearch
        ],
    }
  },
  methods: {

    /**
     * @desc   : 加载数据
     * @author : 宋扬
     * @date   : 2024/3/25 11:16
     */
    initData() {
      // 盘点状态
      this.getCheckStatus();
      //查询业务部门
      this.getOrg();
      //查询业务员
      this.getStaff();
    },

    /**
     * @desc   : 获取盘点状态
     * @author : 宋扬
     * @date   : 2024/3/14 16:33
     */
    getCheckStatus() {
      // 读取系统字典
      let dataKindList =this.$store.state.user.dataKindList
      let data = dataKindList.filter(it=>it.kindType == this.$config.kindType.checkStatus)
      let list = this.searchContent.filter(it =>  it.valueFormat && it.valueFormat.code == 'checkStatusList')
      if (list.length > 0) {
        list[0].valueFormat.data = data
      }
    },

    /**
     * @desc   : 部门条件
     * @author : 宋扬
     * @date   : 2024/3/14 13:43
     */
    getOrg() {
      let params = {
        cpId: this.$store.state.user.cpId,
        flgValid: true
      }
      this.excute(this.$service.commonService, this.$service.commonService.getOrg, params).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/3/14 13:43
     */
    getStaff() {
      let params = {
        cpId: this.$store.state.user.cpId,
        flgValid: true
      }
      this.excute(this.$service.commonService, this.$service.commonService.getStaff, params).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/3/13 10:42
     */
    getData(params) {
      this.tableData = [];
      this.goodsDetailData = [];
      return this.excute(this.$service.checkService, this.$service.checkService.selectByCond, params)
    },

    /**
     * @desc   : 切换行查询明细
     * @author : 宋扬
     * @date   : 2024/3/13 15:51
     */
    currentChange({row}) {
      this.loading = true
      // 赋值freezeId
      let params = {}
      if (row != null) {
        params = {
          checkId:row.checkId,
        }
        if(row.checkStatus === this.$config.checkStatus.done) {
          this.flgEdit = false;
        }
        else{
          this.flgEdit = true;
        }
      }
      // 查询盘点明细
      this.excute(this.$service.checkItemService, this.$service.checkItemService.selectByCond, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.goodsDetailData = res.data.list;
        }
      })
      //附件
      this.tableDataDetail_files = row.annexPaths
      this.loading = false
    },

    /**
     * @desc   : 点击Tab进行切换
     * @author : 宋扬
     * @date   : 2024/4/3 16:24
     */
    detailTabsClick(val) {
      if (val === 'checkGoodsDetail') {
        this.visGoodsDetailFlag = true
        this.visFilesDetailFlag = false
      } else if (val === 'filesDetail') {
        this.visGoodsDetailFlag = false
        this.visFilesDetailFlag = true
      }
    },

    /**
     * @desc   : desc
     * @author : 宋扬
     * @date   : 2024/4/18 16:36
     */
    openValidData(btnName,row){
      if(btnName === 'edit' && row && row.checkStatus === this.$config.checkStatus.done){
        this.$Message.error(this.$t('W_033'))
        return false;
      }
      return  true;
    },

    /**
     * @desc   : 打开附件
     * @author : 宋扬
     * @date   : 2024/4/3 16:25
     */
    openFile(row) {
      window.open(this.$config.imgUrl + row.url)
    },

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

