<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="NuInvoicingCgdMainForm">
          <a-row>
						<a-col :span="8">
							<a-form-item label="采购单号" v-bind="validateInfos.cgdNo" id="NuInvoicingCgdMainForm-cgdNo" name="cgdNo">
                <span>{{formData.cgdNo}}</span>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="采购时间" v-bind="validateInfos.qgDate" id="NuInvoicingCgdMainForm-qgDate" name="qgDate">
                <span>{{formData.qgDate}}</span>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="采购人" v-bind="validateInfos.qgBy" id="NuInvoicingCgdMainForm-qgBy" name="qgBy">
                <span>{{formData.qgBy}}</span>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="供应商" v-bind="validateInfos.gysId" id="NuInvoicingCgdMainForm-gysId" name="gysId">
                <span>{{formData.gysId_dictText}}</span>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="联系人" v-bind="validateInfos.gysLxr" id="NuInvoicingCgdMainForm-gysLxr" name="gysLxr">
                <span>{{formData.gysLxr}}</span>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="联系电话" v-bind="validateInfos.gysLxrdh" id="NuInvoicingCgdMainForm-gysLxrdh" name="gysLxrdh">
                <span>{{formData.gysLxrdh}}</span>
							</a-form-item>
						</a-col>
            <a-col :span="24" v-if="formData.sxdPath">
							<a-form-item label="随行单" v-bind="validateInfos.sxdPath" id="NuInvoicingCgdMainForm-sxdPath" name="sxdPath" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
								<j-image-upload :fileMax="0" v-model:value="formData.sxdPath" ></j-image-upload>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
    <a-table
      :columns="rkDetailColumns"
      row-key="id"
      :data-source="dataSource"
      :pagination="false"
    >
      <!-- :loading="loading"
      @change="handleTableChange" -->
      <template #bodyCell="{ column, text ,record  }">
        <!-- <template v-if="column.dataIndex === 'arrivalPrice'">
          <a-input-number :value="text" placeholder="到货价格" min="0" max="999" @change="(value) => handleRkslChange(record, 'arrivalPrice', value)" />
        </template> -->
        <!-- <template v-if="column.dataIndex === 'rksl'">
          <a-input-number :value="text" placeholder="入库数量" min="0" max="999" @blur="(value) => handleInputChange(record, 'rksl', value)" />
        </template> -->
        <template v-if="column.dataIndex === 'action'">
          <a @click="handleJianhuo(record)" v-if="record.purchaseQuantity - record.rksl - record.xzsl != 0">拣货</a>
        </template>
      </template>
    </a-table>

    <NuInvoicingCgdJhModal ref="jhModal" @success="handleJhOk"></NuInvoicingCgdJhModal>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { rukuInfo } from '../NuInvoicingCgdMain.api';
  import { rkDetailColumns } from '../NuInvoicingCgdInfo.data';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import NuInvoicingCgdJhModal from '/@/views/invoicing/cgd/components/NuInvoicingCgdJhModal.vue';
  import { useUserStore } from '/@/store/modules/user';
  import dayjs from 'dayjs';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const jhModal = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const dataSource = ref([]);
  const userStore = useUserStore();
  const formData = reactive<Record<string, any>>({
    id: '',
    qgdId: '',   
    cgdNo: '',   
    gysId: '',   
    qgDate: '',   
    qgBy: '',   
    gysLxr: '',   
    gysLxrdh: '',   
    gysFkfs: '',   
    status: '',   
    cgdType: '',   
    sxdPath: '',   
    xzdPath: '',   
    jzdPath: '',   
    reviewedBy: '',   
    reviewedTime: '',   
    content: '',   
    gysId_dictText:'',
    status_dictText:'',
    cgdType_dictText:'',
    gysFkfs_dictText:'',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 9 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 15 } });
  const labelCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 3 } });
  const wrapperCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 19 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });


  function handleJhOk(){
    getCgdInfoList();
  }
function handleJianhuo(record){
  console.log("🚀 ~ handleJianhuo ~ record:", record)
  jhModal.value.disableSubmit = false;
  jhModal.value.edit(record);
}
  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);

      getCgdInfoList();
    });
  }

  function getCgdInfoList() {
    defHttp.get({ url: '/api/pad/invoicing/queryCgdInfoList', params: { cgdId: formData.id,pageSize:-1 } }).then((res) => {
      console.log("🚀 ~ getCgdInfoList ~ res:", res)
      dataSource.value = res.records;
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {

  }

  /**
   * 作废
   */
  async function handleZuofei() {
    var status = '9';
    defHttp.post({ url: '/api/pad/invoicing/querenOrZuofei', params: { id: formData.id,status } }).then((res) => {
      emit('ok');
    });
  }

  /**
   * 确认
   */  
  async function handleQueren() {
    var status = '1';
    defHttp.post({ url: '/api/pad/invoicing/querenOrZuofei', params: { id: formData.id,status } }).then((res) => {
      emit('ok');
    });
    
  }

  defineExpose({
    add,
    edit,
    submitForm,
    handleZuofei,
    handleQueren,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
