<!--
  功能：功能描述  盘点审核
  作者：陶庆峰
-->
<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" label-width="80px" label-position="left">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="盘点计划" prop="SHIPREQUESTNAME">
              <el-Input v-model="queryParams.CHECKPLANNAME" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="组织" prop="ERPFACTORY">
              <el-select
                filterable
                v-model="queryParams.ERPFACTORY"
                placeholder="下拉选择组织"
                size="small"
              >
                <el-option
                  v-for="item in ERPFactoryList"
                  :key="item.ERPFACTORY"
                  :label="item.DESCRIPTION"
                  :value="item.ERPFACTORY"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="handleQueryInfo">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </div>
    </el-row>
    <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>
      <div>
        <el-table border v-loading="loading" :data="state.list2" highlight-current-row row-key="id" style="width: 100%;min-height: 100px;height: 200px "
          @row-click="handle">
          <template v-for="(col, index) in column1" :key="index">
            <el-table-column v-if="!col.hide" :label="col.header" show-overflow-tooltip>
              <template #default="scope">
                <span>{{ scope.row[col.field] }}</span>
              </template>
            </el-table-column>
          </template>
        </el-table>
<!--        <pagination v-if="total2 > 0" :total2="total2" v-model:pageNumTo="pageNum" v-model:pageSizeTo="pageSize"-->
<!--          @pagination="handleQuery2" />-->
        <pagination v-if="total2 > 0" :total="total2" v-model:pageNumTo="state.pageNum2"
                    v-model:pageSizeTo="state.pageSize2" @pagination="handleQuery2" />
      </div>
      <div>
        <el-table :height="tableHeight"  border v-loading="loading" :data="list" highlight-current-row row-key="id" style="width: 100%"
           @selection-change="handleSelectionChange">
<!--          <el-table-column type="selection" width="55" />-->
          <template v-for="(col, index) in column" :key="index">
            <el-table-column v-if="!col.hide" :label="col.header" show-overflow-tooltip>
              <template #default="scope">
                <span>{{ scope.row[col.field] }}</span>
              </template>
            </el-table-column>
          </template>
        </el-table>
      </div>
    </div>

    <div class="demo-date-picker" style="background-color: #fff; padding-top: 10px;">
      <el-row>
        <el-col :span="6" :offset="16">
          <el-form-item label="过账日期" prop="state.commitDate">
              <el-date-picker :clearable="true" :editable="true" v-model="state.commitDate" type="date" placeholder="选择过账时间" value-format="YYYY-MM-DD"/>
            </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="audit">审核</el-button>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: "auditSheet",
};
</script>

<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance } from "vue";
import { ElForm, ElMessageBox, ElTable } from "element-plus";
import { getQueryList, getQueryPageList, BpelEvent } from '@/api/common';
import { localStorage } from '@/utils/storage'
import { Auditcheckmark, checkmarkAudit, overruleAudit } from '@/api/check'
import { bTableHeight } from "../../../composables/calcTableHeight";
import { getORG } from '@/api/auth'

const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(440);

const queryFormRef = ref(ElForm);
const formDataRef = ref(ElForm);

const { proxy }: any = getCurrentInstance();


const state = reactive({
  ERPFactoryList: [] as any, // 组织
  queryParams: {
    SHIPREQUESTNAME: '',
    CHECKPLANNAME: '',
    PLANDATE : '',
    ERPFACTORY:''
  },
  loading: false,
  list: [],
  list2: [],
  commitDate : '',
  total2: 0,
  pageNum: 1,
  pageSize: 10,
  pageNum2: 1,
  pageSize2: 10,
  column: [] as any,
  column1: [] as any,
  rules: {
    SHIPREQUESTNAME: [{ required: true, message: "请输入盘点计划名", trigger: "blur" }],
  },
  handleSelectionlist : [] as any
});

const { ERPFactoryList,loading, list, list2, column, queryParams, rules, pageNum, pageSize, column1, total2 } = toRefs(state)

