<!-- @desc:新建销售出库  @auth:付斌  @time:2023-09-25 14:10 -->
<template>
  <div class="main-div">
    <DkCollapse @on-change="changeCollapse" ref="collapse">
      <DkPanel prop="essentialInformation">
        <!--  单据信息  -->
        <DkForm slot="content" ref="formInline" v-model="formData" :labelMaxWords="6" style="width:100%;">
          <!--订单单号-->
          <DkFormItem prop="orderNo">
            <InputPop ref="orderNo" v-model="formData.orderNo" :readonly="true"/>
          </DkFormItem>
          <!--客户电话-->
          <DkFormItem prop="cusPhone">
            <SelectMagnifier v-model="formData.cusPhone"
                             :telephone="true"
                             :other-condition="{flgOrderForm: true}"
                             primary-key="cusId"
                             ref="cusPhone"
                             :type="this.$config.MagnifierType.customer"
                             :disabled="true"
            >
            </SelectMagnifier>
          </DkFormItem>
          <!--客户名称-->
          <DkFormItem prop="cusName">
            <InputPop ref="cusName" :disabled="true" v-model="formData.cusName"/>
          </DkFormItem>
          <!--客户区域-->
          <DkFormItem prop="addressArea">
            <RegionsChoose v-model="regionData" :options="cityData" :default-value="formData.addressArea"
                           :readonly="true" ref="customerDistrict" :disabled="true"></RegionsChoose>
          </DkFormItem>
          <!--门牌号-->
          <DkFormItem prop="addressNo">
            <InputPop ref="addressNo" v-model="formData.addressNo" :disabled="true"/>
          </DkFormItem>
          <!--联系人-->
          <DkFormItem prop="contactName">
            <InputPop ref="contactName" :disabled="true"
                      v-model="formData.contactName"/>
          </DkFormItem>
          <!--详细地址-->
          <DkFormItem prop="addressFull">
            <InputPop ref="addressFull" v-model="formData.addressFull" :disabled="true"/>
          </DkFormItem>
          <!--业务部门-->
          <DkFormItem prop="orgId" :label="$t('orgId')">
            <SelectMagnifier v-model="formData.orgId" :display-text="formData.orgName"
                             :type="this.$config.MagnifierType.org"
                             @ok="orgOk"
                             :initSearchFlag="true"
                             :multiple="false" :disabled="true"/>
          </DkFormItem>
          <!--业务员-->
          <DkFormItem prop="staffId" :label="$t('staffId')">
            <SelectMagnifier v-model="formData.staffId" :display-text="formData.staffName"
                             :type="this.$config.MagnifierType.staff"
                             @ok="staffOk"
                             :initSearchFlag="true"
                             :multiple="false" :disabled="true"/>
          </DkFormItem>
          <!--客户来源-->
          <DkFormItem prop="cusFrom">
            <SelectPop v-model="formData.cusFrom" ref="cusFrom" :multiple="false"
                       :options="cusFromList"
                       labelKey="fromName"
                       valueKey=fromId :disabled="true">
            </SelectPop>
          </DkFormItem>
          <!--销售渠道-->
          <DkFormItem prop="channelId">
            <SelectPop v-model="formData.channelId" ref="channelId"
                       :options="channelList"
                       :multiple="false"
                       labelKey="channelName"
                       valueKey="channelId" :disabled="true">
            </SelectPop>
          </DkFormItem>
          <!--送货时间-->
          <DkFormItem prop="outDate" :data-type="$config.dataType.date">
            <DatePickerPop v-model="formData.outDate"
                           :short-cut-flag="true"/>
          </DkFormItem>
          <!--单据备注-->
          <DkFormItem prop="remarks">
            <InputPop ref="remarks" 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.goodsList" showFooter
                     combined-field="promotionId"
                     name="table"
                     :operate-flag="true"
                     :addFlag="false"
                     :readonly="this.type === this.$config.formMode.handle"
                     :columns="goodsColumns" controlId="skuId"
                     :new-row-flag="false"
                     @on-choose="getTotals"
                     @changeValue="changeValue"
                     @currentChangeGetData="currentChangeGetData"
                     @delRow="delItem"
          ></EditTable>
          <div>
            <DkForm ref="goodsFootFormData" v-model="goodsFootFormData" :labelMaxWords="4" name="dk-form"
                    class="form-content-class">
              <!--货物金额-->
              <DkFormItem prop="sumAmount">
                <InputNumberPop ref="sumAmount" v-model="goodsFootFormData.sumAmount" :readonly="true"
                                :digits="2"/>
              </DkFormItem>
            </DkForm>
          </div>
        </div>
      </DkPanel>
      <!--出库附件-->
      <DkPanel prop="outFiles">
        <div slot="content">
          <DkPicWall v-model="formData.annexPaths" :disabled="this.allCanNoEdit?true:false"
                     folder="t_order/annex_paths" :accept="$config.uploadFileConfig.acceptPicType"
                     :format="['jpg','jpeg','png']"></DkPicWall>
        </div>
      </DkPanel>
      <!--订单附件-->
      <DkPanel prop="orderFiles">
        <div slot="content">
          <DkPicWall v-model="formData.annexPaths" :disabled="this.allCanNoEdit?true:false"
                     folder="t_order/annex_paths" :accept="$config.uploadFileConfig.acceptPicType"
                     :format="['jpg','jpeg','png']"></DkPicWall>
        </div>
      </DkPanel>
    </DkCollapse>
    <!--下部分按钮区域   -->
    <DkSaveButton ref="saveButton" :loading="loading" :showQuotation=true :disabled="allCanNoEdit" @save="submitForm"
                  @close="closeForm"></DkSaveButton>
    <!--查询库存-->
    <DkModal
      v-model="invModel"
      :title="$t('select-ivt')"
      width="1200px"
      height="1200"
      :loading="loading"
      :okVisibleFlag="true"
      :cancelVisibleFlag="true"
      @modalOk="ivtHandleOk"
      @modalCancel="ivtHandleCancel"
      @on-visible-change="onVisibleChange"
    >
      <BaseIndexButtonGroup id="BaseIndexButtonGroup">
        <template #left>
          <!--查询-->
          <BaseIndexButton ref="search" name="search" @click="searchInventory()"></BaseIndexButton>
          <!--清空条件-->
          <BaseIndexButton :finalFlag="true" ref="clear" name="clear" @click="searchCondClear"></BaseIndexButton>
        </template>
      </BaseIndexButtonGroup>

      <div style="width: 100%;height: 10px"></div>
      <DkTabs ref="tabs" :options="ivtTabsOptions" @on-click="tabsClick" @on-clear="clearSelected"></DkTabs>
      <!--查询列表-->
      <DkTable v-show="tabIndex === 0" ref="inventoryTable"
               primaryKey="ivtId"
               :id="'table-inventory-select'"
               name="inventoryTable"
               :data="inventoryData"
               :height="400"
               :showCheckboxHeader="false"
               :selectFlag="false"
               @pageChange="pageSizeChange"
               @select-change="addSelectedTable"
               @select-all="selectAll"
      >
        <DkTableColumn class-name="fixed-left" type="checkbox" fixed="left"></DkTableColumn>
        <DkTableColumn field="skuModel"></DkTableColumn>
        <DkTableColumn field="skuName"></DkTableColumn>
        <DkTableColumn field="skuCode"></DkTableColumn>
        <DkTableColumn field="whName"></DkTableColumn>
        <DkTableColumn field="nonStdCode"></DkTableColumn>
        <DkTableColumn field="unitName"></DkTableColumn>
        <!--库存数量-->
        <DkTableColumn field="ivtQty"></DkTableColumn>
        <!--可售量-->
        <DkTableColumn field="usableQty"></DkTableColumn>
      </DkTable>
      <!--已选列表-->
      <div v-show="tabIndex === 1">
        <DkTable :primaryKey="ivtId" ref="table-selected"
                 :id="'table-inventory-selected'"
                 name="selectedTable"
                 :data="selectedTable"
                 :height="430"
                 :selectFlag="false"
                 :operateFlag="true"
                 :tree-flag="false"
                 :lazy="false"
                 :pageTotalFlag="false"
                 :pageFlag="false"
        >
          <DkTableColumn field="skuModel"></DkTableColumn>
          <DkTableColumn field="skuName"></DkTableColumn>
          <DkTableColumn field="skuCode"></DkTableColumn>
          <DkTableColumn field="whName"></DkTableColumn>
          <DkTableColumn field="nonStdCode"></DkTableColumn>
          <DkTableColumn field="unitName"></DkTableColumn>
          <!--库存数量-->
          <DkTableColumn field="ivtQty"></DkTableColumn>
          <!--可售量-->
          <DkTableColumn field="usableQty"></DkTableColumn>
        </DkTable>
      </div>
      <div slot="footer"/>
    </DkModal>
  </div>
