<!--库位查询-->
<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
      <el-row class="page-search" justify="space-between" align="middle">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-row :gutter="20">
          <span class="CommonStyle2">工厂</span>
          <el-col :span="2" style="display: block;" >
            <el-form-item label="" prop="params.valueF">
                <el-select v-model="value" class="m-2" placeholder="Select" size="large">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"

                  />
              </el-select>
            </el-form-item>
          </el-col>

          <span class="CommonStyle">库位名称</span>
          <el-col :span="2" style="display: block;" >
            <el-form-item label="" prop="params.valueF">
                  <el-select v-model="queryParams.params.AREANAME" clearable filterable class="m-F" placeholder="库房" size="Select" @change="selectChange">
                      <el-option
                      v-for="item in storageFList"
                      :key="item.ENUMVALUE"
                      :label="item.ENUMVALUE"
                      :value="item.ENUMVALUE"
                      />
                  </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="display: block;" >
            <el-form-item label="" prop="params.valueS">
                  <el-select v-model="queryParams.params.AREANAMES" clearable filterable class="m-S" placeholder="库区" size="Select" >
                      <el-option
                      v-for="item in storageSList"
                      :key="item.ENUMVALUE"
                      :label="item.ENUMVALUE"
                      :value="item.ENUMVALUE"
                      />
                  </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="display: block;" >
            <el-form-item label="" prop="params.valueZ">
              <el-Input v-model:params="queryParams.params.AREANAMEZ" placeholder="库架" clearable @keyUpEvent="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2" style="display: block;" >
            <el-form-item label="" prop="params.accountX">
                  <el-select v-model="value" clearable class="m-X" placeholder="货位" size="Select" >
                      <el-option
                      v-for="item in siteOption"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                      />
                  </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="handleQuery">搜索</el-button>

      </div>
    </el-row>

    <div class="vue-element-page-table">
      <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <span>供应商信息列表：</span>
        </div>
      </div>
      <el-table
        border
        v-loading="loading"
        :data="pageList"
        highlight-current-row
        row-key="id"
        style="width: 100% "
        height=300px
        @row-click="handle"
      >
        <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 v-if="col.field === 'status'">
                <span v-if="scope.row[col.field]" class="table-status-active">启用</span>
                <span v-else class="table-status-inActive">禁用</span>
              </span>
              <span v-else>{{ scope.row[col.field] }}</span>
            </template>
          </el-table-column>
        </template>


        <template v-slot:empty>
          <svg-icon icon-class="noData" width="160px" height="160px"/>
        </template>
      </el-table>
      <pagination
        v-if="total>0"
        :total="total"
        v-model:pageNumTo="queryParams.pageNum"
        v-model:pageSizeTo="queryParams.pageSize"
        @pagination="handleQuery"
      />
      </div>
      <div class="vue-element-page-table">
      <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <span>BOX信息列表：</span>
        </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  @updateList="updateList" />
          </el-popover>
        </div>
      </div>
      <el-table
        border
        v-loading="loading"
        :data="pageList2"
        highlight-current-row
        row-key="id"
        style="width: 100% "
        height=200px
      >
        <template
          v-for="(col,index) in column2"
          :key="index"
        >
          <el-table-column
            v-if="!col.hide"
            :label="col.header"
            show-overflow-tooltip
          >
            <template #default="scope">
              <span v-if="col.field === 'status'">
                <span v-if="scope.row[col.field]" class="table-status-active">启用</span>
                <span v-else class="table-status-inActive">禁用</span>
              </span>
              <span v-else>{{ scope.row[col.field] }}</span>
            </template>
          </el-table-column>
        </template>


        <template v-slot:empty>
          <svg-icon icon-class="noData" width="160px" height="160px"/>
        </template>
      </el-table>
      </div>

    </div>
    <!--  新增或编辑  -->
    <el-drawer
      :title="dialog.title"
      v-model="dialog.visible"
      @close="cancel"
      @opened="dialogOpend"
    >
      <el-form
        ref="formDataRef"
        :model="formData"
        :rules="rules"
        label-position="top"
      >

        <el-form-item label="操作类型" prop="OperationType">
          <el-input v-model="formData.account" placeholder="请输入操作类型" />
        </el-form-item>
        <el-form-item label="位置" prop="site" >
          <el-input v-model="formData.name"  style="width: 60px" />
          <el-input v-model="formData.name"  style="width: 60px"/>
          <el-input v-model="formData.name"  style="width: 60px" />
          <el-input v-model="formData.name"  style="width: 60px"/>
          <el-input v-model="formData.name"  style="width: 60px"/>
          <el-input v-model="formData.name"  style="width: 60px"/>
          <el-input v-model="formData.name"  style="width: 60px"/>
          <el-input v-model="formData.name"  style="width: 60px"/>
        </el-form-item>
        <el-form-item label="工厂" prop="factory">
          <el-input name="password" show-password v-model="formData.pwd" placeholder="请输入工厂" />
        </el-form-item>
        <el-form-item label="容量" prop="phone">
          <el-input v-model="formData.phone" placeholder="容量描述不可以为空" />
        </el-form-item>
        <el-form-item label="资源状态" prop="email">
          <el-input v-model="formData.email" placeholder="资源状态描述不可以为空" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="PostCode">
          <el-input v-model="formData.jobNo" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="库位名称" prop="city">
          <el-input v-model="formData.jobNo" placeholder="库位名称不为空" />
        </el-form-item>
        <el-form-item label="库位使用状态" prop="phone">
          <el-input v-model="formData.jobNo" placeholder="库位使用状态不为空" />
        </el-form-item>

      </el-form>

      <template #footer>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-drawer>
  </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'WareHouseLocationQuery'
};
</script>

