<template>
  <el-dialog title="添加产品" v-model="visible" width="900px" height="400px" @close="close" append-to-body draggable>
    <el-form ref="dialogForm" :model="queryParams" :inline="true" style="padding-top: 16px">
      <el-form-item label="关键字：" prop="productDescription" label-width="104">
        <el-input v-model.trim="queryParams.keyword" type="text" @keydown.enter.prevent style="width: 170px"
          placeholder="请输入批号或产品描述" :clearable="true" @keyup.enter="handleSearch" />
      </el-form-item>
      <el-form-item v-if="supplierId !== '' && supplierId !== '0'" label="只看外协商配置的产品：" prop="isSupplierProducts">
        <el-switch v-model="queryParams.isSupplierProducts" active-text="是" :active-value="1" inactive-text="否"
          :inactive-value="0" @change="handleSearch" />
      </el-form-item>
      <el-form-item label-width="20px">
        <el-button type="info" icon="Search" @click="handleSearch">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="dialogTable" :data="productList" size="small" v-loading="loading" border height="370px"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column type="index" label="行号" width="50" align="center" />
      <el-table-column label="批次号" align="center" prop="lotCode" width="96" />
      <el-table-column label="料号" align="center" prop="productCode" width="96" />
      <el-table-column label="产品描述" align="center" prop="productDescription" />
      <el-table-column label="周转时间" align="center" prop="turnoverDate" width="150">
        <template #default="scope">
          {{ moment(scope.row.turnoverDate).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="箱号" align="center" prop="originalCarrier" width="220" />
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" icon="Check" :disabled="selections.length === 0" @click="handleMultipleSelected">
          确 定
        </el-button>
        <el-button type="danger" icon="Close" @click="close">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { listForOutsource } from "@/api/business/daywork";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { process_status } = proxy.useDict("process_status");
/** 工序变量 */
const total = ref(0);
const props = defineProps({
  multipleSelected: {
    type: Function,
    default: null,
  },
  supplierId: {
    type: String,
    default: "",
  },
});

const { multipleSelected, supplierId } = toRefs(props);
const productList = ref([]);
const visible = ref(false);
const loading = ref(false);
const queryParams = ref({
  existingDayworkIds: [],
  deptCode: "170000",
  // 是否看所有产品【0：看全部产品，1：只看该外协商配置的产品】
  isSupplierProducts: 0,
  supplierId: "",
  productDescription: "",
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});
const selections = ref([]);

/**
 * 对话框打开 事件
 */
function open(existingDayworkIds) {
  visible.value = true;
  queryParams.value.existingDayworkIds = existingDayworkIds;
  getList();
}

/**
 * 对话框关闭 事件
 */
function close() {
  proxy.$refs.dialogForm.resetFields();
  proxy.$refs.dialogTable.clearSelection();
  queryParams.value.pageNum = 1;
  queryParams.value.keyword = ""
  visible.value = false;
}
/**
 * 加载数据
 */
function getList() {
  loading.value = true;
  queryParams.value.supplierId = props.supplierId;
  listForOutsource(queryParams.value).then((res) => {
    productList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

/**
 * 列表checkbox列选择 事件
 */
function handleSelectionChange(selection) {
  selections.value = selection;
}

/**  搜索 事件 */
function handleSearch() {
  getList();
}
/** 多选事件 */
function handleMultipleSelected() {
  if (multipleSelected.value) {
    multipleSelected.value(selections.value);
  }
  close();
}

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