onMounted(() => {
  state.column = [

    { header: "计划日期", field: "PLANDATE", hide: false },
    { header: "盘点计划名", field: "CHECKPLANNAME", hide: false },
    { header: "盘点物料编号", field: "MATERIALSPECNAME", hide: false },
    { header: "品名", field: "CN_MATERIALSPECNAME", hide: false },
    // { header: "盘点方式", field: "PLANTYPENAME", hide: false },
    { header: "盘点方式", field: "PLANTYPE", hide: false },
    { header: "仓库", field: "CN_ERPLOCATION", hide: false },
    { header: "货位", field: "CN_LOCATIONNAME", hide: false },
    { header: "计划盘点数量", field: "TOTAL_MATERIALQUANTITY", hide: false },
    { header: "实际盘点数量", field: "MATERIALACTQUANTITY", hide: false },
    { header: "差值", field: "DIFFERENCEQUANTITY", hide: false },
    { header: "是否审核标记", field: "CHECKPLATATE", hide: false },

  ],
    state.column1 = [
      { header: "盘点时间", field: "PLANDATE", hide: false },
      { header: "盘点任务", field: "CHECKPLANNAME", hide: false },
      { header: "盘点方式", field: "PLANTYPE", hide: false },
      // { header: "盘点类型", field: "MOVEFLAG", hide: false },
      { header: "盘点类型", field: "PLANTYPENAME", hide: false },
      { header: "计划数量", field: "JHBOX", hide: false },
      { header: "盘点数量", field: "ALLBOX", hide: false },
    ]
  handleQuery2();
  updateTableHeight();
  ERPFactoryQuery();
  window.addEventListener('resize', handleResize);
});
function handleSelectionChange(val : any) {
  state.handleSelectionlist = val;
}

function audit() {
  let query = {
    CHECKPLANNAME : state.queryParams.CHECKPLANNAME,
    PLANDATE : state.queryParams.PLANDATE,
    commitDate : state.commitDate,
    user : localStorage.get('userId')
  }
  Auditcheckmark(query).then((res : any) => {

    if (res.success) {
      ElMessageBox.alert('审核过账成功', '提醒框', {
        confirmButtonText: 'OK',
      })
      state.list = [];
      state.queryParams.CHECKPLANNAME = '';
      handleQuery2()
    } else {
      ElMessageBox.alert('审核过账失败：' + res.data.message, '提醒框', {
        confirmButtonText: 'OK',
      })
      
    }
  }).catch((err: any) => {
    ElMessageBox.alert('操作失败' + err, '提醒框', {
      confirmButtonText: 'OK',
    })
  });
  
}
//ERP工厂查询
function ERPFactoryQuery() {
  getORG({ orgNo: null })
    .then((res: any) => {
      state.ERPFactoryList = res.resultObj
      state.ERPFactoryList.unshift({
        ERPFACTORY: ''
      })
    })
    .catch(() => {})
}
function handleQuery() {
  queryFormRef.value.validate((isValid: boolean) => {
    if (isValid) {
      getQueryList({
        queryId: "GetCheckPlanInfo1",
        version: "WEB0002",
        params: {
          userId: localStorage.get('userId'),
          SHIPREQUESTNAME: state.queryParams.SHIPREQUESTNAME,
          CHECKPLANNAME: state.queryParams.CHECKPLANNAME,
          PLANDATE : state.queryParams.PLANDATE,
          SITENAME : 'SDK'
        },
      }).then((res: any) => {
        state.list = res.data
        for (var i=0;i<state.list.length;i++){
          if(state.list[i].CHECKPLATATE == null){
            state.list[i].CHECKPLATATE = '是'
          }
        }
      });
    }
  });
}

function resetQuery() {
  // queryFormRef.value.resetFields();
  // handleQuery();
  state.queryParams.CHECKPLANNAME = '';
  state.list = [];
}
function handle(row: any, enent: any, clount: any) {
  state.queryParams.CHECKPLANNAME = row.CHECKPLANNAME
  state.queryParams.PLANDATE = row.PLANDATE
  handleQuery()
}
function handleQueryInfo() {
  state.pageNum2 = 1
  handleQuery2()
}
function handleQuery2() {
  queryFormRef.value.validate((valid: any) => {
    if (valid) {
      state.loading = true;
      let query = {
        queryId: "GetCheckPlanCode",
        // version: "00001",
        version: "00004",
        params: {
          SITENAME: 'SDK',
          CHECKPLANNAME: state.queryParams.CHECKPLANNAME,
          ERPFACTORY : state.queryParams.ERPFACTORY
        },
        pageNum: state.pageNum2,
        pageSize: state.pageSize2,
      }
      getQueryPageList(query).then((res: any) => {
        if(res.data.list.length==0){

          ElMessageBox.alert('查无数据', '提醒框', {
            confirmButtonText: 'OK',
          })
          state.loading = false;
        } else {

          state.loading = false;
          state.list2 = res.data.list;
          state.total2 = res.data.total;
        }
      });
    }
    state.loading = false;
  });
}
</script>

<style scoped lang="scss"></style>