<script lang="ts" setup>
import {reactive, ref, onMounted, toRefs, getCurrentInstance, nextTick} from 'vue';
import {ElForm, ElMessageBox, ElTable} from 'element-plus';
import { getQueryList,getQueryPageList } from '@/api/common';
import { addStorage, updateStorage, deleteStorage } from '@/api/basics/storage';

const queryFormRef = ref(ElForm);
const formDataRef = ref(ElForm);
const { proxy }: any = getCurrentInstance();

const roleTableRef = ref<InstanceType<typeof ElTable>>();
const value = ref('')
const options = [
{
  value: 'SITENAME',
  label: localStorage.getItem('siteName')||'',
}
]
const siteOption=[
  {
      value: 'A',
      label: 'A',
  },
  {
      value: 'F',
      label: 'F',
  }
]

const state = reactive({
  queryParams: {
    queryId: 'GetStorageSpec',
    version: 'web02',
    params: {
      AREANAME:'',
      AREANAMES:'',
      AREANAMEZ:'',
      AREANAMEX:'',
      SITENAME:'PH'
    },
     pageNum: 1,
     pageSize: 10
  },
  queryParams2: {
    queryId: 'GetBoxList',
    version: 'WEB10001',
    params: {
      SUPERPROCESSGROUPNAME:'',
      SITENAME:localStorage.getItem('userName')||''
    },
    // pageNum: 1,
    // pageSize: 10
  },
  storageFParams: {
    queryId: 'GetStorage',
    version: 'web01',
    params: {
      SITENAME:'PH'
    }
  },
  storageSParams: {
    queryId: 'GetStorage',
    version: 'web02',
    params: {
      AREANAME:'',
      SITENAME:'PH'
    }
  },

  total: 0,
  loading: false,
  pageList: [] as any,
  pageList2: [] as any,
  dialog: {
    title: '新增库位',
    visible: false
  },
  formData: {
    account: '',
    name: '',
    pwd: '',
    phone: '',
    email: '',
    jobNo: ''
  } as any,
  rules: {
    account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
    pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  },
  column: [] as any,
  column2: [] as any,
  selectItem: undefined as any,
  currentTime: '',
  storageFList: [] as any,
  storageSList: [] as any,
  valueF: undefined as any,
  values: undefined as any
})
const {
  queryParams,
  queryParams2,
  total,
  loading,
  pageList,
  pageList2,
  dialog,
  formData,
  rules,
  column,
  column2,
  selectItem,
  currentTime,
  storageFList,
  storageFParams,
  storageSList,
  storageSParams,
  valueF,
  values
} = toRefs(state);

onMounted(() => {
  state.column = [
    // { header: '操作类型',field:'OperationType',hide: false },
    { header: '工厂',field:'SITENAME',hide: false },
    { header: '用途',field:'STORAGEUSETYPE',hide: false },
    // { header: 'Content type',field:'ConType',hide: false },
    { header: '库位名称',field:'STORAGENAME',hide: false },

    // { header: 'Z',field:'ZCode',hide: false },
    { header: '容量',field:'CAPACITY',hide: false },
    { header: '使用状态',field:'USESTATE',hide: false },
    // { header: '物料描述',field:'MatDes',hide: false },
    { header: '创建人',field:'CREATEUSER',hide: false },
    { header: '创建时间',field:'CREATETIME',hide: false },
    { header: 'Packing Name',field:'PACKINGNAME',hide: false },
    { header: '最后操作名称',field:'LASTEVENTNAME',hide: false },
    { header: '最后事件的时间点',field:'LASTEVENTTIMEKEY',hide: false },
    { header: '最后操作时间',field:'LASTEVENTTIME',hide: false },
    { header: '最后操作员',field:'LASTEVENTUSER',hide: false },
    { header: '最后的操作注解',field:'LASTEVENTCOMMENT',hide: false },
    { header: 'Reason Code类型',field:'REASONCODETYPE',hide: false },
    { header: 'Reason Code',field:'REASONCODE',hide: false },

  ]

  state.column2=[
    { header: '工厂',field:'SITENAME',hide: false },
    { header: 'BOXID',field:'MATERIALPACKINGNAME',hide: false },
    { header: 'ERP工厂',field:'ERPFACTORY',hide: false },
    { header: '物料编号',field:'MATERIALSPECNAME',hide: false },
    { header: '中文描述',field:'DESC_CN',hide: false },
    { header: '物料总数量',field:'MATERIALQUANTITY',hide: false },
    { header: '位置',field:'LOCATIONNAME',hide: false },
    { header: 'BOX类型',field:'MATERIALTYPE',hide: false },
    { header: '物料类型',field:'CONTENTMATERIALTYPE',hide: false },
    { header: 'Hold状态',field:'HOLDSTATE',hide: false },
  ]
  // ,
  // state.pageList = [
  //   {
  //     OperationType: '锻造',
  //     factory: 'B9'
  //   },
  //   {
  //     OperationType: '段造',
  //     factory: 'B9'
  //   }
  // ]
  // handleQuery();
  storageFQuery();
  // storageXQuery();
});
// 更新列
function updateList(col: any) {
  state.column = col;
}
//点击触发下面表********
 function handle(row:any, event:any, column:any) {
        state.queryParams2.params.SUPERPROCESSGROUPNAME= row.STORAGENAME;
        getQueryList(state.queryParams2)
        .then((res:any)=>{
           console.log(res.data[0]);
           state.pageList2 = res.data;
           // state.total = res.data.total;
           showCurrentTime();
        })
     }
