<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <el-row class="page-search" justify="space-between" align="bottom">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80px" label-position="left">
        <el-row :gutter="20">
          <el-col :span="6" class="col_height">
            <el-form-item label="组织" prop="params.ERPFACTORY" label-width="50px">
              <el-select filterable v-model="queryParams.params.ERPFACTORY" placeholder="下拉选择组织" style="width: 240px" @change="getErpFactory">
                <el-option v-for="item in ERPFactoryList" :key="item.ERPFACTORY" :label="item.DESCRIPTION" :value="item.ERPFACTORY"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="col_height">
            <el-form-item label="物料编号" prop="params.MATERIALSPECNAME" label-width="70px">
              <el-Input v-model="queryParams.params.MATERIALSPECNAME" placeholder="输入物料编号" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="col_height">
            <el-form-item label="批次" prop="params.CHARGE" label-width="50px">
              <el-Input v-model="queryParams.params.CHARGE" placeholder="输入批次" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="queryInfo">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </div>
    </el-row>
    <!-- table 区域-->
    <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <div class="page-table-title-left">
            <span>标签信息</span>
          </div>
        </div>
        <div class="page-table-operateBtns">
          <el-popover :persistent="false" placement="right" popper-class="config-table-wrap" trigger="click">
            <template #reference>
              <el-button class="operateBtns-setting">
                <svg-icon icon-class="table_title" width="16px" height="16px" />
              </el-button>
            </template>
            <ConfigTable :list="tableConfig.column" @updateList="updateList" />
          </el-popover>
        </div>
      </div>
      <el-table :height="tableHeight" border v-loading="loading" :default-sort="{ prop: 'MATERIALSPECNAME', order: 'descending' }"
                :data="state.dataList" highlight-current-row row-key="id" style="width: 100%" @selection-change="handleSelection">
        <el-table-column type="selection" width="55" />
        <template v-for="(col, index) in tableConfig.column" :key="index">
          <el-table-column v-if="!col.hide" :label="col.header" :prop="col.field" show-overflow-tooltip
                           :sort-orders="['descending', 'ascending']" :sortable="true"/>
        </template>
      </el-table>
      <div>
        <pagination v-if="total > 0" :total="total" v-model:pageNumTo="queryParams.pageNum"
                    v-model:pageSizeTo="queryParams.pageSize" @pagination="handleQuery" />
      </div>
      <div>
        <el-form :model="state.updateParams" ref="formDataRef" label-position="top" :rules="rules">
          <el-row :gutter="22">
            <el-col :span="5">
              <el-form-item prop="CHARGE">
                <el-Input v-model="state.updateParams.CHARGE" placeholder="批次"/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item prop="EXPIRNGDATE">
                <el-date-picker v-model="state.updateParams.EXPIRNGDATE" type="datetime"
                                format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"
                                placeholder="选择保质期" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item prop="MAKEDATE">
                <el-date-picker v-model="state.updateParams.MAKEDATE" type="datetime"
                                format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"
                                placeholder="选择制造日期" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button type="primary" @click="updateInfo" v-loading="state.loadings">更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: 'notPosted'
}
</script>
<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance } from 'vue'
import { ElForm, ElMessageBox, ElTable } from 'element-plus'
import { getQueryPageList } from '@/api/common'
import { localStorage } from '@/utils/storage'
import { updatePacking } from '@/api/material/notPosted'
import { bTableHeight } from '@/composables/calcTableHeight'
import { getORG } from '@/api/auth'
// 公共变量
const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(310)
const { proxy }: any = getCurrentInstance()
const tableConfig = ref({
  loading: false,
  column: [] as any,
  data: []
})
const queryFormRef = ref(ElForm)
const formDataRef = ref()
// 变量
const state = reactive({
  dataList: [] as any, // 表格数据
  total: 0, // 总数
  selection: [] as any, // 多选
  loadings: false, // 更新按钮
  loading: true, // 表格
  // 组织
  ERPFactoryList: [] as any,
  // 查询参数
  queryParams: {
    queryId: 'NotPosted',
    version: 'G5001',
    params: {
      SITENAME: localStorage.get('siteName') || 'SDK',
      MATERIALSPECNAME: '', // 物料编号
      ERPFACTORY: '',
      CHARGE: ''
    },
    pageNum: 1,
    pageSize: 10
  },
  // 更新参数
  updateParams: {
    CHARGE: '',
    EXPIRNGDATE: '',
    MAKEDATE: ''
  },
  // 条件
  rules: {
    CHARGE: [{required: true, message: '请输入批次号', trigger: 'blur'}],
    EXPIRNGDATE: [{required: true, message: '请选择保质期', trigger: 'blur'}],
    MAKEDATE: [{required: true, message: '请选择制造日期', trigger: 'blur'}],
  }
})
const { queryParams, total, loading, ERPFactoryList, rules } = toRefs(state)

// 加载
onMounted(() => {
  tableConfig.value.column = [
    { header: 'SITENAME', field: 'SITENAME', hide: true },
    { header: 'MATERIALPACKINGNAME', field: 'MATERIALPACKINGNAME', hide: true },
    { header: '组织(CN)', field: 'ERPFACTORYDESC', hide: false },
    { header: '货位名称', field: 'LOCATIONNAMEDESC', hide: false },
    { header: '物料编号', field: 'MATERIALSPECNAME', hide: false },
    { header: '品名', field: 'DESC_CN', hide: false },
    { header: '批次号', field: 'CHARGE', hide: false },
    { header: '阶段', field: 'PHASE', hide: false },
    { header: '系统规格', field: 'DESCRIPTION', hide: false },
    { header: 'SDK规格', field: 'SPECNAME', hide: false },
    { header: '是否良品', field: 'OQARESULT', hide: false },
    { header: '保质期', field: 'EXPIRINGDATE', hide: false },
    { header: '制造日期', field: 'MAKEDATE', hide: false }
  ]
  handleQuery()
  getErpFactory()
  updateTableHeight()
  window.addEventListener('resize', handleResize)
})
// 搜索
function queryInfo() {
  state.queryParams.pageNum = 1
  handleQuery()
}
// 表格事件
const updateList = (col: any) => {
  tableConfig.value.column = col
}
// 多选
const handleSelection = (val: any) => {
  state.selection = val
}
// 更新
function updateInfo() {
  if (state.selection.length == 0) {
    ElMessageBox.alert('请选择条目', { confirmButtonText: 'OK' })
    return
  }
  formDataRef.value.validate((valid: any) => {
    if (!valid) {
      return
    }
    updatePacking({
      ...state.updateParams,
      dataList: state.selection,
    }).then((res: any) => {
      if (res.success) {
        ElMessageBox.alert('更新成功', '提醒框', {
          confirmButtonText: 'OK'
        })
        formDataRef.value.resetFields();
        handleQuery()
      } else {
        proxy.$ElMessage.error(res.message)
      }
    })
  });
}
// 查询
async function handleQuery() {
  state.loading = true
  await getQueryPageList(state.queryParams).then((res: any) => {
    if (res.data.list.length > 0) {
      state.dataList = res.data.list
      state.total = res.data.total
    } else {
      state.dataList = res.data.list
      proxy.$ElMessage.success('查询结果为空')
    }
    state.loading = false
  })
}
// 加载组织
function getErpFactory() {
  getORG({ orgNo: null }).then((res: any) => {
    state.ERPFactoryList = res.resultObj
    state.ERPFactoryList.unshift({
      ERPFACTORY: ''
    })
  })
}
// 重置
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}
</script>
<style>
.col_height {
  padding-top: 3.5px;
  padding-bottom: 3.5px;
}
</style>