
<template>


  <BasicModal
    :height="600"
    :width="900"
    v-bind="$attrs" @register="registerModal" :title="getTitle"
    @ok="handleSubmit" @cancel="handleClose"  >
    <div style="margin: 20px;">
      <a-button type="primary" style="margin-bottom: 10px; margin-right: 10px;" @click="handleAllInWarehouse">全部入库</a-button>
      <a-button type="primary" style="margin-bottom: 10px;" @click="handleCancelInWarehouse">取消全部入库</a-button>
      <a-table bordered :data-source="dataSource" :columns="columns" :pagination="false" >
        <template #bodyCell="{ column, text, record, index }">
          <template v-if="column.key === 'index'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'bcrksl'">
            <a-input-number v-model:value="record.bcrksl" :min="0" :max="record.sl - record.yrlsl" />
          </template>
        </template>
      </a-table>
    </div>
  </BasicModal>

</template>
<script lang="ts" setup>
import { ref, computed, onMounted, reactive, provide, Ref  } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';

import { useMessage } from '/@/hooks/web/useMessage';
import { getMesCheliangBg, inWarehouse } from '/@/api/chaiche/ccbg';



const emit = defineEmits(['success', 'register']);

const { notification } = useMessage();
const formRef = ref();
const isCopy = ref<boolean>(false)
const state = reactive({
  formModel: {},
  isUpdate: true,
  isView: false,
  rowId: '',
});
provide<Ref<boolean>>('isCopy', isCopy);

const [registerModal, { closeModal }] = useModalInner(async () => {
});
const props = defineProps(['recordId'])
const getTitle = computed(() => ('拆车物料入库'));
const dataSource = ref([]);
const columns = reactive([
  {
    title: '序号',
    dataIndex: 'index',
    key: 'index',
    align: 'center',
  },
  {
    title: '物料名称',
    dataIndex: 'cpmc',
    key: 'cpmc',
  },
  {
    title: '物料编码',
    dataIndex: 'cpbh',
    key: 'cpbh',
  },
  {
    title: '单位',
    dataIndex: 'dw',
    key: 'dw',
  },
  {
    title: '总数量',
    dataIndex: 'sl',
    key: 'sl',
  },
  {
    title: '已入库数量',
    dataIndex: 'yrlsl',
    key: 'yrlsl',
  },
  {
    title: '本次入库数量',
    dataIndex: 'bcrksl',
    key: 'bcrksl',
  },
]);

onMounted(async () => {
  const recordInfo = await getMesCheliangBg(props.recordId);
  recordInfo.mesCheliangBgCpList.forEach(item => {
    item.bcrksl = 0;
  })
  dataSource.value = recordInfo.mesCheliangBgCpList;
})

function handleAllInWarehouse() {
  dataSource.value.forEach(item => {
    item.bcrksl = item.sl - item.yrlsl;
  });
}

function handleCancelInWarehouse() {
  dataSource.value.forEach(item => {
    item.bcrksl = 0;
  });
}


async function handleSubmit() {
  await inWarehouse(dataSource.value);
  notification.success({
    message: 'Tip',
    description: '入库成功',
  });
  handleClose();
}

function handleClose() {
  emit('success')
}

</script>

