<!-- @desc:新建库存盘点 @auth:songy @time : 2024/2/20 9:38 -->
<template>
  <div class="main-div" style="position: relative;">
    <DkCollapse @on-change="changeCollapse" ref="collapse">
      <DkPanel prop="essentialInformation">
        <!--  单据信息  -->
        <DkForm slot="content" ref="formInline" v-model="formData"
                style="width:100%" :label-max-words="5">
          <!--盘点单号-->
          <DkFormItem prop="checkNo" :label="$t('procureOrderNo')">
            <InputPop ref="checkNo" v-model="formData.orderNo" :readonly="true"/>
          </DkFormItem>
          <!--账务时间-->
          <DkFormItem :label="$t('expectedDeliverDate')"
                      prop="entryTime" :required="false">
            <DatePickerPop v-model="formData.entryTime"
                           :placeholder="$t('inputWords',{'search-name':$t('entryTime')})"
                           :readonly="checkColumnIsEdit('entryTime')"
                           type="date" :short-cut-flag="true"/>
          </DkFormItem>
          <!--业务部门-->
          <DkFormItem :required="true" prop="orgId" :data-type="$config.dataType.number" :label="$t('organizationLevel')">
            <SelectMagnifier v-model="formData.orgId" :display-text="formData.levelName" :disabled="type === 'edit-staff'"
                             :type="this.$config.MagnifierType.organization"
                             :multiple="false"
                             title="业务部门"
                             @ok="chooseOrg"></SelectMagnifier>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks">
            <InputPop v-model="formData.remarks" textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--商品信息-->
      <DkPanel prop="goodsInformation">
        <div id="search-cond-div-goods-info" ref="search-cond-div-goods-info" slot="content">
          <EditTreeTable ref="goodsTable" :data="formData.goodsList" showFooter exchange-field="replaceableSku"
                         major-field="itemId"
                         combined-field="promotionId"
                         :new-row-flag="!(!this.formData.orgId)"
                         :readonly="checkColumnIsEdit('goodsTable')"
                         :amount-name="this.type == this.$config.formMode.add?'itemAmount':'tItemAmount'"
                         :columns="(this.type == this.$config.formMode.add
                                    || this.type == this.$config.formMode.approval)?(visUserSensitive? goodsColumns : goodsColumns.filter(it => !it.flgUserSensitive))
                                    :(visUserSensitive ?goodsColumnsEdit : goodsColumnsEdit.filter(it => !it.flgUserSensitive))"
                         :orgId="this.formData.orgId"
                         :customerId="this.formData.customerId"
                         :saleChannelCode="this.formData.salesChannelCode"
                         :defWhId="this.formData.defWhId"
                         :supplierWhName="this.formData.supplierWhName"
                         :entryWhId="this.formData.entryWhId"
                         :purchaseWhFlag="true"
                         :flgHidePrice="!visUserSensitive"
                         controlId="skuId"
                         @on-choose="getTotals"
                         @changeValue="changeValue"
                         :height="this.tableHeight+300"
          ></EditTreeTable>
          <div>
            <DkForm slot="content" ref="goodsFootFormData" v-model="goodsFootFormData" :labelMaxWords="5"
                    name="dk-form"
                    class="form-content-class">
              <DkRow>
                <!--货物金额-->
                <Col class="Col" span="6" v-if="visUserSensitive">
                  <DkFormItem prop="sumGoodsAmount">
                    <InputNumberPop ref="sumGoodsAmount" v-model="goodsFootFormData.sumGoodsAmount" :clearable="false"
                                    :digits="2" :readonly="true"/>
                  </DkFormItem>
                </Col>
                <!--总重量-->
                <Col class="Col" span="6">
                  <DkFormItem prop="sumWeight">
                    <InputNumberPop ref="sumWeight" v-model="goodsFootFormData.sumWeight" :digits="2"
                                    :readonly="true" unit="KG"/>
                  </DkFormItem>
                </Col>
                <!--总体积-->
                <Col class="Col" span="6">
                  <DkFormItem prop="sumVolume">
                    <InputNumberPop ref="sumVolume" v-model="goodsFootFormData.sumVolume" :digits="2"
                                    :readonly="true" unit="m³"/>
                  </DkFormItem>
                </Col>
              </DkRow>
            </DkForm>
          </div>
        </div>
      </DkPanel>
    </DkCollapse>

    <!--      下部分按钮区域-->
    <!--分销订单-->
    <DkSaveButton
      v-if="this.type != this.$config.formMode.approval"
      ref="saveButton" :disabled="allCanNoEdit"
      :showQuotation=true :loading="loading"
      right-button="purchase-order-save"
      @save="submitForm(true)"
      @close="closeForm">
    </DkSaveButton>

    <!--以销定采-->
    <DkModal
      :loading="loading"
      v-model="purchaseBasedOnSalesFlag"
      :title="$t('purchaseBasedOnSales')"
      width="1500px"
      @modalOk="purchaseBasedOnSalesOk"
      @modalCancel="clearPurchaseBasedOnSales"
    >
      <card :padding=0 style="margin-top: 1px;padding: 0">
        <!--按钮区-->
        <BaseIndexButtonGroup ref="BaseIndexButtonGroup" id="BaseIndexButtonGroup">
          <template #left>
            <BaseIndexButton ref="purchaseBasedOnSales" name="purchaseBasedOnSales"></BaseIndexButton>
            <BaseIndexButton ref="clearPurchaseBasedOnSales" name="clearPurchaseBasedOnSales"></BaseIndexButton>
          </template>
        </BaseIndexButtonGroup>
        <!-- 查询条件-->
        <DkForm ref="purchaseBasedOnSalesFormData" v-model="purchaseBasedOnSalesFormData"
                name="purchase-based-on-sales-form"
                class="form-content-class" style="margin-top: 5px">
          <!--调整单号-->
          <DkFormItem prop="orderNo" :label="$t('orderNo')">
            <InputPop ref="orderNo" v-model="purchaseBasedOnSalesFormData.orderNo"/>
          </DkFormItem>
          <!--商品型号-->
          <DkFormItem prop="skuModel" :label="$t('skuModel')">
            <InputPop ref="skuModel" v-model="purchaseBasedOnSalesFormData.skuModel"/>
          </DkFormItem>
          <!--商品编码-->
          <DkFormItem prop="skuCode" :label="$t('goodCode')">
            <InputPop ref="skuCode" v-model="purchaseBasedOnSalesFormData.skuCode"/>
          </DkFormItem>
          <!--商品名称-->
          <DkFormItem prop="skuName" :label="$t('skuName')">
            <InputPop ref="skuName" v-model="purchaseBasedOnSalesFormData.skuName"/>
          </DkFormItem>
          <!--部门-->
          <DkFormItem prop="orgId" :label="$t('orgId')">
            <SelectMagnifier v-model="purchaseBasedOnSalesFormData.orgId"
                             :display-text="purchaseBasedOnSalesFormData.orgName"
                             :type="this.$config.MagnifierType.organization"
                             :other-condition="{cpId:$store.state.user.cpId}"
                             :multiple="false"/>
          </DkFormItem>
          <!--账务日期-->
          <DkFormItem prop="deliveryTime">
            <DatePickerPop v-model="purchaseBasedOnSalesFormData.deliveryTime"
                           :placeholder="$t('inputWords',{'search-name':$t('estimateDeliveryDate')})"
                           type="date" :short-cut-flag="true"/>
          </DkFormItem>
        </DkForm>
        <DkTable primaryKey="itemId"
                 ref="table-purchase-based-on-sales-select" :id="'table-'+$options.name"
                 name="table"
                 :data="purchaseBasedOnSalesTableData"
                 :height="purchaseBasedOnSalesHeight"
                 :lazy="false"
                 :selectFlag="false"
                 :operateFlag="false"
                 :link-click-checked="true"
                 @pageChange="purchaseBasedOnSalePageSizeChange">
          <DkTableColumn class-name="fixed-left" type="checkbox" fixed="left" width="45"></DkTableColumn>
          <DkTableColumn type="seq" align="center" width="60" min-width="44px"></DkTableColumn>
          <DkTableColumn v-for="(item,index) in purchaseBasedOnSalesColumns" :key="index" :field="item.field"
                         :title="item.title"
                         :width="item.width"
                         :filter="false"
                         :resize-flag="index === purchaseBasedOnSalesColumns.length - 1"></DkTableColumn>
        </DkTable>
      </card>
    </DkModal>
  </div>
