<!--
 * @Descripttion: (入库管理/入库记录表/in_instore_record)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-04-13)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-04-13)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent
      size="large">
      <el-row>
        <el-form-item label="交验单号" prop="deliverNo">
          <el-input v-model="queryParams.deliverNo" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="生产令号" prop="bomNo">
          <el-input v-model="queryParams.bomNo" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="质检记录单号" prop="inspectNo">
          <el-input v-model="queryParams.inspectNo" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="BN / SN码" prop="bnSnCode">
          <el-input v-model="queryParams.bnSnCode" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="queryParams.materialCode" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="queryParams.materialName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="规格型号 / 图号" prop="materialSpec">
          <el-input v-model="queryParams.materialSpec" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="入库结果" prop="result">
          <!-- <el-select v-model="queryParams.result" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.operationResultOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.operationResultOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.result" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.operationResultOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="物料类型" prop="category">
          <!-- <el-select v-model="queryParams.category" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.materialTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.materialTypeOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.category" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.materialTypeOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker v-model="queryCreateTime" style="width: 400px" type="datetimerange" range-separator="-"
            @change="handleQuery" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:ininstorerecord:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['business:ininstorerecord:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:ininstorerecord:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['business:ininstorerecord:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="inStoreId" label="ID" align="center" v-if="columns.showColumn('inStoreId')" />
      <el-table-column prop="inStoreNo" label="入库记录单号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('inStoreNo')" />
      <el-table-column prop="deliverNo" label="交验单号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('deliverNo')" />
      <el-table-column prop="bomNo" label="生产令号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('bomNo')" />
      <el-table-column prop="inspectNo" label="质检记录单号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('inspectNo')" />
      <el-table-column prop="materialCode" label="物料编码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialCode')" />
      <el-table-column prop="materialName" label="物料名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialName')" />
      <el-table-column prop="materialSpec" label="规格型号 / 图号" align="center" min-width="100" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialSpec')" />
      <el-table-column prop="category" label="物料类型" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('category')">
        <template #default="scope">
          <dict-tag :options="options.materialTypeOptions" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column prop="keyFlag" label="关键件" align="center" v-if="columns.showColumn('keyFlag')">
        <template #default="scope">
          <el-icon v-if="scope.row.keyFlag == 1" size="large" color="red">
            <StarFilled />
          </el-icon>
          <el-icon v-else />
        </template>
      </el-table-column>
      <el-table-column prop="bnSnCode" label="BN / SN码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('bnSnCode')" />
      <el-table-column prop="checkQty" label="合格数量" align="center" v-if="columns.showColumn('checkQty')" />
      <el-table-column prop="inStoreQty" label="入库数量" align="center" v-if="columns.showColumn('inStoreQty')" />
      <el-table-column prop="result" label="入库结果" align="center" v-if="columns.showColumn('result')">
        <template #default="scope">
          <dict-tag :options="options.operationResultOptions" :value="scope.row.result" />
        </template>
      </el-table-column>
      <el-table-column prop="rowNo" label="交验单行项目" align="center" v-if="columns.showColumn('rowNo')" />
      <el-table-column prop="qty" label="交验单数量" align="center" v-if="columns.showColumn('qty')" />

      <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('remark')" />
      <el-table-column prop="createBy" label="创建人" align="center" v-if="columns.showColumn('createBy')" />
      <el-table-column prop="createTime" label="创建时间" align="center" width="160"
        v-if="columns.showColumn('createTime')" />
      <el-table-column prop="updateBy" label="更新人" align="center" v-if="columns.showColumn('updateBy')" />
      <el-table-column prop="updateTime" label="更新时间" align="center" width="160"
        v-if="columns.showColumn('updateTime')" />
      <!-- <el-table-column label="操作" align="center" width="160">
                                                                                                                        <template #default="scope">
                                                                                                                          <el-button v-hasPermi="['business:ininstorerecord:edit']" type="success" plain icon="edit" title="编辑"
                                                                                                                            @click="handleUpdate(scope.row)"></el-button>
                                                                                                                          <el-button v-hasPermi="['business:ininstorerecord:delete']" type="danger" plain icon="delete" title="删除"
                                                                                                                            @click="handleDelete(scope.row)"></el-button>
                                                                                                                        </template>
                                                                                                                      </el-table-column> -->
    </el-table>
    <pagination class="mt10" background :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改入库管理/入库记录表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px" size="large">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item label="ID" prop="inStoreId">
              <el-input v-model="form.inStoreId" disabled />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="入库记录单号" prop="inStoreNo">
              <el-input v-model="form.inStoreNo" placeholder="请输入入库记录单号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="质检记录单号" prop="inspectNo">
              <el-input v-model="form.inspectNo" placeholder="请输入质检记录单号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="交验单号" prop="deliverNo">
              <el-input v-model="form.deliverNo" placeholder="请输入交验单号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="生产令号" prop="bomNo">
              <el-input v-model="form.bomNo" placeholder="请输入生产令号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料规格" prop="materialSpec">
              <el-input v-model="form.materialSpec" placeholder="请输入物料规格" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料类型" prop="category">
              <el-radio-group v-model="form.category">
                <el-radio-button v-for="item in options.materialTypeOptions" :key="item.dictValue"
                  :label="item.dictValue">{{
                    item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="关键件标识" prop="keyFlag">
              <el-radio-group v-model="form.keyFlag">
                <el-radio-button v-for="item in options.keyFlagOptions" :key="item.dictValue" :label="item.dictValue">{{
                  item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="入库数量" prop="inStoreQty">
              <el-input-number v-model="form.inStoreQty" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="入库结果" prop="result">
              <el-radio-group v-model="form.result">
                <el-radio-button v-for="item in options.operationResultOptions" :key=item.dictValue
                  :label="item.dictValue">{{
                    item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="BN / SN码" prop="bnSnCode">
              <el-input v-model="form.bnSnCode" placeholder="请输入BN/SN码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="交验单行项目" prop="rowNo">
              <el-input-number v-model="form.rowNo" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="交验单数量" prop="qty">
              <el-input-number v-model="form.qty" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="质检合格数量" prop="checkQty">
              <el-input-number v-model="form.checkQty" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="ininstorerecord">
import {
  listInInstoreRecord, addInInstoreRecord, delInInstoreRecord, updateInInstoreRecord, getInInstoreRecord,
  exportInInstoreRecord
}
  from '@/api/business/ininstorerecord.js'

import { formatDate } from '@vueuse/shared';

const { proxy } = getCurrentInstance()
// 选中inStoreId数组数组
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const loading = ref(false)
const showSearch = ref(true)
const queryCreateTime = ref([])
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  deliverNo: undefined,
  bomNo: undefined,
  inspectNo: undefined,
  materialCode: undefined,
  materialName: undefined,
  materialSpec: undefined,
  category: undefined,
  bnSnCode: undefined,
  result: undefined,
  beginTime: undefined,
  endTime: undefined,
})
const columns = ref([
  { visible: false, prop: 'inStoreId', label: 'ID' },
  { visible: false, prop: 'inStoreNo', label: '入库记录单号' },
  { visible: true, prop: 'deliverNo', label: '交验单号' },
  { visible: true, prop: 'bomNo', label: '生产令号' },
  { visible: true, prop: 'inspectNo', label: '质检记录单号' },
  { visible: true, prop: 'materialCode', label: '物料编码' },
  { visible: true, prop: 'materialName', label: '物料名称' },
  { visible: true, prop: 'materialSpec', label: '物料规格' },
  { visible: true, prop: 'category', label: '物料类型' },
  { visible: true, prop: 'keyFlag', label: '关键件' },
  { visible: true, prop: 'bnSnCode', label: 'BN/SN码' },
  { visible: true, prop: 'checkQty', label: '合格数量' },
  { visible: true, prop: 'inStoreQty', label: '入库数量' },
  { visible: true, prop: 'result', label: '入库结果' },
  { visible: true, prop: 'createBy', label: '创建人' },
  { visible: true, prop: 'createTime', label: '创建时间' },
  { visible: false, prop: 'updateBy', label: '更新人' },
  { visible: false, prop: 'updateTime', label: '更新时间' },
  { visible: false, prop: 'remark', label: '备注' },
  { visible: false, prop: 'rowNo', label: '交验单行项目' },
  { visible: false, prop: 'qty', label: '交验单数量' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

function getList() {
  loading.value = true
  listInInstoreRecord(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  if (queryCreateTime.value != null) {
    queryParams.beginTime = queryCreateTime.value[0]
    queryParams.endTime = queryCreateTime.value[1]
    // console.log(queryParams)
  }
  else {
    queryParams.beginTime = undefined
    queryParams.endTime = undefined
  }
  getList()
}

// 重置查询操作
function resetQuery() {
  queryCreateTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

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

// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref("")
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  form: {},
  rules: {
    inStoreId: [{ required: true, message: "入库记录Id不能为空", trigger: "blur", type: "number" }],
    inStoreNo: [{ required: true, message: "入库记录单号不能为空", trigger: "blur" }],
    inspectNo: [{ required: true, message: "质检记录单号不能为空", trigger: "blur" }],
    deliverNo: [{ required: true, message: "交验单号不能为空", trigger: "blur" }],
    bomNo: [{ required: true, message: "生产令号不能为空", trigger: "blur" }],
    materialCode: [{ required: true, message: "物料编码不能为空", trigger: "blur" }],
    materialName: [{ required: true, message: "物料名称不能为空", trigger: "blur" }],
    materialSpec: [{ required: true, message: "物料规格不能为空", trigger: "blur" }],
    category: [{ required: true, message: "类别不能为空", trigger: "blur" }],
    keyFlag: [{ required: true, message: "关键件标识不能为空", trigger: "blur" }],
    result: [{ required: true, message: "入库结果不能为空", trigger: "blur" }],
    bnSnCode: [{ required: true, message: "BN/SN码不能为空", trigger: "blur" }],
    rowNo: [{ required: true, message: "交验单行项目不能为空", trigger: "blur", type: "number" }],
    qty: [{ required: true, message: "交验单数量不能为空", trigger: "blur", type: "number" }],
    checkQty: [{ required: true, message: "质检合格数量不能为空", trigger: "blur", type: "number" }],
  },
  options: {
    keyFlagOptions: [], // 关键件标识字典
    operationResultOptions: [],// 操作结果字典
    materialTypeOptions: []// 物料类型字典
  }
})

const { form, rules, options } = toRefs(state)
var dictParams = [
  { dictType: 'my_key_flag', columnName: 'keyFlagOptions' },
  { dictType: 'my_operation_result', columnName: 'operationResultOptions' },
  { dictType: 'my_material_type', columnName: 'materialTypeOptions' },
]
proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.columnName] = element.list
  })
})

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单   为测试新增功能，增加了随机的默认值，因新增功能非业务需要，所以新增按钮隐藏了
function reset() {
  form.value = {
    inStoreId: parseInt(formatDate(new Date(), "YYYYMMDDHHmmss")),
    inStoreNo: formatDate(new Date(), "IN" + "YYYYMMDDHHmmss"),
    inspectNo: formatDate(new Date(), "Q" + "YYYYMMDDHHmmss"),
    deliverNo: formatDate(new Date(), "D" + "YYYYMMDDHHmmss"),
    bomNo: formatDate(new Date(), "BOM" + "YYYYMMDDHHmmss"),
    materialCode: formatDate(new Date(), "M" + "YYYYMMDDHHmmss"),
    materialName: "testMaterial",
    materialSpec: undefined,
    category: "外协件",
    keyFlag: false,
    inStoreQty: undefined,
    result: 0,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
    remark: undefined,
    bnSnCode: undefined,
    rowNo: 1,
    qty: 1,
    checkQty: 1,
  };
  proxy.resetForm("formRef")
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加'
  opertype.value = 1
}

// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.inStoreId || ids.value
  getInInstoreRecord(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = "修改数据"
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.inStoreId != undefined && opertype.value === 2) {
        updateInInstoreRecord(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
          .catch(() => { })
      } else {
        addInInstoreRecord(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
          .catch(() => { })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.inStoreId || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
    .then(function () {
      return delInInstoreRecord(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}
// 导出按钮操作
function handleExport() {
  proxy
    .$confirm('是否确认导出当前入库记录?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(async () => {
      await exportInInstoreRecord(queryParams)
    })
}

handleQuery()
</script>
