<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="modelRef.title"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
    :min-height="0"
  >
    <div class="pt-2px pr-3px recoverPage">
      <div class="form_item"><div class="item_lable">维修单号：</div>{{modelRef.repairId}}</div>
      <div class="form_item"><div class="item_lable">维修工程师：</div>{{modelRef.repairManName}}</div>
      <BasicTable @register="registerTable" />
      <div class="tips">{{ modelRef.title == '备件出库'?'注：请按备件清单给维修工程师提供备件':'回收说明：完成维修，请回收废旧备件；取消维修，请回收新的备件 (自动恢复库存)。' }}</div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted, h } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicTable, useTable, BasicColumn } from '/@/components/Table';
import { useMessage } from '/@/hooks/web/useMessage';
import { partInfo, partRecovery, partOut } from '/@/api/spares';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n();
export default defineComponent({
  components: { BasicModal, BasicTable },
  props: {
    userData: { type: Object },
  },
  emits: ['update'],
  setup(_, { emit }) {
    const repairId = ref('')
    const type = ref(0)
    const modelRef = ref({
      title:'设备入库',
      repairId:'0000123',
      repairManName:'',
    });
    const columns: BasicColumn[] = [
      {
        title: '备件编号',
        dataIndex: 'partNum',
        width: 100,
      },{
        title: '备件名称',
        dataIndex: 'partName',
        width: 100,
      },{
        title: '数量',
        dataIndex: 'partCount',
        width: 100,
      },
    ]
    const [registerTable, { reload }] = useTable({
      api: partInfo,
      searchInfo: {repairId:repairId,type:type},
      immediate:false,
      pagination:false,
      columns,
    });
    const { createMessage, createConfirm } = useMessage();
    onMounted(() => {});
    let addListFunc = () => {};
    const [register, { closeModal }] = useModalInner((data) => {
      // console.log(data);
      data && onDataReceive(data);
    });

    function onDataReceive(data) {
      type.value = data.status == 50?0:1
      modelRef.value.title = data.status == 50?'备件出库':'备件回收';
      repairId.value = data.repairId
      modelRef.value.repairId = data.repairId;
      modelRef.value.repairManName = data.repairManName;
      reload()
    }
    function handleVisibleChange() {}

    const handleSubmit = async () => {
      let api = modelRef.value.title == '备件出库'?partOut:partRecovery
      createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: `确定要提交${modelRef.value.title == '备件出库'?'出库':'备件回收'}吗?`,
          onOk: async () => {
            await api({repairId:modelRef.value.repairId})
            createMessage.success(t('common.optSuccess'));
            closeModal();
            emit('update');
          },
        });
    };
    return {
      register,
      modelRef,
      registerTable,
      handleVisibleChange,
      handleSubmit,
      addListFunc,
      t,
    };
  },
});
</script>
<style lang="less" scoped>
.recoverPage{
  .form_item{
    line-height: 30px;
    .item_lable{
      display: inline-block;
      width: 100px;
      text-align: right;
    }
  }
}
</style>