</template>

<script>

import {formMixin} from '@/mixins/form'

export default {
  components: {},
  mixins: [formMixin],
  data() {
    let self = this
    return {
      //用户敏感信息隐藏
      visUserSensitive: false,
      //以销定采
      purchaseBasedOnSalesFlag: false,
      purchaseBasedOnSalesTableData: [],
      purchaseBasedOnSalesHeight: 400,
      purchaseBasedOnSalesColumns: [
        {field: 'orderNo', width: '200px', title: self.$t('saleOrderNo'), type: 'text'},
        {
          field: 'transferableQuantity',
          width: '120px',
          type: 'number',
          digits: 0,
          sum: true
        },//数量
        {field: 'skuModel', type: 'text'},//商品型号
        {field: 'skuName', width: '200px', type: 'text'},//商品名称
        {field: 'skuCode', width: '100px', type: 'text'},//商品编码
        {field: 'makingTime', width: '200px', type: 'text'},//账务日期
        {field: 'outStatusStringDetail', width: '200px', title: self.$t('orderStatus'), type: 'text'},//单据状态
        {field: 'orgName', width: '200px', title: self.$t('orgId'), type: 'text'},//业务部门
        {field: 'staffName', width: '200px', title: self.$t('staffId'), type: 'text'},//业务员
        {field: 'priceSale', width: '120px', title: self.$t('saleQu'), type: 'text'},//调整数量
        {field: 'priceSale', width: '120px', title: self.$t('warehousePrice'), type: 'text'},//调整单价
        {field: 'itemAmount', width: '120px', title: self.$t('warehouseAmount'), type: 'text'},//调整金额
        {field: 'warehouseName', width: '120px', type: 'text'},//仓库
        {field: 'brandName', width: '120px', type: 'text'},//商品品牌
        {field: 'nonStandardCode', width: '120px', type: 'text'},//非标号
        {field: 'skuWeight', width: '120px', type: 'number', digits: 2, sum: true},//重量
        {field: 'skuVolume', width: '120px', type: 'number', digits: 2, sum: true},//体积
        {field: 'remarks', width: '120px', type: 'text'},//备注
      ],
      purchaseBasedOnSalePageInfo: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      orderId: null,
      procureList: [],//每次以销定采选择后存放数据
      //连接业务接口
      purchaseBasedOnSalesFormData: {
        orderNo: null,//调整单号
        deliveryTime: null,//账务日期
        orgId: null,//业务部门
        orgName: null,//业务名称
        skuModel: null,//商品型号
        skuCode: null,//商品名称
        skuName: null,//商品编码
      },
      //画面模式选项角标
      bottomList: ['1', '2', '3', '5'],
      setCustomerFlag: false,  // 是否是选择客户设置以及编辑时设置
      loading: false,
      allCanEdit: true,  //是否可以进行大编辑
      allCanNoEdit: false, //是否所有的都不允许编辑
      formData: {
        orderId: 0, //订单Id
        orderNo: null,
        entryTime: null,//预计收货日期
        entryWhId: null,//默认仓库
        warehouseName: null,
        orgId: self.$store.state.user.orgId,
        staffId: self.$store.state.user.staffId,
        staffName: null,
        staffCode: null,
        orgName: null,
        orgCode: null,
        erpOrgCode: null,
        remarks: null,
        goodsList: [],
        staffList: [],//业务员数组
        orgList: [],//组织机构数组
      },
      //监听上一次所选的用户
      userFormData: {},
      tableHeight: 300,
      // 保存错误消息
      errMessage: {},
      goodsColumns: [
        {
          field: 'skuModel',
          controlId: 'skuId',
          treeNode: true,
          width: 200,
          type: 'tableSelect',
          fixed: 'left',
          promotionType: true,
          dataType: self.$config.tableSelectType.goods,
          tip: self.$t('W_117'),
          searchDetailFlag: false
        },
        {
          field: 'itemQuantity',
          type: 'number',
          shareField: 'orderItemQuantity',
          digits: 0,
          sum: true,
          formula: {
            itemAmount: ['itemQuantity*priceSale*nonStandardArea', 'itemQuantity', 'priceSale', 'nonStandardArea'],
            discountStandard: ['priceSale/priceStandard*100', 'priceSale', 'priceStandard'],
            discountPromotion: ['priceSale/pricePromotion*100', 'priceSale', 'pricePromotion']
          },
        },//数量
        {
          field: 'priceSale',
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          switchField: 'flgGift', // 开关控制的列
          formula: {
            itemAmount: ['itemQuantity*priceSale*nonStandardArea', 'itemQuantity', 'priceSale', 'nonStandardArea'],
            discountStandard: ['priceSale/priceStandard*100', 'priceSale', 'priceStandard'],
            discountPromotion: ['priceSale/pricePromotion*100', 'priceSale', 'pricePromotion']
          },
          shareField: 'percent',
          percent: ['priceSale/pricePromotion*pricePromotion[child]', 'priceSale', 'pricePromotion[child]', 'pricePromotion']
        },//价格(子级放在前面，不然replace就不对)
        {
          field: 'itemAmount',
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          number: true,
          sum: true,
          shareField: 'percent',
          switchField: 'flgGift', // 开关控制的列
          supply: true,  // 补缺，最后一条，用父级-其他子级
          // amount: ['itemQuantity[child]*priceSale[child]', 'itemQuantity[child]', 'priceSale[child]'],
          percent: ['itemAmount/pricePromotion*pricePromotion[child]', 'itemAmount', 'pricePromotion[child]', 'pricePromotion'],
          formulaLate: true,  // 只有在changevalue时触发，为了避免价格和数量计算金额，然后再根据金额和数量计算价格
          formula: {
            priceSale: ['itemAmount/itemQuantity/nonStandardArea', 'itemAmount', 'itemQuantity', 'nonStandardArea'],
            discountStandard: ['itemAmount/itemQuantity/priceStandard/nonStandardArea*100', 'itemAmount', 'itemQuantity', 'priceStandard', 'nonStandardArea'],
            discountPromotion: ['itemAmount/itemQuantity/pricePromotion/nonStandardArea*100', 'itemAmount', 'itemQuantity', 'pricePromotion', 'nonStandardArea']
          },
        },//金额
        {
          field: 'warehouseName',
          title: self.$t('supplierWarehouseName'),
          type: 'disabled',
        },//经销商仓库
        {
          field: 'brandName',
          title: self.$t('skuBrand'),
          type: 'disabled',
        },//商品品牌
        {
          field: 'skuWeight',
          title: self.$t('skuWeight'),
          type: 'disabled',
        },//重量
        {
          field: 'skuVolume',
          title: self.$t('skuVolume'),
          type: 'disabled',
        },//体积
        {
          field: 'flgGift',
          type: 'switch',
          width: 100,
          value: false,
          controlField: 'cpFlgGift',
          switchField: 'priceSale'  // 开关控制的列
        },//赠品标识
        {
          field: 'remarks', type: 'text', width: 300,
          equal: true,
        },
      ],
      //商品合计信息
      goodsFootFormData: {
        sumGoodsAmount: 0,
        sumStandard: 0,
        sumPromotion: 0,
        discountPromotion: 0,
        discountStandard: 0,
        sumVolume: 0,
        sumWeight: 0,
        sumQuantity: 0,//总数量
      },
      warehouseList: [],//仓库
    }
  },

  methods: {
    /**
     * @desc   : 分页
     * @author : 付斌
     * @date   : 2024-02-19 10:44
     */
    purchaseBasedOnSalePageSizeChange(pageInfo) {
    },
    /**
     * @desc   : 以销定采弹窗打开并查询数据
     * @author : 付斌
     * @date   : 2024-02-19 10:45
     */
    purchaseBasedOnSales(modalOpenFlag) {
    },

    /**
     * @desc   : 合并以销定采数量
     * @author : 付斌
     * @date   : 2024-02-19 10:45
     */
    mergeProcureQuantity(list) {

    },
    /**
     * @desc   : 以销定采选择商品确认带回方法
     * @author : 付斌
     * @date   : 2024-02-19 10:45
     */
    purchaseBasedOnSalesOk() {

    },
    /**
     * @desc   : 关闭弹窗
     * @author : 付斌
     * @date   : 2024-02-19 10:45
     */
    clearPurchaseBasedOnSales() {
    },

    /**
     * @desc   : 选择默认仓库
     * @author : 付斌
     * @date   : 2024-02-19 10:45
     */
    warehouseOk(val) {
      if (val && val.length > 0) {
        let row = val[0]
        this.formData.entryWhId = row.whId
        this.formData.warehouseName = row.whName
      }
    },
    /**
     * @desc   : 表单值改变事件
     * @author : 付斌
     * @date   : 2024-02-19 10:46
     */
    changeValue(field, row, rowIndex) {

    },

    /**
     * @desc   : 获取商品表格中数量、金额后计算总单信息
     * @author : 付斌
     * @date   : 2024-02-19 10:46
     */
    getTotals(field, row, rowIndex, purchaseFlag) {

    },

    /**
     * @desc   : 编辑时查询非标参数
     * @author : 付斌
     * @date   : 2024-02-19 10:46
     */
    setNonGlassForm(row) {

    },

    /**
     * @desc   : 计算合计金额
     * @author : 付斌
     * @date   : 2024-02-19 10:46
     */
    getSumAmount() {

    },
    /**
     * @desc   : 关闭窗体
     * @author : 付斌
     * @date   : 2024-02-19 10:46
     */
    closeForm() {

    },
    /**
     * @desc   : 提交事件
     * @author : 付斌
     * @date   : 2024-02-19 10:47
     */
    submitForm(flag) {

    },
    /**
     * @desc   : 新建成功后清理数据
     * @author : 付斌
     * @date   : 2024-02-19 10:47
     */
    clearForm() {

    },

    /***************************列表及其他事件************************************/

    /**
     * @desc   : tabs切换事件
     * @author : 付斌
     * @date   : 2024-02-19 10:47
     */
    changeElTabs(e) {

    },
    /***************************设定参数函数************************************/

    /**
     * @desc   : 设置保存的参数（小编辑）
     * @author : 付斌
     * @date   : 2024-02-19 10:47
     */
    setSubmitFormParamsLimitEdit() {

    },
    /**
     * @desc   : 设置保存的参数
     * @author : 付斌
     * @date   : 2024-02-19 10:47
     */
    setSubmitFormParams(flag) {

    },
    /**
     * @desc   : 获取改变的数据
     * @author : 付斌
     * @date   : 2024-02-19 10:48
     */
    getTableChangeData() {

    },
    /**
     * @desc   : 处理组合中的bom数据
     * @author : 付斌
     * @date   : 2024-02-19 10:48
     */
    setCombinedBomData(row, pRow) {

    },
    /***************************created调用数据接口函数************************************/

    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2024-02-19 10:48
     */
    detail(id) {

    },
    /**
     * @desc   : 判断列是否可以编辑
     * @author : 付斌
     * @date   : 2024-02-19 10:48
     */
    checkColumnIsEdit(field) {

    },
    /**
     * @desc   : 赋值
     * @author : 付斌
     * @date   : 2024-02-19 10:48
     */
    setEntityByEdit(data) {

    },
    /**
     * @desc   : 页面初始化所需下拉数据
     * @author : 付斌
     * @date   : 2024-02-19 10:49
     */
    initSelectData() {

    },
    /**
     * @desc   : 查询客户
     * @author : 付斌
     * @date   : 2024-02-19 10:49
     */
    getCustomer() {

    },
    /**
     * @desc   : 获取业务员
     * @author : 付斌
     * @date   : 2024-02-19 10:50
     */
    getStaff(staffId) {

    },
    /**
     * @desc   : 获取业务部门 销售渠道
     * @author : 付斌
     * @date   : 2024-02-19 10:50
     */
    getOrgAndSaleChannel(orgId) {

    },
    /**
     * @desc   : 查询仓库
     * @author : 付斌
     * @date   : 2024-02-19 10:50
     */
    getWarehouse() {

    },
    /**
     * @desc   : 查询仓库
     * @author : 付斌
     * @date   : 2024-02-19 10:50
     */
    getCpIdWarehouse() {

    },
    /**
     * @desc   : 查询明细仓库
     * @author : 付斌
     * @date   : 2024-02-19 10:50
     */
    getDetailWarehouse() {

    },
    /**
     * @desc   : 设置默认值
     * @author : 付斌
     * @date   : 2024-02-19 10:50
     */
    setDefaultValue() {

    },
  },
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 寇珊珊
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.focusItem = 'checkId';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'check'  // 设置主键Id
  },
}
</script>
