<template>
  <el-dialog title="选择产品" v-model="visible" width="800px" height="400px" @close="close" append-to-body draggable>
    <el-form ref="dialogForm" class="master-container" :model="queryParams" :inline="true" style="align-items: center">
      <div style="display: flex">
        <el-form-item>
          <el-button type="primary" icon="Plus" @click="handleAddReject">新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Finished" @click="handleSave">保存</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-table ref="dialogTable" :data="dataList" size="small" border height="370px">
      <el-table-column type="index" label="行号" width="50" align="center" />
      <el-table-column label="废品数量" width="160px" prop="rejectNum" align="center">
        <template #default="scope">
          <el-input-number v-model.trim="scope.row.rejectNum" placeholder="废品数量" :precision="0" :min="0"
            controls-position="right" />
        </template>
      </el-table-column>
      <el-table-column prop="reason" label="废品原因" align="center" width="300px">
        <template #default="scope">
          <el-select v-model="scope.row.reason" placeholder="请选择废品原因">
            <el-option v-for="cause in waste_causes" :key="cause.value" :label="cause.label"
              :value="cause.value"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template #default="scope">
          <el-button link type="danger" icon="Delete" @click="handleDeleteReject(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>
</template>
<script setup>
import {
  getDayworkItemInfo,
  updateDayworkItemInfo,
  getRejectList,
  saveRejectList
} from "@/api/business/dayworkAuxiliaryItem.js";
const emit = defineEmits(['submit'])
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { process_status } = proxy.useDict("process_status");
/** 工序变量 */
const total = ref(0);
const props = defineProps({
  singleSelected: {
    type: Function,
    default: null,
  },
});

const detailsRow = {
  id: null,
  rejectNum: "",
  reason: "",
};

const { singleSelected } = toRefs(props);
const dataList = ref([]);
const visible = ref(false);
const loading = ref(false);
const row = ref({})
const { waste_causes } = proxy.useDict("waste_causes");
const queryParams = ref({
  name: "",
  pageNum: 1,
  pageSize: 10,
});
const selections = ref([]);

/**
 * 对话框打开 事件
 */
function open(data) {
  row.value = data
  loadRejectList()
  visible.value = true;
}

function loadRejectList() {
  getRejectList(row.value).then(res => {
    if (res.code == 200) {
      dataList.value = res.data
    }
  })
}

/**
 * 对话框关闭 事件
 */
function close() {
  proxy.$refs.dialogForm.resetFields();
  proxy.$refs.dialogTable.clearSelection();
  queryParams.value.pageNum = 1;
  visible.value = false;
}

/**
 * 加载数据
 */
function getList() {
  loading.value = true;
  queryParams.value.supplierId = props.supplierId;
  listProduct(queryParams.value).then((res) => {
    dataList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

/** 单选事件 */
function handleSingleSelected(row) {
  if (singleSelected.value) {
    singleSelected.value(row);
  }
  close();
}

//删除废弃信息
function handleDeleteReject(row) {
  dataList.value.splice(dataList.value.indexOf(row), 1);
}

function handleSave() {
  // 判断是否有废品原因为空
  if (dataList.value.some(t => t.reason == null || t.reason == '')) {
    proxy.$modal.msgError('请选择废品原因')
    return
  }
  console.log(dataList.value)
  saveRejectList({ ...row.value, rejectList: dataList.value }).then(res => {
    if (res.code === 200) {
      emit('submit')
      close()
    } else {
      proxy.$modal.msgError(res.msg)
    }
  })
}


//添加废弃信息
function handleAddReject() {
  console.log(row.value)
  const newDetail = JSON.parse(JSON.stringify(detailsRow));
  newDetail.rejectNum = 0;
  newDetail.dayworkId = row.value.dayworkId;
  newDetail.processId = row.value.dayworkItem.processId
  newDetail.dayworkAuxiliaryItemId = row.value.dayworkAuxiliaryItemId
  newDetail.dayworkAuxiliaryItemRejectId = row.value.id;
  newDetail.deptId = row.value.dayworkItem.deptId
  newDetail.lotId = row.value.lotId;
  newDetail.dayworkItemId = row.value.dayworkItemId
  newDetail.productionPlanDetailId = row.value.productionPlanDetailId
  dataList.value.splice(0, 0, newDetail);
}

defineExpose({
  open,
});
</script>
