<template>
  <div class="page-container row-container">

    <section class="list-part-container" style="flex: 2">
      <!-- 搜索区域 -->
      <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item class="section-title" label="客户产品包装关联管理" />

        <!-- <el-form-item label="辅料名称：" prop="code">
          <el-input v-model="queryParams.name" placeholder="请输入辅料名称" clearable @keyup.enter="handleQuery"
            style="width: 160px;" />
        </el-form-item> -->
        <el-form-item label="辅料编号：" prop="packageCode">
          <el-input v-model="queryParams.packageCode" placeholder="请输入辅料编号" clearable @keyup.enter="handleQuery"
            style="width:180px" />
        </el-form-item>
        <el-form-item label="产品描述：" prop="productDescription">
          <el-input v-model="queryParams.productDescription" placeholder="请输入产品描述" clearable @keyup.enter="handleQuery"
            style="width:180px" />
        </el-form-item>
        <el-form-item>
          <el-button type="info" icon="Search" @click="handleQuery">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Plus" @click="handleAdd(null)"
            v-hasPermi="['business:accessories:add']">新增</el-button>
          <el-button type="primary" icon="Download" @click="handleSync()"
            v-hasPermi="['business:accessories:sync']">拉取p2数据</el-button>
        </el-form-item>

      </el-form>
      <!-- 列表区域 -->
      <div class="el-table-container">
        <div class="el-table-inner-container">
          <el-table v-loading="loading" :data="processList" border height="100%"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column type="index" label="行号" width="50" align="center" />
            <el-table-column label="产品编码" align="center" prop="productCode" width="140">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="产品编码" v-model="scope.row.productCode"
                  style="width: 100%;" readonly>
                  <template #append>
                    <el-button icon="Search" @click="handleSelectProduct(scope.row)" />
                  </template>
                </el-input>
                <div v-else>{{ scope.row.productCode }}</div>
              </template>
            </el-table-column>
            <el-table-column label="客户编码" align="center" prop="companyCode" width="140">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="客户编码" v-model="scope.row.companyCode"
                  style="width: 100%;" readonly>
                  <template #append>
                    <el-button icon="Search" @click="handleSelectCompany(scope.row)" />
                  </template>
                </el-input>
                <div v-else>{{ scope.row.companyCode }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装形式码" align="center" prop="packageTypeCode">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装形式码" v-model="scope.row.packageTypeCode"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.packageTypeCode }}</div>
              </template>
            </el-table-column>
            <el-table-column label="产品描述" align="center" prop="productDescription" width="240">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="产品描述" v-model="scope.row.productDescription"
                  style="width: 100%;" readonly>
                </el-input>
                <div v-else>{{ scope.row.productDescription }}</div>
              </template>
            </el-table-column>
            <el-table-column label="客户助记码" align="center" prop="mnemonicCode">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="客户助记码" v-model="scope.row.mnemonicCode"
                  style="width: 100%;" readonly />
                <div v-else>{{ scope.row.mnemonicCode }}</div>
              </template>
            </el-table-column>
            <el-table-column label="客户简称" align="center" prop="companyAlias" width="120">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="客户简称" v-model="scope.row.companyAlias"
                  style="width: 100%;" readonly />
                <div v-else>{{ scope.row.companyAlias }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装箱辅料编码" align="center" prop="packageCode" width="140">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装箱辅料编码" v-model="scope.row.packageCode"
                  style="width: 100%;" readonly>
                  <template #append>
                    <el-button icon="Search" @click="handleSelectAccessories(scope.row)" />
                  </template>
                </el-input>
                <div v-else>{{ scope.row.packageCode }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装箱号" align="center" prop="packageNo" width="140">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装箱号" v-model="scope.row.packageNo"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.packageNo }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装尺寸" align="center" prop="packageStandard" width="140">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装尺寸" v-model="scope.row.packageStandard"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.packageStandard }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装长度" align="center" prop="packageLength">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装长度" v-model="scope.row.packageLength"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.packageLength }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装宽度" align="center" prop="packageWidth">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装宽度" v-model="scope.row.packageWidth"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.packageWidth }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装箱高度" align="center" prop="packageHeight">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="包装箱高度" v-model="scope.row.packageHeight"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.packageHeight }}</div>
              </template>
            </el-table-column>
            <el-table-column label="单箱毛重" align="center" prop="singleGrossWeight">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="单箱毛重" v-model="scope.row.singleGrossWeight"
                  style="width: 100%;" :step="0.0001" step-strictly :controls="false" />
                <div v-else>{{ scope.row.singleGrossWeight }}</div>
              </template>
            </el-table-column>
            <el-table-column label="单箱支数" align="center" prop="singleNumber">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="单箱支数" v-model="scope.row.singleNumber"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.singleNumber }}</div>
              </template>
            </el-table-column>
            <el-table-column label="每层箱数" align="center" prop="perNumber">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="每层箱数" v-model="scope.row.perNumber"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.perNumber }}</div>
              </template>
            </el-table-column>
            <el-table-column label="满托最小箱数" align="center" prop="minNumber">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="满托最小箱数" v-model="scope.row.minNumber"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.minNumber }}</div>
              </template>
            </el-table-column>
            <el-table-column label="满托最大箱数" align="center" prop="maxNumber">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="满托最大箱数" v-model="scope.row.maxNumber"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.maxNumber }}</div>
              </template>
            </el-table-column>
            <el-table-column label="满托支数" align="center" prop="maxBranchNumber">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="满托支数" v-model="scope.row.maxBranchNumber"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.maxBranchNumber }}</div>
              </template>
            </el-table-column>
            <el-table-column label="托盘规格" align="center" prop="palletStandard" width="140">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="托盘规格" v-model="scope.row.palletStandard"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.palletStandard }}</div>
              </template>
            </el-table-column>
            <el-table-column label="托盘类型" align="center" prop="palletType">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="托盘类型" v-model="scope.row.palletType"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.palletType }}</div>
              </template>
            </el-table-column>
            <el-table-column label="放置区域" align="center" prop="areaType">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="放置区域" v-model="scope.row.areaType"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.areaType }}</div>
              </template>
            </el-table-column>
            <el-table-column label="库存单位" align="center" prop="unit">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="库存单位" v-model="scope.row.unit"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.unit }}</div>
              </template>
            </el-table-column>
            <el-table-column label="传送状态" align="center" prop="deliveryStatus">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="传送状态" v-model="scope.row.deliveryStatus"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.deliveryStatus }}</div>
              </template>
            </el-table-column>
            <el-table-column label="外箱高度" align="center" prop="outboxHeight">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="外箱高度" v-model="scope.row.outboxHeight"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.outboxHeight }}</div>
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="备注" v-model="scope.row.remark"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.remark }}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
              <template #default="scope">
                <template v-if="!scope.row.editStatus">
                  <el-button link type="warning" size="small" icon="Edit" @click="handleUpdate(scope.row)"
                    v-hasPermi="['business:accessories:edit']">修改</el-button>
                  <el-button link type="danger" size="small" icon="Delete"
                    @click="handleDelete(scope.row, scope.$index)"
                    v-hasPermi="['business:accessories:remove']">删除</el-button>
                </template>
                <template v-else>
                  <el-button link type="success" size="small" icon="Check" @click="handleSave(scope.row)">保存</el-button>
                  <el-button link type="info" size="small" icon="Close"
                    @click="handleCancel(scope.row, scope.$index)">取消</el-button>
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
      <choice-accessories-dialog ref="choiceAccessoriesDialogRef" :singleSelected="handleChoiceAccessories" />
      <choice-company-dialog ref="choiceCompanyDialogRef" :singleSelected="handleChoiceCompany" />
      <choice-product-dialog ref="choiceProductDialogRef" :singleSelected="handleChoiceProduct" />
    </section>
  </div>