//*****
function handleQuery() {
  if(!state.queryParams.params.AREANAME){
    proxy.$ElMessage.warning('库房不能为空！');
    return;
  }
  if(!state.queryParams.params.AREANAMES){
    proxy.$ElMessage.warning('库区不能为空！');
    return;
  }
  getQueryPageList(state.queryParams)
    .then((res:any)=>{
      state.pageList = res.data.list;
      //state.total=res.total;
      state.total = res.data.total;
      showCurrentTime();
    })
    .catch(() => {});
}
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}
function showCurrentTime() {
  let date = new Date()
  state.currentTime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ":" +
    (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ":" + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
}
function storageFQuery() {
  getQueryList(state.storageFParams)
    .then((res:any)=>{
      console.log(res.data);
      state.storageFList = res.data;
    })
    .catch(() => {});
}
function storageXQuery() {
  getQueryList(state.storageSParams)
    .then((res:any)=>{
      console.log(res.data);
      state.storageSList = res.data;
    })
    .catch(() => {});
}
function selectChange() {
  state.storageSParams.params.AREANAME = state.queryParams.params.AREANAME;
  storageXQuery();
}

/**
 * 新增 dialog
 */
function handleAdd() {
  selectItem.value = undefined;
  dialog.value = {
    title: '新增库位',
    visible: true
  };
}

/**
 * 删除 dialog
 */
 function handleDelete(row: any) {

  const params = {
    siteName: row.SITENAME
  } as any;
  deleteStorage(params).then((res: any) => {
    if (res.code === '0'){
      proxy.$ElMessage.success('删除成功');
    }
    handleQuery();
  });
}
/**
 * 修改库位 dialog
 */
function handleUpdate(row: any) {
  state.dialog = {
    title: '修改库位',
    visible: true
  };
  state.selectItem = row;
}

function dialogOpend() {
  formDataRef.value.resetFields();
  if(state.selectItem) {
    state.formData = {
      account: state.selectItem.account,
      name: state.selectItem.name,
      pwd: state.selectItem.pwd,
      phone: state.selectItem.phone,
      email: state.selectItem.email,
      jobNo: state.selectItem.jobNo,
    }
  }
}

/**
 * 取消关闭弹窗
 */
function cancel() {
  state.dialog.visible = false;
}

/**
 * 保存
 */
function submitForm() {
  formDataRef.value.validate((isValid: boolean) => {
    if (isValid) {
      if (state.selectItem) {// 编辑
        let params = {
          dataId: state.selectItem.dataId,
          ...state.formData
        }
        updateStorage(params).then((res: any) => {
          if (res.code === '0'){
            proxy.$ElMessage.success('修改成功');
          }
          cancel();
          handleQuery();
        });
      } else {
        addStorage(state.formData).then((res: any) => {

          if (res.code === '0'){
            proxy.$ElMessage.success('新增成功');
          }

          cancel();
          handleQuery();
        });
      }
    }
  });
}
</script>

<style scoped lang="scss">
.CommonStyle{
  width: 48px;
  height: 20px;
  font-family: PingFangSC-Regular;
  font-size: 12px;
  color: #323233;
  text-align: right;
  line-height: 30px;
  font-weight: 400;
}
.CommonStyle2{
  width: 24px;
  height: 20px;
  font-family: PingFangSC-Regular;
  font-size: 12px;
  color: #323233;
  text-align: left;
  line-height: 30px;
  font-weight: 400;
}
</style>
