<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="accessoriesCode">
          <el-input v-model="queryParams.accessoriesCode" placeholder="请输入辅料编号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料描述：" prop="description">
          <el-input v-model="queryParams.description" placeholder="请输入物料描述" clearable @keyup.enter="handleQuery" />
        </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="accessoriesCode">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="辅料编号" v-model="scope.row.accessoriesCode"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.accessoriesCode }}</div>
              </template>
            </el-table-column>
            <el-table-column label="账号" align="center" prop="itype">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="账号" v-model="scope.row.itype" style="width: 100%;" />
                <div v-else>{{ scope.row.itype }}</div>
              </template>
            </el-table-column>
            <el-table-column label="主管部门" align="center" prop="cusven">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="主管部门" v-model="scope.row.cusven"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.cusven }}</div>
              </template>
            </el-table-column>
            <el-table-column label="物料描述" align="center" prop="description" width="200">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="物料描述" v-model="scope.row.description"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.description }}</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="pouom">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="换算单位" v-model="scope.row.pouom"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.pouom }}</div>
              </template>
            </el-table-column>
            <el-table-column label="环保大类码" align="center" prop="htype">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="环保大类码" v-model="scope.row.htype"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.htype }}</div>
              </template>
            </el-table-column>
            <el-table-column label="环保大类简称" align="center" prop="htyna" width="120">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="环保大类简称" v-model="scope.row.htyna"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.htyna }}</div>
              </template>
            </el-table-column>
            <el-table-column label="月用量" align="center" prop="monthUseAmount">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="月用量" v-model="scope.row.monthUseAmount"
                  style="width: 100%;" :step="0.01" step-strictly :controls="false" />
                <div v-else>{{ scope.row.monthUseAmount }}</div>
              </template>
            </el-table-column>
            <el-table-column label="类别" align="center" prop="ltype">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="类别" v-model="scope.row.ltype" style="width: 100%;" />
                <div v-else>{{ scope.row.ltype }}</div>
              </template>
            </el-table-column>
            <el-table-column label="交货天数" align="center" prop="deliveryDate">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="交货天数" v-model="scope.row.deliveryDate"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.deliveryDate }}</div>
              </template>
            </el-table-column>
            <el-table-column label="安全库存量" align="center" prop="safetyStock">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="安全库存量" v-model="scope.row.safetyStock"
                  style="width: 100%;" :step="0.01" step-strictly :controls="false" />
                <div v-else>{{ scope.row.safetyStock }}</div>
              </template>
            </el-table-column>
            <el-table-column label="单价" align="center" prop="price">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="单价" v-model="scope.row.price"
                  style="width: 100%;" :step="0.00000001" step-strictly :controls="false" />
                <div v-else>{{ scope.row.price }}</div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="状态" v-model="scope.row.status"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.status }}</div>
              </template>
            </el-table-column>
            <el-table-column label="环保标志" align="center" prop="ecoMark">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="环保标志" v-model="scope.row.ecoMark"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.ecoMark }}</div>
              </template>
            </el-table-column>
            <el-table-column label="单位换算比例" align="center" prop="conversionRatio" width="120">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="单位换算比例" v-model="scope.row.conversionRatio"
                  style="width: 100%;" :step="0.0001" step-strictly :controls="false" />
                <div v-else>{{ scope.row.conversionRatio }}</div>
              </template>
            </el-table-column>
            <el-table-column label="包装箱尺寸" align="center" prop="packageStandard">
              <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="ypart">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="美捷原系统物料编码" v-model="scope.row.ypart"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.ypart }}</div>
              </template>
            </el-table-column>
            <el-table-column label="当月加权平均单价" align="center" prop="monthAvgPrice">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="当月加权平均单价" v-model="scope.row.monthAvgPrice"
                  style="width: 100%;" :step="0.000001" step-strictly :controls="false" />
                <div v-else>{{ scope.row.monthAvgPrice }}</div>
              </template>
            </el-table-column>
            <el-table-column label="费用类别" align="center" prop="ftype">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="费用类别" v-model="scope.row.ftype"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.ftype }}</div>
              </template>
            </el-table-column>
            <el-table-column label="领用计费形式" align="center" prop="ktype">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="领用计费形式" v-model="scope.row.ktype"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.ktype }}</div>
              </template>
            </el-table-column>
            <el-table-column label="标准寿命天数" align="center" prop="ktyts">
              <template #default="scope">
                <el-input-number v-if="scope.row.editStatus" placeholder="标准寿命天数" v-model="scope.row.ktyts"
                  style="width: 100%;" :step="1" step-strictly :controls="false" />
                <div v-else>{{ scope.row.ktyts }}</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" />
    </section>
  </div>
</template>

<script setup name="Equipment">
import { listAccessories, delAccessories, addAccessories, updateAccessories, getP2Accessories, getAccessories } from "@/api/business/accessories";
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 queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  code: null,
  name: null,
  status: null,
})

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

/** 查询检查类别列表 */
function getList() {
  loading.value = true;
  listAccessories(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 delAccessories(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功！");
  }).catch(() => { });
}

function handleSave(row) {
  // 保存判断
  if (row.accessoriesCode == null || row.accessoriesCode == '') {
    proxy.$modal.msgError("请输入辅料编码！")
    return
  }
  if (row.itype == null || row.itype == '') {
    proxy.$modal.msgError("请输入账号")
    return
  }
  if (row.ltype == null || row.ltype == '') {
    proxy.$modal.msgError("请输入辅料类别")
    return
  }
  loading.value = true;
  if (row.id == null) {
    addAccessories(row).then(afterSave)
  } else {
    updateAccessories(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 {
    getAccessories(row.id).then(res => {
      processList.value[index] = res.data
    })
  }
  row.editStatus = false
}

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

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>
