<!-- @desc:采购退货新建  @auth:付斌  @time:2024/02/20 15:15 -->
<template>
  <div class="main-div" style="position: relative;">
    <!--按钮区-->
    <BaseIndexButtonGroup ref="BaseIndexButtonGroup1" id="BaseIndexButtonGroup1">
      <template #right>
        <BaseIndexButton ref="selectSourceOrder" name="selectSourceOrder" @click="selectSourceOrder"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>

    <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 :label="$t('affiliatedCompany')"
                      prop="ascpId" :required="true">
            <SelectMagnifier v-model="formData.ascpId" :displayText="formData.cpName"
                             :type="this.$config.MagnifierType.staff"
                             :readonly="true"
                             @ok="ascpIdOk"
                             :multiple="false"/>
          </DkFormItem>
          <!--采购退货单号-->
          <DkFormItem prop="rejectNo" :label="$t('procureRejectNo')">
            <InputPop ref="rejectNo" v-model="formData.rejectNo" :readonly="true"/>
          </DkFormItem>
          <!--供应商-->
          <DkFormItem prop="supplierId" :label="$t('supplier')" :required="true">
            <SelectMagnifier v-model="formData.supplierId" :display-text="formData.supplierName"
                             :type="this.$config.MagnifierType.staff"
                             :other-condition="{cpId:$store.state.user.cpId,orgId:$store.state.user.orgId}"
                             @ok="supplierOk"
                             :readonly="true"
                             :multiple="false"/>
          </DkFormItem>
          <!--预计发货日期-->
          <DkFormItem prop="deliveryDate">
            <DatePickerPop v-model="formData.deliveryDate"
                           :placeholder="$t('inputWords',{'search-name':$t('deliveryDate')})"
                           :readonly="true"
                           type="date" :short-cut-flag="true"/>
          </DkFormItem>
          <!--退货日期-->
          <DkFormItem :label="$t('rejectMakingTime')" prop="makingTime">
            <DatePickerPop v-model="formData.makingTime"
                           :placeholder="$t('inputWords',{'search-name':$t('makingTime')})"
                           :readonly="true"
                           type="date" :short-cut-flag="true"/>
          </DkFormItem>
          <!--退件收货人-->
          <DkFormItem prop="contactName" :label="$t('rejectContactName')">
            <InputPop ref="contactName" :readonly="true" v-model="formData.contactName"/>
          </DkFormItem>
          <!--退件收货电话-->
          <DkFormItem prop="contactPhones" :label="$t('rejectContactPhones')">
            <InputPop :telephone="true" :readonly="true" ref="contactPhones" v-model="formData.contactPhones"/>
          </DkFormItem>
          <!--客户区域-->
          <!--          <DkFormItem prop="addressPosition"-->
          <!--                      :required="true">-->
          <!--            &lt;!&ndash; 高德 &ndash;&gt;-->
          <!--            <RegionsChooseAmap v-model="regionData" :options="cityData" :default-value="formData.addressName"-->
          <!--                               @on-choose="chooseAddress"></RegionsChooseAmap>-->
          <!--          </DkFormItem>-->
          <!--门牌号-->
          <DkFormItem prop="addressNo" :required="true">
            <InputPop ref="addressNo"
                      v-model="formData.addressNo"/>
          </DkFormItem>
          <!--收货地址-->
          <DkFormItem :label="$t('rejectAddressFull')" prop="addressFull">
            <InputPop ref="addressFull" :readonly="true" v-model="formData.addressFull"/>
          </DkFormItem>
          <!--          &lt;!&ndash;自动办理出库&ndash;&gt;-->
          <!--          <DkFormItem prop="flgAutoIvt">-->
          <!--            <i-switch v-model="formData.flgAutoIvt" @on-change="changeOutStatus" :disabled="this.switchFlag"/>-->
          <!--          </DkFormItem>-->
          <!--出库日期-->
          <DkFormItem :label="$t('outTime')" prop="outTime">
            <DatePickerPop v-model="formData.outTime"
                           :placeholder="$t('inputWords',{'search-name':$t('outTime')})"
                           :readonly="true"
                           type="date" :short-cut-flag="true"/>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks">
            <InputPop v-model="formData.remarks" maxlength=500 textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--商品信息-->
      <DkPanel prop="goodsInformation">
        <div id="search-cond-div-goods-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="goodsTable" :data="formData.orderOutRejectItemVOList"
                     :columns-social="this.type === this.$config.formMode.add?detailAddColumns:detailColumns"
                     :columns="detailAddColumns"
                     :height="this.tableHeight-20"
                     :addFlag="false"
                     @changeValue="changeValue"
                     @delRow="delValue"
          ></EditTable>
          <div>
            <DkForm ref="goodsFootFormData" v-model="goodsFootFormData" :labelMaxWords="5" name="dk-form"
                    class="form-content-class">
              <!--退货总额-->
              <DkFormItem prop="sumGoodsAmount" :label="$t('rejectSumAmount')">
                <InputNumberPop ref="sumGoodsAmount" v-model="goodsFootFormData.sumGoodsAmount" :clearable="false"
                                :digits="2" :readonly="true"/>
              </DkFormItem>
              <!--总重量-->
              <DkFormItem prop="sumWeight">
                <InputNumberPop ref="sumWeight" v-model="goodsFootFormData.sumWeight" :digits="2"
                                :readonly="true" unit="KG"/>
              </DkFormItem>
              <!--总体积-->
              <DkFormItem prop="sumVolume">
                <InputNumberPop ref="sumVolume" v-model="goodsFootFormData.sumVolume" :digits="2"
                                :readonly="true" unit="m³"/>
              </DkFormItem>
            </DkForm>
          </div>
        </div>
      </DkPanel>
      <!--附件-->
      <DkPanel prop="files">
        <div id="search-cond-div-files-info" ref="search-cond-div-files-info"
             slot="content">
          <DkPicWall v-model="formData.annexPaths"
                     folder="t_order/annex_paths" :accept="$config.uploadFileConfig.acceptPicType"
                     :format="['jpg','jpeg','png']"></DkPicWall>
        </div>
      </DkPanel>
    </DkCollapse>

    <!--      下部分按钮区域-->
    <DkSaveButton
      ref="saveButton"
      :showQuotation=true :loading="loading"
      @save="submitForm(true)"
      @close="closeForm">
    </DkSaveButton>

    <!--选择源单-->
    <DkModal
      :loading="loading"
      v-model="selectSourceOrderFlag"
      :title="$t('selectSourceOrder')"
      width="1600px"
      @modalOk="purchaseBasedOnSalesOk"
      @modalCancel="clearPurchaseBasedOnSales"
    >
      <!-- 总单-->
      <card :padding=0 style="margin-top: 1px;padding: 0">
        <!--按钮区-->
        <BaseIndexButtonGroup ref="BaseIndexButtonGroup" id="BaseIndexButtonGroup">
          <template #left>
            <BaseIndexButton ref="selectSourceOrder" name="selectSourceOrder" @click="selectSourceOrder(true)"></BaseIndexButton>
            <BaseIndexButton ref="clearPurchaseBasedOnSales" name="clearPurchaseBasedOnSales" @click="clearPurchaseBasedOnSales(true)"></BaseIndexButton>
          </template>
        </BaseIndexButtonGroup>
        <!-- 查询条件-->
        <DkForm ref="selectSourceOrderFormData" v-model="selectSourceOrderFormData" name="select-source-order-form"
                class="form-content-class" style="margin-top: 5px">
          <!--采购单号-->
          <DkFormItem prop="procureOrderNo" :label="$t('procureOrderNo')">
            <InputPop ref="procureOrderNo" :placeholder="$t('inputWords',{'search-name':$t('procureOrderNo')})"
                      v-model="selectSourceOrderFormData.procureOrderNo"/>
          </DkFormItem>
          <!--入库单号-->
          <DkFormItem prop="entryNo" :label="$t('entryNo')">
            <InputPop ref="entryNo" v-model="selectSourceOrderFormData.entryNo"/>
          </DkFormItem>
          <!--供应商-->
          <DkFormItem prop="supplierId" :label="$t('supplier')">
            <SelectMagnifier v-model="selectSourceOrderFormData.supplierId"
                             :display-text="selectSourceOrderFormData.supplierName"
                             :type="this.$config.MagnifierType.staff"
                             :other-condition="{cpId:$store.state.user.cpId,orgId:$store.state.user.orgId}"
                             :multiple="false"/>
          </DkFormItem>
          <!--收货人-->
          <DkFormItem prop="contactName" :label="$t('consignee')">
            <InputPop ref="contactName" v-model="selectSourceOrderFormData.contactName"/>
          </DkFormItem>
          <!--收货电话-->
          <DkFormItem prop="contactPhone" :label="$t('consigneePhone')">
            <InputPop ref="contactPhone" v-model="selectSourceOrderFormData.contactPhone"/>
          </DkFormItem>
          <!--商品编码-->
          <DkFormItem prop="skuCode" :label="$t('goodCode')">
            <InputPop ref="skuCode" v-model="selectSourceOrderFormData.skuCode"/>
          </DkFormItem>
          <!--商品名称-->
          <DkFormItem prop="skuName" :label="$t('skuName')">
            <InputPop ref="skuName" v-model="selectSourceOrderFormData.skuName"/>
          </DkFormItem>
          <!--商品型号-->
          <DkFormItem prop="skuModel" :label="$t('skuModel')">
            <InputPop ref="skuModel" v-model="selectSourceOrderFormData.skuModel"/>
          </DkFormItem>
          <!--详细地址-->
          <DkFormItem prop="addressFull" :label="$t('takeOverAddress')">
            <InputPop ref="addressFull" v-model="selectSourceOrderFormData.addressFull"/>
          </DkFormItem>
          <!--采购日期-->
          <DkFormItem prop="makingTime" :label="$t('purchaseDate')">
            <DatePickerPop v-model="selectSourceOrderFormData.makingTime"
                           :placeholder="$t('inputWords',{'search-name':$t('purchaseDate')})"
                           type="date" :short-cut-flag="true"/>
          </DkFormItem>
        </DkForm>
        <DkTable primaryKey="itemId"
                 ref="select-source-order-select" :id="'table-'+$options.name"
                 name="table"
                 :data="selectSourceOrderTableData"
                 :height="selectSourceOrderHeight-100"
                 :lazy="false"
                 :selectFlag="false"
                 :operateFlag="false"
                 :link-click-checked="true"
                 @current-change="selectChangeTable"
                 @pageChange="selectSourceOrderPageSizeChange">
          <DkTableColumn class-name="fixed-left" type="radio" fixed="left" width="45"></DkTableColumn>
          <DkTableColumn type="seq" align="center" width="60" min-width="44px"></DkTableColumn>
          <DkTableColumn v-for="(item,index) in selectSourceOrderColumns" :key="index" :field="item.field"
                         v-if="!item.flgUserSensitive"
                         :title="item.title"
                         :width="item.width"
                         :filter="false"
                         :resize-flag="index === selectSourceOrderColumns.length - 1"></DkTableColumn>
        </DkTable>
      </card>
      <!-- 明细-->
      <card ref="buttonCard" class="function-button-card" :padding=0
            style="margin-top: 1px;overflow-y: auto;padding: 0">
        <DkTable primaryKey="itemId"
                 ref="select-source-order-detail-select"
                 name="table"
                 :data="selectSourceOrderDetailTableData"
                 :height="selectSourceOrderHeight-85"
                 :pageTotalFlag="false"
                 :pageFlag="false"
                 :selectFlag="false"
                 :link-click-checked="true"
                 :operateFlag="false">
          <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 selectSourceOrderDetailColumns" :key="index" :field="item.field"
                         v-if="!item.flgUserSensitive"
                         :title="item.title"
                         :width="item.width"
                         :filter="false"
                         :resize-flag="index === selectSourceOrderColumns.length - 1"></DkTableColumn>
        </DkTable>
      </card>
    </DkModal>
  </div>
