<!--
 * @Descripttion: (出库管理/装配记录表/out_assemble_record)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-04-14)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-04-14)
-->
<template>
  <div>
    <el-form :model="statParams" label-position="right" inline ref="statRef" v-show="showSearch" @submit.prevent
      size="large">
      <el-form-item label="生产令号" prop="bomNo">
        <el-input v-model="statParams.bomNo" placeholder="请输入" class="z-el-input" clearable @keyup.enter="handleStat" />
      </el-form-item>
      <el-form-item label="装配结果" prop="result">
        <el-select v-model="statParams.result" placeholder="请选择" clearable style="width:120px" @change="handleStat">
          <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-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleStat">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetStat">{{ $t('btn.reset') }}</el-button>
      </el-form-item>

    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:outassemblerecord: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:outassemblerecord: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:outassemblerecord:delete']" plain
                                                                              icon="delete" @click="handleDelete">
                                                                              {{ $t('btn.delete') }}
                                                                            </el-button>
                                                                          </el-col> -->
      <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataStat" 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="bomNo" label="生产令号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="result" label="装配结果" align="center">
        <template #default="scope">
          <dict-tag :options="options.operationResultOptions" :value="scope.row.result" />
        </template>
      </el-table-column>
      <el-table-column prop="assembleQty" label="装配数量" align="center" />
      <el-table-column label="追溯" align="center" width="160">
        <template #default="scope">
          <el-button plain type="primary" @click="handleQuery(scope.row.bomNo)" icon="setting"> 装配 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="totalStat" v-model:page="statParams.pageNum"
      v-model:limit="statParams.pageSize" @pagination="getStat" />


    <!-- 装配追溯对话框 -->
    <el-dialog :title="title" v-model="open" width="70%">
      <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent
        size="large">
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="queryParams.materialCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery(title)" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="queryParams.materialName" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery(title)" />
        </el-form-item>
        <el-form-item label="物料类型" prop="category">
          <el-select v-model="queryParams.category" placeholder="请选择" clearable style="width:120px"
            @change="handleQuery(title)">
            <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-form-item>
        <el-form-item label="规格型号 / 图号" prop="materialSpec">
          <el-input v-model="queryParams.materialSpec" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery(title)" />
        </el-form-item>
        <el-form-item label="装配结果" prop="result">
          <el-select v-model="queryParams.result" placeholder="请选择" clearable style="width:120px"
            @change="handleQuery(title)">
            <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-form-item>
        <el-form-item label="关键件标识" prop="keyFlag">
          <el-select v-model="queryParams.keyFlag" placeholder="请选择" clearable style="width:120px"
            @change="handleQuery(title)">
            <el-option v-for="dict in options.keyFlagOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.keyFlagOptions" :value="dict.dictValue" />
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="BN / SN码">
          <el-input v-model="queryParams.bnSnCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery(title)" />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker v-model="queryCreateTime" style="width: 400px" type="datetimerange" range-separator="-"
            @change="handleQuery(title)" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery(title)">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery(title)">{{ $t('btn.reset') }}</el-button>
        </el-form-item>

      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <!-- <el-button type="danger" :disabled="multiple" v-hasPermi="['business:sapdeliverrecord:delete']" plain
          icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </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="assembleId" label="ID" align="center" v-if="columns.showColumn('assembleId')" />
        <el-table-column prop="bomNo" label="生产令号" align="center" :show-overflow-tooltip="true"
          v-if="columns.showColumn('bomNo')" />
        <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" :show-overflow-tooltip="true" width="120"
          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" 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="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="picture" label="实物图片" align="center" v-if="columns.showColumn('picture')">
          <template #default="scope">
            <el-image preview-teleported :hide-on-click-modal="true" lazy class="table-td-thumb" fit="contain"
              :src="scope.row.picture" :preview-src-list="[scope.row.picture]">
              <div>
                <el-icon :size="15">
                  <document />
                </el-icon>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="assembleQty" label="装配数量" align="center" v-if="columns.showColumn('assembleQty')" />
        <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:outassemblerecord:edit']" type="success" plain icon="edit" title="编辑"
                                          @click="handleUpdate(scope.row)"></el-button>
                                        <el-button v-hasPermi="['business:outassemblerecord: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>
  </div>
</template>

<script setup name="outassemblerecord">
import {
  listOutAssembleRecord, addOutAssembleRecord, delOutAssembleRecord, updateOutAssembleRecord, getOutAssembleRecord, statOutAssembleRecord

}
  from '@/api/business/outassemblerecord.js'

const { proxy } = getCurrentInstance()
// 选中assembleId数组数组
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',
  bomNo: undefined,
  materialCode: undefined,
  materialName: undefined,
  materialSpec: undefined,
  category: undefined,
  keyFlag: undefined,
  result: undefined,
  bnSnCode: undefined,
  beginTime: undefined,
  endTime: undefined,
})
const statParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: false, prop: 'assembleId', label: 'ID' },
  { visible: false, prop: 'bomNo', 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: 'result', label: '装配结果' },
  { visible: true, prop: 'picture', label: '实物图片' },
  { visible: true, prop: 'assembleQty', 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: '备注' },
])
const total = ref(0)
const dataList = ref([])
const totalStat = ref(0)
const dataStat = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

function getStat() {
  loading.value = true
  statOutAssembleRecord(statParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataStat.value = data.result
      totalStat.value = data.totalNum
      loading.value = false
      // console.log(dataStat)
    }
  })
}
function getList() {
  loading.value = true
  listOutAssembleRecord(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery(bomNo) {
  queryParams.pageNum = 1
  queryParams.bomNo = bomNo
  title.value = bomNo
  if (queryCreateTime.value != null) {
    queryParams.beginTime = queryCreateTime.value[0]
    queryParams.endTime = queryCreateTime.value[1]
    // console.log(queryParams)
  }
  else {
    queryParams.beginTime = undefined
    queryParams.endTime = undefined
  }
  open.value = true
  getList()
}
// 查询
function handleStat() {
  statParams.pageNum = 1
  getStat()
}
// 重置查询操作
function resetQuery() {
  queryCreateTime.value = []
  proxy.resetForm("queryRef")
  handleQuery(title.value)
}
function resetStat() {
  proxy.resetForm("statRef")
  handleStat()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.assembleId);
  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 title = ref("")
const open = ref(false)
const state = reactive({
  options: {
    keyFlagOptions: [], // 关键件标识字典
    operationResultOptions: [],// 操作结果字典
    materialTypeOptions: []// 物料类型字典
  }
})

const { 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 closeDialog() {
  open.value = false
  reset()
}

handleStat()
</script>
