<template>
  <div class="p-2">
    <el-row :gutter="24">
      <el-col :span="24">
        <el-card shadow="never">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="success" plain icon="Check" @click="submitForm">保存</el-button>
              </el-col>

              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>
          <el-row :gutter="20">
            <!-- <el-col :span="8" :offset="0">
              <el-form-item label="单据号">
                <el-input v-model="form.batchNumbers" placeholder="请输入单据号" @input="getList" prefix-icon="Search" clearable />
              </el-form-item>
            </el-col> -->
            <el-col :span="8" :offset="0">
              <el-form-item label="采购人">
                <el-input v-model="form.purchasingAgent" placeholder="请输入采购人" @input="getList" prefix-icon="Search" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8" :offset="0">
              <el-form-item label="采购时间">
                <el-date-picker v-model="form.purchasingPeriod" type="date" value-format="YYYY-MM-DD" placeholder="选择采购时间" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :offset="0">
              <el-form-item label="供应商">
                <el-input v-model="form.dhSupplier" disabled placeholder="请输入供应商" @input="getList" prefix-icon="Search" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8" :offset="0">
              <el-form-item label="合同号">
                <el-input v-model="form.contractNumber" placeholder="请输入合同号" @input="getList" prefix-icon="Search" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8" :offset="0">
              <el-form-item label="备注">
                <el-input v-model="form.dhRemark" placeholder="请输入备注" @input="getList" prefix-icon="Search" clearable />
              </el-form-item>
            </el-col>
          </el-row>
          <el-table v-loading="loading" :data="cailiaoshebeiList">
            <el-table-column label="设备材料名称" align="center" prop="name" />
            <el-table-column label="规格型号" align="center" prop="specification" />
            <el-table-column label="物料编码" align="center" prop="materialCode" width="200" />
            <el-table-column label="需求数量" align="center" prop="demandQuantity" width="80" />
            <el-table-column label="验收数量" align="center" prop="acceptanceQuantity" />
            <el-table-column label="订货量" align="center" prop="orderQuantity" />
            <el-table-column label="预计到货时间" align="center" prop="expectedArrival" width="250">
              <template #default="scope">
                <div class="flex justify-center w100%">
                  <el-date-picker v-model="scope.row.expectedArrival" type="date" value-format="YYYY-MM-DD" />
                </div>
              </template>
            </el-table-column>
            <el-table-column label="预计生产完成时间" align="center" prop="productionTime" width="250">
              <template #default="scope">
                <div class="flex justify-center w100%">
                  <el-date-picker v-model="scope.row.productionTime" type="date" value-format="YYYY-MM-DD" />
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Cailiaoshebei" lang="ts">
import { CailiaoshebeiVO, CailiaoshebeiForm } from '@/api/materials/orderEquipment/types';
import axios from 'axios';
const cailiaoshebeiList = ref<CailiaoshebeiVO[]>([]);
const loading = ref(false);
const showSearch = ref(true);
const queryFormRef = ref<ElFormInstance>();
const cailiaoshebeiFormRef = ref<ElFormInstance>();
const { proxy } = getCurrentInstance();
var token = '';
const initFormData: CailiaoshebeiForm = {
  id: undefined,
  batchNumber: undefined,
  supplierId: undefined,
  addDataList: [],
  supplier: undefined,
  name: undefined,
  supply: undefined,
  specification: undefined,
  signalment: undefined,
  materialCode: undefined,
  arrivalTime: undefined,
  finishTime: undefined,
  unit: undefined,
  plan: undefined,
  realQuantity: undefined,
  projectId: undefined,
  remark: undefined
};
const data = reactive<PageData<any, any>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    batchNumber: undefined,
    supplierId: undefined,
    supplier: undefined,
    name: undefined,
    projectId: undefined,
    supply: undefined,
    specification: undefined,
    signalment: undefined,
    materialCode: undefined,
    arrivalTime: undefined,
    finishTime: undefined,
    unit: undefined,
    plan: undefined,
    realQuantity: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键ID不能为空', trigger: 'blur' }]
  }
});
const { queryParams, form, rules } = toRefs(data);
/** 查询物资-材料设备列表 */
const getList = async () => {
  // 请求数据
  axios
    .get('http://192.168.110.159:8898/cailiaoshebei/materialsorder/pcPlanListGHS', {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`,
        'X-Requested-With': 'XMLHttpRequest'
      },
      params: {
        projectId: initFormData.projectId,
        batchNumber: initFormData.batchNumber
      }
    })
    .then((response) => {
      form.value = response.data.rows[0];
      getListAll();
    })
    .catch((error) => {
      console.error('请求失败:', error);
    });
};
// 获取列表
const getListAll = () => {
  loading.value = true;
  axios
    .get('http://192.168.110.159:8898/cailiaoshebei/materialsorder/listGYS', {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`,
        'X-Requested-With': 'XMLHttpRequest'
      },
      params: {
        projectId: initFormData.projectId,
        batchNumber: initFormData.batchNumber
      }
    })
    .then((response) => {
      console.log('请求成功:', response.data);
      cailiaoshebeiList.value = response.data.rows;

      loading.value = false;
    })
    .catch((error) => {
      console.error('请求失败:', error);
      loading.value = false;
    });
};
/** 提交按钮 */
const submitForm = async () => {
  cailiaoshebeiList.value.forEach((item) => {
    if (item.id) {
      delete item.id;
    }
  });
  axios
    .put(
      'http://192.168.110.159:8898/cailiaoshebei/materialsorder/modifyTheOrderFormGYS',
      { ...form.value, list: cailiaoshebeiList.value, batchNumber: form.value.batchNumber }, // 请求体
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${token}`,
          'X-Requested-With': 'XMLHttpRequest'
        },
        params: {
          /* 如果你还要传 URL 查询参数 */
        }
      }
    )
    .then((response) => {
      proxy?.$modal.msgSuccess('操作成功');

      loading.value = false;
    })
    .catch((error) => {
      console.error('请求失败:', error);
      loading.value = false;
    });
};
onMounted(() => {
  const queryString = window.location.search;
  // 解析查询字符串
  const urlParams = new URLSearchParams(queryString);
  // 获取参数值
  // const token = urlParams.get('token');
  token = urlParams.get('token');
  // localStorage.setItem('Admin-Token', 'Bearer ' + token);
  initFormData.projectId = urlParams.get('projectId');
  initFormData.batchNumber = urlParams.get('batchNumber');
  getList();
});
</script>
