<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="产品描述：" prop="productDescription">
        <el-input v-model="queryParams.productDescription" placeholder="请输入产品描述" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="入库部门：" prop="deptId">
        <el-select-v2 v-model="deptId" :options="deptList" placeholder="请选择入库部门" style="width: 150px"
          @change="handleQuery" />
      </el-form-item>
      <el-form-item label="入库类别：" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择入库类别" style="width: 100px" @change="handleQuery">
          <el-option v-for="item in inbound_type" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="转运托盘码：" prop="transferCode">
        <el-input v-model="queryParams.transferCode" placeholder="请输入转运托盘码" clearable @keyup.enter="handleQuery"
          style="width: 100px" />
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker v-model="queryParams.startTime" type="date" value-format="YYYY-MM-DD" :editable="false"
          clearable placeholder="请选择开始时间" style="width: 145px" @change="handleDateChange" />
        <span>到</span>
        <el-date-picker v-model="queryParams.endTime" type="date" value-format="YYYY-MM-DD" :editable="false" clearable
          placeholder="请选择结束时间" style="width: 145px" @change="handleDateChange" />
      </el-form-item>
      <el-form-item label="入库单号：" prop="inboundNumber">
        <el-input v-model="queryParams.inboundNumber" placeholder="请输入入库单号" style="width: 100px" />
      </el-form-item>
      <el-form-item label="反检出库单号：" prop="reSelectOutgoingOrderNumber">
        <el-input v-model="queryParams.reSelectOutgoingOrderNumber" placeholder="请输入反检出库单号" style="width: 100px" />
      </el-form-item>
      <el-form-item label="入库状态：" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择入库类别" style="width: 144px" @change="handleQuery">
          <el-option v-for="item in product_storage_status" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </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>
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['business:inboundOrder:add']">新增</el-button>
    </div>

    <!-- 列表区 -->
    <el-table v-loading="loading" :data="inboundOrderList" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品描述" align="center" prop="productDescription" width="220" />
      <el-table-column label="入库单号" align="center" prop="inboundNumber" />
      <el-table-column label="入库部门" align="center" prop="deptName" />
      <el-table-column label="转运码" align="center" prop="transferCode" />
      <el-table-column label="入库类别" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="inbound_type" :value="scope.row.type + ''"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="入库总量" align="center" prop="inboundTotalNum" />
      <el-table-column label="存储区域" align="center" prop="storageArea">
        <template #default="scope">
          <dict-tag :options="storage_area" :value="scope.row.storageArea + ''"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否打印" align="center" prop="isPrint">
        <template #default="scope">
          {{ scope.row.isPrint == 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="product_storage_status" :value="scope.row.status + ''"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="反检出库单号" align="center" prop="reSelectOutgoingOrderNumber" />
      <el-table-column label="客户" align="center" prop="companyAlias" />
      <el-table-column label="客户编码" align="center" prop="companyCode" />
      <el-table-column label="保管员姓名" align="center" prop="storageNickName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template #default="scope">
          <el-button link type="primary" size="small" icon="Download" @click="handlePrint(scope.row)"
            v-hasPermi="['business:inboundOrder:export']">打印</el-button>
          <el-button v-if="checkEdit(scope.row)" link type="primary" size="small" icon="Edit"
            @click="handleUpdate(scope.row, true)">修改</el-button>
          <el-button link type="warning" size="small" icon="Edit" @click="handleUpdate(scope.row, false)"
            v-hasPermi="['business:inboundOrder:query']">详情</el-button>
          <el-button v-if="checkRemove(scope.row)" link type="danger" size="small" 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" />

    <!-- 表单 -->
    <inboundOrder-form ref="inboundOrderRef" :get-list="getList"></inboundOrder-form>
  </div>
</template>
<script setup name="InboundOrder">
import { listInboundOrder, delInboundOrder, exportInboundOrderPdf, setPrint, getDeptList as getDept } from "@/api/business/inboundOrder";
import inboundOrderForm from "./form"
const { proxy } = getCurrentInstance()
const { inbound_type } = proxy.useDict("inbound_type");
const { storage_area } = proxy.useDict("storage_area");
const { product_storage_status } = proxy.useDict("product_storage_status");
const { product_production_category } = proxy.useDict("product_production_category");
import auth from '@/plugins/auth'
/** 字典数组区 */
/** 查询 对象 */

const inboundOrderList = ref([]);
const loading = ref(true);
const ids = ref([])
const single = ref(true);
const multiple = ref(true);
const isDispatch = ref(false)
const total = ref(0);
const deptId = ref('0')
/**工段 */
const deptList = ref([]);
/** 入库类型 */
const typeList = ref([]);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  type: null,
  deptName: null,
  inboundTime: null,
  inboundNumber: null,
  companyAlias: null,
  companyCode: null,
  storageUserName: null,
  storageNickName: null,
  packageResourceGroupName: null,
  packageResourceGroupLineNo: null,
  productCode: null,
  productType: null,
  isSubItem: null,
  inputUserName: null,
  inputNickName: null,
  inboundTotalNum: null,
  storageArea: null,
  labelNumber: null,
  transferCode: null,
  originalTransferCode: null,
  isPrint: null,
  reSelectOutgoingOrderNumber: null,
  boxNumber: null,
  factoryWarehouse: null,
  unit: null,
  packageType: null,
  status: null,
  isAllInbound: null,
})

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

/** 查询生产入库列表 */
function getList() {
  loading.value = true;
  queryParams.value.deptId = deptId.value == '0' ? null : deptId.value;
  listInboundOrder(queryParams.value).then(response => {
    inboundOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 打印按钮操作 */
function handlePrint(row) {
  exportInboundOrderPdf({ id: row.id }).then(_ => {
    console.log('已打印完毕')
    setPrint({ id: row.id, isPrint: 1 }).then(() => {
      getList();
    })
  })
}

function getDeptList() {
  getDept().then(response => {
    console.log(response.data);
    deptList.value = response.data.rows;
    isDispatch.value = response.data.others.isDispatch;
    if (isDispatch.value) {
      deptList.value.unshift({ label: "全部", value: "0" });
    }
    deptId.value = deptList.value[0].value;
    getList()
  });
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.startTime = null
  queryParams.value.endTime = null
  deptId.value = deptList.value[0].value;
  handleQuery();
}

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

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs.inboundOrderRef.open()
}

/** 修改按钮操作 */
function handleUpdate(row, edit) {
  // const id = row.id || ids.value
  proxy.$refs.inboundOrderRef.open(row, edit)
}


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

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

function checkEdit(row) {
  // 未打印 判断权限
  // 已打印 判断权限
  // 已出库 不可编辑
  if (row.status >= 1) {
    return false
  } else if (row.isPrint == 1) {
    if (auth.hasPermi("business:inboundOrder:francise:edit")) {
      return true
    } else {
      return false
    }
  } else if (auth.hasPermi("business:inboundOrder:edit")) {
    return true
  }
  return false
}

function checkRemove(row) {
  // 未打印 判断权限
  // 已打印 判断权限
  // 已出库 不可编辑
  if (row.status >= 1) {
    return false
  } else if (row.isPrint == 1) {
    if (auth.hasPermi("business:inboundOrder:francise:remove")) {
      return true
    } else {
      return false
    }
  } else if (auth.hasPermi("business:inboundOrder:remove")) {
    return true
  }
  return false
}

getDeptList()
/** 修改查询时间 */
function handleDateChange(value) { }

function handleDeptChange() { }

// getList();
</script>
