<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <el-form :inline="true" @submit.native.prevent style="background-color: #fff;padding: 10px;">
      <el-row  >
        <el-col :span="6" class="col_height">
          <el-form-item label="组织" prop="params.ERPFACTORY">
            <el-select filterable v-model="queryParams.params.ERPFACTORY" placeholder="下拉选择组织" style="width: 240px">
              <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="14" class="col_height">
          <el-form-item label="条件选择">
            <el-radio-group v-model="selectedCondition" @change="handleConditionChange">
              <el-radio-button label="料号">料号</el-radio-button>
              <el-radio-button label="料号+仓库">料号+仓库</el-radio-button>
              <el-radio-button label="料号+仓库+货位">料号+仓库+货位</el-radio-button>
              <el-radio-button label="仓库">仓库</el-radio-button>
              <el-radio-button label="仓库+货位">仓库+货位</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="4" class="col_height">
          <el-button type="primary" @click="fetchData" :disabled="isLoading" >查询</el-button>
        </el-col>
        <el-col :span="4" class="col_height" v-if = "v_erplocation">
          <el-form-item label="仓库" prop="params.ERPLOCATION">
            <el-Input v-model="queryParams.params.ERPLOCATION" placeholder="输入仓库" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="4" class="col_height" v-if = "v_locationname">
          <el-form-item label="货位" prop="params.LOCATIONNAME" >
            <el-Input v-model="queryParams.params.LOCATIONNAME" placeholder="输入货位" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="4" class="col_height" v-if = "v_materialsepcname">
          <el-form-item label="物料编号" prop="params.MATERIALSPECNAME">
            <el-Input v-model="queryParams.params.MATERIALSPECNAME" placeholder="输入物料编号" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :height="tableHeight" :data="tableData" style="width: 100% ;height: 100%;padding:10px" border  v-loading="loading" >
      <el-table-column
              v-for="(column, index) in tableColumns"
              :key="index"
              :prop="column.prop"
              :label="column.label"
              :row-class-name="getRowClassName"
      >
      <template #default="scope">
        <span v-if="column.prop === 'DIFF'">
          <span style="background-color: red;padding: 8px;" v-if="!scope.row[column.prop]" class="table-status-active">有差异</span>
          <span style="background-color: green;padding: 8px;" v-else class="table-status-inActive">无差异</span>
        </span>
        <span v-else>{{ scope.row[column.prop] }}</span>
      </template>
    </el-table-column>
    </el-table>
    <div>
      <pagination v-if="total > 0" :total="total" v-model:pageNumTo="queryParams.pageNum"
                  :page-sizes="[20, 30, 50, 100,200]"
                  v-model:pageSizeTo="queryParams.pageSize" @pagination="fetchData" />
    </div>
  </div>