</template>

<script setup name="Equipment">
import { listCompanyProductPackage, delCompanyProductPackage, addCompanyProductPackage, updateCompanyProductPackage, getP2CompanyProductPackage, getCompanyProductPackage } from "@/api/business/companyProductPackage";
import ChoiceCompanyDialog from "./ChoiceCompanyDialog.vue";
import ChoiceProductDialog from "./ChoiceProductDialog.vue";
import ChoiceAccessoriesDialog from "./ChoiceAccessoriesDialog.vue";
const { proxy } = getCurrentInstance();
const processList = ref([]);
const loading = ref(true);
const ids = ref([])
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const choiceAccessoriesDialogRef = ref(null)
const choiceCompanyDialogRef = ref(null)
const choiceProductDialogRef = ref(null)
const currentDetail = ref(null)
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  code: null,
  name: null,
  status: null,
})

/***********************  方法区  ****************************/

/** 查询检查类别列表 */
function getList() {
  loading.value = true;
  listCompanyProductPackage(queryParams.value).then(response => {
    processList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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


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

/** 新增按钮操作 */
function handleAdd() {
  processList.value.push({ editStatus: true })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  row.editStatus = true
}

/** 删除按钮操作 */
function handleDelete(row, index) {
  const _ids = row.id || ids.value;
  if (row.id == null) {
    console.log(index)
    processList.value.splice(index, 1)
    return
  }
  proxy.$modal.confirm('是否确认删除选中的数据项？').then(function () {
    return delCompanyProductPackage(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功！");
  }).catch(() => { });
}

function handleSave(row) {
  // 保存判断
  if (row.companyId == null || row.companyAlias == '') {
    proxy.$modal.msgError("请选择对应客户")
    return
  }
  if (row.productId == null || row.productDescription == '') {
    proxy.$modal.msgError("请选择对应产品")
    return
  }
  if (row.accessoriesId == null || row.packageCode == '') {
    proxy.$modal.msgError("请输选择包装辅料")
    return
  }
  loading.value = true;
  if (row.id == null) {
    addCompanyProductPackage(row).then(afterSave)
  } else {
    updateCompanyProductPackage(row).then(afterSave)
  }
}

function afterSave(res) {
  if (res.code === 200) {
    getList()
    proxy.$modal.msgSuccess("保存成功！")
  } else {
    proxy.$modal.msgError("保存失败！")
  }
}

function handleCancel(row, index) {
  if (row.id == null) {
    processList.value.splice(index, 1)
  } else {
    getCompanyProductPackage(row.id).then(res => {
      processList.value[index] = res.data
    })
  }
  row.editStatus = false
}

function handleSync() {
  // pullP2Accessories
  loading.value = true;
  getP2CompanyProductPackage().then(res => {
    if (res.code === 200) {
      getList()
      proxy.$modal.msgSuccess("拉取成功")
    } else {
      proxy.$modal.msgError("拉取失败")
    }
  })
}

function handleSelectAccessories(row) {
  currentDetail.value = row
  choiceAccessoriesDialogRef.value.open()
}

function handleSelectCompany(row) {
  currentDetail.value = row
  choiceCompanyDialogRef.value.open()
}

function handleSelectProduct(row) {
  currentDetail.value = row
  choiceProductDialogRef.value.open()
}

function handleChoiceAccessories(data) {
  currentDetail.value.accessoriesId = data.id
  currentDetail.value.packageCode = data.accessoriesCode
}

function handleChoiceCompany(data) {
  currentDetail.value.companyId = data.id
  currentDetail.value.companyAlias = data.companyAlias
  currentDetail.value.companyCode = data.companyCode
  currentDetail.value.mnemonicCode = data.mnemonicCode
}

function handleChoiceProduct(data) {
  currentDetail.value.productId = data.id
  currentDetail.value.productDescription = data.description
  currentDetail.value.productCode = data.productCode
}

getList();
</script>
<style scoped>
:deep(.el-tree-node__label) {
  font-size: 14px !important;
  display: inline-block;
  width: 100%;
}

:deep(.el-tree-node__content) {
  height: 40px;
  border-bottom: 1px solid #ebeef5;
  padding: 8px 0;
  line-height: 23px;
}

.move-handle {
  cursor: move;
  background-color: #eee;
}
</style>
