<template>
  <div>
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配料" prop="materialId">
        <el-input v-model="queryParams.materialId" placeholder="请输入配料" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="操作人" prop="operator">
        <el-input v-model="queryParams.operator" placeholder="请输入入库操作人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="purchaseInDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库明细ID 主键" align="center" prop="detailId" v-if="false" />
      <!-- <el-table-column label="采购单ID" align="center" prop="purchaseId" /> -->
      <el-table-column label="配料" align="center" prop="materialId">
        <template #default="scope">
          <raw :data="scope.row" />
        </template>
      </el-table-column>
      <el-table-column label="入库数量" align="center" prop="inNum" />
      <el-table-column label="入库单价" align="center" prop="inPrice" />
      <el-table-column label="入库金额" align="center" prop="inAmount" />
      <el-table-column label="入库时间" align="center" prop="inTime" width="180">
        <template #default="scope">
          <span>{{ formatterTime(scope.row.inTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operator" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button> -->
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button> -->
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">撤回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改入库记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="purchaseInDetailRef" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="采购单ID 关联t_purchase.id" prop="purchaseId">
          <el-input v-model="form.purchaseId" placeholder="请输入采购单ID 关联t_purchase.id" />
        </el-form-item> -->
        <el-form-item label="配料" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入配料" />
        </el-form-item>
        <el-form-item label="入库数量" prop="inNum">
          <el-input v-model="form.inNum" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="入库单价" prop="inPrice">
          <el-input v-model="form.inPrice" placeholder="请输入入库单价" />
        </el-form-item>
        <!-- <el-form-item label="入库金额" prop="inAmount">
          <el-input v-model="form.inAmount" placeholder="请输入入库金额" />
        </el-form-item> -->
        <el-form-item label="入库时间" prop="inTime">
          <el-date-picker clearable v-model="form.inTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择实际入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入入库操作人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PurchaseInDetail">
import { listPurchaseInDetail, getPurchaseInDetail, delPurchaseInDetail, addPurchaseInDetail, updatePurchaseInDetail } from "@/api/mat/purchaseInDetail";
import useUserStore from '@/store/modules/user'
import Raw from '@/components/Renderer/Raw.vue'

const props = defineProps({
  purchaseId: {
    type: String,
    default: ''
  }
})

const userStore = useUserStore()

const { proxy } = getCurrentInstance();

const purchaseInDetailList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const formatterTime = (time) => {
  return proxy.parseTime(time, '{y}-{m}-{d}')
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    purchaseId: undefined,
    materialId: undefined,
    inNum: undefined,
    inPrice: undefined,
    inAmount: undefined,
    inTime: undefined,
    operator: undefined,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询入库记录列表 */
function getList() {
  loading.value = true;
  listPurchaseInDetail(queryParams.value).then(response => {
    purchaseInDetailList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

const nickName = computed(() => userStore.nickName)


// 表单重置
function reset() {
  const inTime = proxy.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
  form.value = {
    detailId: null,
    purchaseId: null,
    materialId: null,
    inNum: null,
    inPrice: null,
    inAmount: null,
    inTime: inTime,
    operator: nickName,
    delFlag: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("purchaseInDetailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.detailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加入库记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _detailId = row.detailId || ids.value
  getPurchaseInDetail(_detailId).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改入库记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["purchaseInDetailRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.detailId != null) {
        updatePurchaseInDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addPurchaseInDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _detailIds = row.detailId || ids.value;
  proxy.$modal.confirm('是否确认撤回入库记录编号为"' + _detailIds + '"的数据项？').then(function () {
    loading.value = true;
    return delPurchaseInDetail(_detailIds);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("撤回成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mat/purchaseInDetail/export', {
    ...queryParams.value
  }, `purchaseInDetail_${new Date().getTime()}.xlsx`)
}


watch(() => props.purchaseId, (newVal, oldVal) => {
  if (newVal) {
    queryParams.value.purchaseId = newVal;
    getList();
  }
}, { immediate: true })

defineExpose({
  getList
})
</script>