</template>
<script>
  import { getORG } from '@/api/auth';
  import {onMounted} from "vue";
  import {getQueryAccountKBList, getQueryPageList} from "../../../../api/common";
  import { bTableHeight } from "../../../../composables/calcTableHeight";

  const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(260);
  
  onMounted(() => {
  updateTableHeight();
    window.addEventListener('resize', handleResize);
  })
  export default {
    name: "DiffAccount",
    data() {
      return {
        loading : false,
        isLoading : false,
        selectedCondition: '料号',
        v_erplocation : false,
        v_locationname : false,
        v_materialsepcname : true,
        ERPFactoryList: [] ,
        total: 0,
        tableColumns: [
          { prop: 'NC_MATERIALSPECNAME', label: 'NC_料号' },
          { prop: 'WMS_MATERIALSPECNAME', label: 'WMS_料号' },
          { prop: 'NC_SEPCNAME', label: 'NC_规格' },
          { prop: 'WMS_SPECNAME', label: 'WMS_规格' },
          { prop: 'NC_PHASE', label: 'NC_阶段' },
          { prop: 'WMS_PHASE', label: 'WMS_阶段' },
          { prop: 'NC_MATERIALQUANTITY', label: 'NC_数量' },
          { prop: 'WMS_MATERIALQUANTITY', label: 'WMS_数量' },
          { prop: 'DIFF', label: '比对结果' }
        ],
        queryParams: {
          queryId: "",
          version: "",
          params: {
            MATERIALSPECNAME: "",
            ERPFACTORY: '00011110000000002ODP',
            ERPLOCATION: '',
            LOCATIONNAME: ''
          },
          pageNum: 1,
          pageSize: 20
        },
        queryParamsERPFactory: {
          queryId: "GetErpFactoryList",
          version: "WEB00001",
          params: {
            SITENAME: localStorage.getItem("siteName") || 'SDK',
          },
          pageNum: 1,
          pageSize: 10
        },
        tableData: [],
        ERPFactoryList : [
          {ERPFACTORY : '00011110000000002ODP', DESCRIPTION : '101'},
          {ERPFACTORY : '00011110000000002OFN', DESCRIPTION : '102'}
        ]
      };
    },
    
    methods: {
      getRowClassName({ row, rowIndex }) {
        console.log(row.DIFF)
        // 根据 DIFF 的值返回不同的 CSS 类名
        if (row.DIFF === 0) {
          return 'true-diff';
        } else {
          return 'false-diff';
        }
      },
        reset () {
          this.tableData = []
          this.total = 0
        this.queryParams.params.ERPLOCATION = ''
        this.queryParams.params.LOCATIONNAME = ''
        this.queryParams.params.MATERIALSPECNAME = ''
      },
      handleConditionChange(value) {
        // 根据选择条件动态调整表头和数据
        if (value === '料号') {
          this.tableColumns = [
            { prop: 'NC_MATERIALSPECNAME', label: 'NC_料号' },
            { prop: 'WMS_MATERIALSPECNAME', label: 'WMS_料号' },
            { prop: 'NC_SEPCNAME', label: 'NC_规格' },
            { prop: 'WMS_SPECNAME', label: 'WMS_规格' },
            { prop: 'NC_PHASE', label: 'NC_阶段' },
            { prop: 'WMS_PHASE', label: 'WMS_阶段' },
            { prop: 'NC_MATERIALQUANTITY', label: 'NC_数量' },
            { prop: 'WMS_MATERIALQUANTITY', label: 'WMS_数量' },
            { prop: 'DIFF', label: '比对结果' }
          ];
          this.reset()
          this.v_erplocation  = false
          this.v_locationname = false
          this.v_materialsepcname = true
        } else if (value === '料号+仓库') {
          this.tableColumns = [
            { prop: 'NC_MATERIALSPECNAME', label: 'NC_料号' },
            { prop: 'WMS_MATERIALSPECNAME', label: 'WMS_料号' },
            { prop: 'NC_ERPLOCATION', label: 'NC_仓库' },
            { prop: 'WMS_ERPLOCATION', label: 'WMS_仓库' },
            { prop: 'NC_SEPCNAME', label: 'NC_规格' },
            { prop: 'WMS_SPECNAME', label: 'WMS_规格' },
            { prop: 'NC_PHASE', label: 'NC_阶段' },
            { prop: 'WMS_PHASE', label: 'WMS_阶段' },
            { prop: 'NC_MATERIALQUANTITY', label: 'NC_数量' },
            { prop: 'WMS_MATERIALQUANTITY', label: 'WMS_数量' },
            { prop: 'DIFF', label: '比对结果' }
          ];
          this.reset()
          this.v_erplocation  = true
          this.v_locationname = false
          this.v_materialsepcname = true
        } else if (value === '料号+仓库+货位') {
          this.tableColumns = [
            { prop: 'NC_MATERIALSPECNAME', label: 'NC_料号' },
            { prop: 'WMS_MATERIALSPECNAME', label: 'WMS_料号' },
            { prop: 'NC_ERPLOCATION', label: 'NC_仓库' },
            { prop: 'WMS_ERPLOCATION', label: 'WMS_仓库' },
            { prop: 'NC_LOCATIONNAME', label: 'NC_货位' },
            { prop: 'WMS_LOCATIONNAME', label: 'WMS_货位' },
            { prop: 'NC_SEPCNAME', label: 'NC_规格' },
            { prop: 'WMS_SPECNAME', label: 'WMS_规格' },
            { prop: 'NC_PHASE', label: 'NC_阶段' },
            { prop: 'WMS_PHASE', label: 'WMS_阶段' },
            { prop: 'NC_MATERIALQUANTITY', label: 'NC_数量' },
            { prop: 'WMS_MATERIALQUANTITY', label: 'WMS_数量' },
            { prop: 'DIFF', label: '比对结果' }
          ];
          this.reset()
          this.v_erplocation  = true
          this. v_locationname = true
          this. v_materialsepcname = true
        } else if (value === '仓库') {
          this.tableColumns = [
            { prop: 'NC_ERPLOCATION', label: 'NC_仓库' },
            { prop: 'WMS_ERPLOCATION', label: 'WMS_仓库' },
            { prop: 'NC_MATERIALQUANTITY', label: 'NC_数量' },
            { prop: 'WMS_MATERIALQUANTITY', label: 'WMS_数量' },
            { prop: 'DIFF', label: '比对结果' }
          ];
          this.reset()
          this.v_erplocation  = true
          this. v_locationname = false
          this. v_materialsepcname = false
        } else if (value === '仓库+货位') {
          this.tableColumns = [
            { prop: 'NC_ERPLOCATION', label: 'NC_仓库' },
            { prop: 'WMS_ERPLOCATION', label: 'WMS_仓库' },
            { prop: 'NC_LOCATIONNAME', label: 'NC_货位' },
            { prop: 'WMS_LOCATIONNAME', label: 'WMS_货位' },
            { prop: 'NC_MATERIALQUANTITY', label: 'NC_数量' },
            { prop: 'WMS_MATERIALQUANTITY', label: 'WMS_数量' },
            { prop: 'DIFF', label: '比对结果' }
          ];
          this.reset()
          this.v_erplocation  = true
          this. v_locationname = true
          this. v_materialsepcname = false
        }
      },
      async fetchData() {
        this.loading = true
        this.isLoading = true
        this.queryParams.queryId = "getNC_WMS_DIFF"
        // 根据选择条件过滤数据
        if (this.selectedCondition === '料号') {
          this.queryParams.version = "lh"
        } else if (this.selectedCondition === '料号+仓库') {
          this.queryParams.version = "lh+ck"
        } else if (this.selectedCondition === '料号+仓库+货位') {
          this.queryParams.version = "lh+ck_hw"
        } else if (this.selectedCondition === '仓库') {
          this.queryParams.version = "ck"
        } else if (this.selectedCondition === '仓库+货位') {
          this.queryParams.version = "ck+hw"
        }

        // getQueryAccountKBList(this.queryParams)
        getQueryAccountKBList(this.queryParams)
                .then((res) => {
                  if (res.data.list.length > 0) {
                    this.loading = false
                    this.isLoading = false
                    this.tableData = res.data.list
                    this.total = res.data.total
                  } else {
                    this.loading = false
                    this.isLoading = false
                    this.tableData = res.data.list
                    this.total = res.data.total
                    proxy.$ElMessage.success('查询结果为空');
                  }

                }) .catch(() => {
          this.loading = false
          this.isLoading = false
        });
      }
    }
  };
</script>

<style scoped>
  /* 添加一些样式 */
  .true-diff {
    background-color: darkred ;
    color : red;
  }

  .false-diff {
    background-color: #1ab249;
    color : green
  }
</style>