</template>

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

export default {
  components: {},
  name: 'sale-return',
  mixins: [formMixin],
  data() {
    let self = this
    return {
      tabIndex: 0,//页签
      loading: false,
      allCanNoEdit: false, //是否所有的都不允许编辑
      // 画面表单数据
      formData: {
        outId: null,
        //大编辑标识
        allEdit: false,
        //所属公司
        cpId: this.$store.state.user.cpId,
        editTime: null,
        orderStatus: null,//订单状态
        cusId: null,//客户Id
        cusName: null,
        cusPhone: null,
        contactName: null,
        contactPhone: null,
        customerDistrict: null,         // 客户区域
        addressArea: null,              // 行政区划
        addressGcj02: null,             // 经纬度
        addressName: null,              // 小区
        addressNo: null,                // 门牌号
        addressFull: null,              // 详细地址
        staffId: null,
        staffName: null,
        staffCode: null,
        orgId: null,
        orgName: null,
        orgCode: null,
        channelId: null, //销售渠道
        cusFrom: null,//客户来源
        remarks: null,
        goodsList: [],
        goodsSocial: [],//记录列表的原始数据
        //出库明细
        orderOutItemVOList: [],
        //删除出库明细
        orderOutItemDeleteList: [],
        //出库状态(默认为出库中)
        outStatus: 2,
        //办理人
        doneUser: this.$store.state.user.id,
        //办理时间
        doneTime: new Date().format("yyyy-MM-dd hh:mm:ss"),
        //制单人
        makingUser: this.$store.state.user.id,
        //制单时间
        makingTime: new Date().format("yyyy-MM-dd hh:mm:ss"),
        //允许操作标识
        flgAllowTodone: true,
        //允许冲正标识
        flgAllowReverse: true,
        // 订单附件
        orderAnnexPaths: [],
        files: [],
        // 附件
        annexPaths: [],
      },
      regionData: [],
      cityData: [],
      channelList: [],
      cusFromList: [],//客户来源
      goodsColumns: [
        //商品型号
        {
          field: 'skuModel',
          controlId: 'skuId',
          treeNode: true,
          type: 'disabled',
          promotionType: true,
          disabled: true,
          searchDetailFlag: false
        },
        //商品名称
        {
          field: 'skuName',
          controlId: 'skuId',
          type: 'disabled',
          promotionType: true,
          disabled: true,
          searchDetailFlag: false
        },
        //唯一编码
        {
          field: 'skuCode',
          type: 'disabled',
        },
        //订单数量
        {
          field: 'orderQty',
          type: 'disabled',
          digits: 0,
          sum: true
        },
        //可出库数量
        {
          field: 'canOutQty',
          type: 'disabled',
          digits: 0,
          sum: true
        },
        {
          field: 'whName',
          type: self.isEditColumns ? 'disabled' : 'tableSelect',
          multiple: false,
          sortBoolean: false,
          dataType: self.$config.tableSelectType.entryOtherInventory,
          param: () => {
            return {skuCode: this.ivtSkuCode}
          },
          fieldUpdate: [
            {updateField: 'nonStandardCode', valueFiled: 'nonStandardCode'},//非标号
            {updateField: 'ivtQty', valueFiled: 'ivtQty'},
            {updateField: 'usableQty', valueFiled: 'usableQty'},
            {updateField: 'remarks', valueFiled: 'remarks'},
            {updateField: 'skuWeight', valueFiled: 'skuWeight'},
            {updateField: 'skuVolume', valueFiled: 'skuVolume'},
            {updateField: 'ivtId', valueFiled: 'ivtId'},
            {updateField: 'skuId', valueFiled: 'skuId'},
            {updateField: 'whId', valueFiled: 'whId'},
            {updateField: 'whName', valueFiled: 'whName'},
          ],
          searchDetailFlag: false
        },
        //出库数量
        {
          field: 'outingQty',
          type: 'number',
          digits: 0,
          dataType: 'number',
          sum: true,
          formula: {
            outingAmt: ['outingQty*priceSale', 'outingQty', 'priceSale'],
          }
        },
        // //库存量
        // {
        //   field: 'ivtQty',
        //   type: 'disabled',
        // },
        // //可售量
        // {
        //   field: 'usableQty',
        //   type: 'disabled',
        // },
        //单价
        {
          field: 'priceSale',
          type: 'disabled',
          formula: {
            outingAmt: ['priceSale*outingQty', 'priceSale', 'outingQty'],
          }
        },
        //出库金额
        {
          field: 'outingAmt',
          type: 'disabled',
          digits: 0,
          sum: true
        },
        //备注
        {field: 'remarks'},
      ],
      //商品合计信息
      goodsFootFormData: {
        sumAmount: 0,
      },
      //库存使用
      ivtTabsOptions: [
        {label: '选择列表', name: 'name1', index: 0},
        {label: '已选列表', name: 'name2', index: 1, clear: true},
      ],
      //查询库存是选中的唯一编码
      ivtSkuCode: null,
      //库存主键
      ivtId: null,
      // 查询库存
      invModel: false,
      //库存数据
      inventoryData: [],
      //已选列表
      selectedTable: [],
    }
  },
  methods: {
    /**
     * @desc   : 删除
     * @author : 付斌
     * @date   : 2022/6/27 13:21
     */
    delItem(rowIndex) {

    },

    /**
     * @desc   : desc
     * @author : 付斌
     * @date   : 2022/6/23 8:48
     */
    changeValue(field, row, rowIndex) {

    },
    /**
     * @desc   : 点击行执行事件
     * @author : 付斌
     * @date   : 2023/11/29 17:18
     */
    currentChangeGetData(e) {
    },

    /**
     *   @desc   : 获取商品表格中数量、金额后计算总单信息
     *   @date   : 2022/5/30 15:31
     *   @author : 付斌
     */
    getTotals(field, row, rowIndex) {

    },

    /**
     * @desc   : 关闭窗体
     * @author : 付斌
     * @date   : 2022/6/21 19:18
     */
    closeForm() {

    },
    /**
     * @desc   : 提交事件
     * @author : 付斌
     * @date   : 2022/03/04 13:17
     */
    submitForm(value) {

    },

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


    /***************************设定参数函数************************************/


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

    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2022/03/04 13:17
     */
    detail(id) {
      //如果是新建用订单ID，编辑、办理用出库单ID
      if (this.type === this.$config.formMode.add) {
        return this.excuteNoParam(this.$service.orderService, this.$service.orderService.getOrderForUpdate, [id]);
      } else if (this.type === this.$config.formMode.handle) {
        return this.excuteNoParam(this.$service.saleOutService, this.$service.saleOutService.getOutForEdit, [id]);
      }
    },
    /**
     * @desc   : 编辑页面赋值
     * @author : 付斌
     * @date   : 2024-03-03 9:33
     */
    setValuesByEdit(data) {
      if (data) {
        this.formData = data["order"];
        this.formData.orderItemList = data["orderItem"];
      }
    },
    /**
     * @desc   : 判断列是否可以编辑
     * @author : 付斌
     * @date   : 2022/6/21 16:42
     */
    checkColumnIsEdit(field) {

    },
    /**
     * @desc   : 自动出库库标识变更
     * @author : 付斌
     * @date   : 2023/10/20 15:24
     */
    changeFlgAutoIvt() {

    },
    /**
     *   @desc   : 获取业务员相关
     *   @date   : 2022/5/28 10:43
     *   @author : 付斌
     */
    staffOk(val) {

    },
    /**
     *   @desc   : 获取业务员相关
     *   @date   : 2022/5/28 10:43
     *   @author : 付斌
     */
    orgOk(val) {

    },

    //region 库存页面使用
    /**
     * @desc   : 查询库存
     * @author : 付斌
     * @date   : 2023/09/08 9:55
     */
    searchInventory() {

    },

    /**
     * @desc   : 切换tab(库存页面)
     * @author : 付斌
     * @date   : 2023/09/11 13:11
     */
    tabsClick(e, option) {

    },

    /**
     * @desc   : 删除已选
     * @author : 付斌
     * @date   : 2023/9/11 15:04
     */
    clearSelected() {

    },

    /**
     * @desc   : 分页
     * @author : 付斌
     * @date   : 2023/9/01 16:26
     */
    pageSizeChange(pageInfo) {

    },

    /**
     * @desc   : 选择库存数据触发
     * @author : 付斌
     * @date   : 2023/09/11 11:48
     */
    addSelectedTable(batchRows, row, checked, allFlag) {

    },

    /**
     * @desc   : 全选,全不选
     * @author : 付斌
     * @date   : 2023/09/13 10:04
     */
    selectAll(checked) {

    },

    /**
     * @desc   : 确定选择库存
     * @author : 付斌
     * @date   : 2023/09/11 13:26
     */
    ivtHandleOk() {

    },

    /**
     * @desc   : 取消选择
     * @author : 付斌
     * @date   : 2023/09/11 13:30
     */
    ivtHandleCancel() {

    },

    /**
     * @desc   : 点击X关闭选择窗体时触发事件
     * @author : 付斌
     * @date   : 2023/09/13 9:21
     */
    onVisibleChange(e) {

    },

    /**
     * @desc   : 清空查询条件
     * @author : 付斌
     * @date   : 2023/09/12 16:24
     */
    searchCondClear() {

    }
    //endregion
  },

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

    //如果是新建用订单ID，编辑、办理用出库单ID
    if (this.type === this.$config.formMode.add) {
      this.detail(this.$route.query.orderId);
    } else if (this.type === this.$config.formMode.handle) {
      this.detail(this.$route.query.outId);
    }
  },
}
</script>