</template>

<script>
import {formMixin} from '@/mixins/form'

export default {
  components: {},
  mixins: [formMixin],
  data() {
    let self = this
    return {
      //自动办理出库只读标识
      switchFlag: false,
      //选择源单
      selectSourceOrderFlag: false,
      selectSourceOrderTableData: [],//总单数据
      selectSourceOrderDetailTableData: [],//明细数据
      selectSourceOrderHeight: 350,
      //总单字段
      selectSourceOrderColumns: [
        {field: 'supplierName', width: '200px', type: 'text'},//供应商名称
        {field: 'entryNo', title: self.$t('purchaseEntryNo'), type: 'disabled'},//采购入库单
        {field: 'makingTime', title: self.$t('purchaseDate'), type: 'disabled'},//采购日期
        {field: 'entryStatusString', title: self.$t('entryStatus'), type: 'disabled'},//入库状态
        {field: 'sumEntryQuantity', width: '120px', type: 'number', digits: 0, dataType: 'number', sum: true},//入库总数
        {
          field: 'sumEntryAmount',
          width: '120px',
          type: 'number',
          flgUserSensitive: true,
          digits: 0,
          dataType: 'number',
          sum: true
        },//入库总额
        {field: 'supplierContactName', title: self.$t('contactName'), width: '200px', type: 'text'},//联系人
        {field: 'supplierContactPhone', title: self.$t('contactPhones'), width: '200px', type: 'text'},//联系电话
        {field: 'doneUserName', width: '120px', type: 'text'},//办理人
        {field: 'doneTime', width: '120px', type: 'text'},//办理时间
        {field: 'makingUserName', width: '120px', type: 'text'},//制单员
        {field: 'makingTime', width: '120px', type: 'text', title: self.$t('makingData')},//制单时间
      ],
      //明细字段
      selectSourceOrderDetailColumns: [
        {field: 'canRejectQuantity', title: self.$t('returnableQuantity'), type: 'number', digits: 2},//可退数量
        {field: 'skuModel', type: 'text'},//型号
        {field: 'skuName', width: '200px', type: 'text'},//商品名称
        {field: 'skuCode', width: '100px', type: 'text'},//sku编码
        {field: 'spuCode', width: '120px', type: 'text'},//spu编码
        {
          field: 'orderPriceSale',
          title: self.$t('orderPriceSale'),
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2
        },//采购单价
        {field: 'orderAmount', title: self.$t('purchasePrice'), type: 'disabled', flgUserSensitive: true, digits: 2},//采购金额
        {field: 'whName', type: 'disabled'},//仓库名称
        {field: 'brandName', title: self.$t('skuBrand'), type: 'disabled'},//商品品牌
        {field: 'nonStandardCode', type: 'disabled'},//非标号
        {field: 'skuWeight', width: '120px', type: 'number', digits: 2, sum: true},//重量
        {field: 'skuVolume', width: '120px', type: 'number', digits: 2, sum: true},//体积
      ],
      selectSourceOrderFormData: {
        entryNo: null,//入库单号
        orderNo: null,//采购单号
        makingTime: null,//采购日期
        supplierId: null,//供应商
        supplierName: null,//供应商名称
        contactName: null,//收货人
        contactPhone: null,//收货电话
        addressFull: null,//收货地址
        skuCode: null,//商品编码
        skuName: null,//商品名称
        skuModel: null,//商品型号
      },
      loading: false,
      orderId: null,//订单id判断是采购订单是入口，还是采购退货是入口
      formData: {
        ascpId: null,//所属公司Id
        sale1Id: null,//二级商户
        sale2Id: null,//一级商户
        cpId: null,//公司ID
        saveAscpId: null,//所属公司Id
        cpName: null,//所属公司
        rejectId: null, //订单Id
        rejectNo: null,
        supplierId: null,//供应商Id
        supplierName: null,//供应商名称
        supplierCode: null,//供应商编码
        makingTime: new Date,//退货日期
        flgAutoIvt: null,//自动办理出库
        contactName: null,//联系人
        contactPhones: null,//联系电话
        addressFull: null,//详细地址
        remarks: null,
        outTime: new Date,//出库日期
        sumRejectQuantity: 0,//合计退货数量
        sumRejectGoodsAmount: 0,//合计退货货物金额
        sumRejectAmount: 0,//合计退货金额
        salesChannel: null,//销售渠道
        salesChannelCode: null,//销售渠道编码
        orderId: null,//订单Id
        orderNo: null,//订单单号
        makingUser: self.$store.state.user.staffId,//制单人
        orderOutRejectItemVOList: [],
        files: [],
        annexPaths: [],
        outId: null,//出库单ID
        outNo: null,//出库单号
        erpOutNo: null,//ERP出库单号
        entryId: null,//入库单ID
        entryNo: null,//入库单号
        editTime: null,
        deliveryDate: null,//预计发货日期
      },
      regionData: [],//客户区域
      cityData: [],//客户区域
      detailColumns: [
        {field: 'skuModel', type: 'disabled'},//商品型号
        {field: 'skuName', type: 'disabled'},//商品名称
        {field: 'skuCode', type: 'disabled'},//商品编码
        //采购退货数量
        {
          field: 'rejectQuantity',
          title: self.$t('purchaseReturnQuantity'),
          type: 'disabled',
          digits: 2,
          sum: true,
        },

        {
          field: 'outGoingQuantity',
          title: self.$t('thisOutQuantity'),
          type: 'number',
          digits: 0,
          //negative: () => true,
          sum: true,
          formula: {
            orderAmount: ['outGoingQuantity*orderPriceSale', 'outGoingQuantity', 'orderPriceSale'],
          },
        }, //本次退货数量
        {
          field: 'orderPriceSale',
          title: self.$t('orderPriceSale'),
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          sum: true
        },//采购单价
        {
          field: 'orderAmount',
          title: self.$t('purchasePrice'),
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          sum: true
        },//采购金额
        {field: 'whName', type: 'disabled'},//仓库名称
        //库存量
        {
          field: 'ivtQty',
          title: self.$t('ivtQty'),
          type: 'disabled',
          digits: 0,
          sum: true,
        },
        //可售量
        {
          field: 'ivtUsableQty',
          title: self.$t('usableQty'),
          type: 'disabled',
          digits: 0,
          sum: true,
        },
        {field: 'nonStandardCode', type: 'disabled'},//非标号
        {field: 'specsName', title: self.$t('skuSpecs'), type: 'disabled'},//商品规格
        {field: 'brandName', title: self.$t('skuBrand'), type: 'disabled'},//商品品牌
        {field: 'skuTypeName', type: 'disabled'},//型号分类
        {field: 'skuWeight', width: '120px', type: 'disabled', digits: 2, sum: true},
        {field: 'skuVolume', width: '120px', type: 'disabled', digits: 2, sum: true},
        {field: 'remarks', type: 'text'},//备注
      ],
      detailAddColumns: [
        {field: 'skuModel', type: 'disabled'},//商品型号
        {field: 'skuName', type: 'disabled'},//商品名称
        {field: 'skuCode', type: 'disabled'},//商品编码
        {
          field: 'rejectQuantity',
          title: self.$t('thisReturnQuantity'),
          type: 'number',
          digits: 2,
          negative: () => true,
          sum: true,
          formula: {
            rejectAmount: ['rejectQuantity*orderPriceSale*nonStandardArea', 'rejectQuantity', 'orderPriceSale', 'nonStandardArea'],
          },
        }, //本次退货数量
        {
          field: 'rejectAmount',
          title: self.$t('nowRejectAmount'),
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          negative: () => true, //负数处理
          switchField: 'flgGift', // 开关控制的列
          formula: {
            rejectPrice: ['rejectAmount/rejectQuantity/nonStandardArea', 'rejectAmount', 'rejectQuantity', 'nonStandardArea'],
            discountStandard: ['rejectAmount/rejectQuantity/priceStandard*100', 'rejectAmount', 'rejectQuantity', 'priceStandard'],
            discountPromotion: ['rejectAmount/rejectQuantity/pricePromotion*100', 'rejectAmount', 'rejectQuantity', 'pricePromotion']
          },
        },//金额
        {
          field: 'canRejectQuantity',
          title: self.$t('returnableQuantity'),
          type: 'disabled',
          digits: 2,
          negative: () => true,
          sum: true
        },//可退数量
        {
          field: 'orderPriceSale',
          title: self.$t('orderPriceSale'),
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          sum: true
        },//采购单价
        {
          field: 'orderAmount',
          title: self.$t('purchasePrice'),
          type: 'disabled',
          flgUserSensitive: true,
          digits: 2,
          sum: true
        },//采购金额
        {field: 'whName', type: 'disabled'},//仓库名称
        {field: 'entryNo', title: self.$t('purchaseEntryNo'), type: 'disabled'},//采购入库单
        {field: 'entryDate', title: self.$t('purchaseDate'), type: 'disabled'},//采购日期
        {field: 'brandName', title: self.$t('skuBrand'), type: 'disabled'},//商品品牌
        // {field: 'nonStandardCode', type: 'disabled'},//非标号
        {field: 'skuWeight', width: '120px', type: 'disabled', digits: 2, sum: true},
        {field: 'skuVolume', width: '120px', type: 'disabled', digits: 2, sum: true},
        {field: 'remarks', type: 'text'},//备注
      ],
      //商品合计信息
      goodsFootFormData: {
        sumGoodsAmount: 0,
        sumVolume: 0,
        sumWeight: 0,
      },
    }
  },

  methods: {
    /**
     * @desc   : 所属公司选择后事件
     * @author : 付斌
     * @date   : 2024-02-20 13:16
     */
    ascpIdOk(val) {
    },
    /**
     * @desc   : 供应商选择后事件
     * @author : 付斌
     * @date   : 2024-02-20 13:16
     */
    supplierOk(val) {
    },
    /**
     * @desc   : 表单值改变事件
     * @author : 付斌
     * @date   : 2024-02-20 13:18
     */
    changeValue(field, row, rowIndex) {
    },
    /**
     * @desc   : 表单值改变事件
     * @author : 付斌
     * @date   : 2024-02-20 13:18
     */
    delValue(row, rowIndex) {
    },
    /**
     * @desc   : 以销定采弹窗打开并查询数据
     * @author : 付斌
     * @date   : 2024-02-20 13:19
     */
    selectSourceOrder() {
      this.selectSourceOrderFlag = true
    },
    /**
     * @desc   : 选择源单把选择商品确认带回方法
     * @author : 付斌
     * @date   : 2024-02-20 13:20
     */
    purchaseBasedOnSalesOk() {
      this.selectSourceOrderFlag = false
    },
    /**
     * @desc   :
     * @author : 付斌关闭弹窗
     * @date   : 2024-02-20 13:19
     */
    clearPurchaseBasedOnSales() {
    },
    /**
     * @desc   : 根据总单查询明细
     * @author : 付斌
     * @date   : 2024-02-20 13:21
     */
    selectChangeTable(e) {
    },
    /**
     * @desc   : 分页
     * @author : 付斌
     * @date   : 2024-02-20 13:21
     */
    selectSourceOrderPageSizeChange(pageInfo) {
    },
    /**
     * @desc   : 关闭窗体
     * @author : 付斌
     * @date   : 2024-02-20 13:24
     */
    closeForm() {

    },
    /**
     * @desc   : 提交事件
     * @author : 付斌
     * @date   : 2024-02-20 13:24
     */
    submitForm(flag) {

    },
  },
  created() {
    this.focusItem = 'supplierId';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'purRejectId'  // 设置主键Id
  },
}
</script>
