<!--发货单据查询-->
<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="CommonStyle">物料预留单</span>
          <el-col :span="6">
            <el-form-item label="" prop="params.account">
              <el-Input v-model:params="queryParams.params.SHIPREQUESTNAME" placeholder="物料预留单" clearable
                @keyUpEvent="handleQuery" />
            </el-form-item>
          </el-col>
          <span class="CommonStyle">交付类型</span>
          <el-col :span="3">
            <el-form-item label="" prop="params.account">
              <el-select v-model="queryParams.params.DELIVERYTYPE" class="m-2" placeholder="" size="small">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="13"></el-col>
          <span class="CommonStyle">移动类型</span>
          <el-col :span="6">
            <el-form-item label="" prop="params.account">
              <el-Input v-model:params="queryParams.params.SHIPREQUESTDETAILTYPE" placeholder="移动类型" clearable
                @keyUpEvent="handleQuery" />
            </el-form-item>
          </el-col>

          <span class="CommonStyle2">预留计划时间</span>
          <el-col :span="6">
            <el-form-item label="" prop="params.account">
              <el-date-picker v-model="queryParams.params.DateValue" type="daterange" unlink-panels range-separator="To"
                v-model:start-placeholder="queryParams.params.FROMDATE"
                v-model:end-placeholder="queryParams.params.TODATE" :shortcuts="shortcuts2" :size="size" />
            </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 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="state.column1" @updateList="updateList" />
            </el-popover>
          </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="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 :list="state.column1" @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>
</template>

<script lang="ts">
export default {
  name: 'materialShipRequestQuery'
};
</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';
import { msToDate } from '@/utils/CommonUtil';
const queryFormRef = ref(ElForm);
const formDataRef = ref(ElForm);
const { proxy }: any = getCurrentInstance();

const roleTableRef = ref<InstanceType<typeof ElTable>>();
const value = ref('')

const options = [
  {
    value: 'Reserve',
    label: 'Reserve',
  },
  {
    value: 'Delivery',
    label: 'Delivery',
  },
  {
    value: 'WO',
    label: 'WO',
  },
  {
    value: 'Scrap',
    label: 'Scrap',
  }
]
const siteOption = [
  {
    value: 'A',
    label: 'A',
  },
  {
    value: 'F',
    label: 'F',
  }
]

const state = reactive({
  queryParams: {
    queryId: 'GetMaterialShipRequestList',
    version: 'WEB10001',
    params: {
      SHIPREQUESTNAME: '',
      DELIVERYTYPE: '',
      FROMDATE: '',
      TODATE: '',
      DateValue: '',
      SHIPREQUESTDETAILTYPE: '',
      SITENAME: localStorage.getItem('siteName') || ''
    },
    pageNum: 1,
    pageSize: 10
  },
  queryParams2: {
    queryId: 'GetMaterialSpecList',
    version: 'WEB10001',
    params: {
      SHIPREQUESTNAME: '',
      SITENAME: localStorage.getItem('siteName') || ''
    },
    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,
  column1: [] 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: 'SHIPREQUESTNAME', hide: false },
    // { header: 'Content type',field:'ConType',hide: false },
    { header: 'Ship状态', field: 'SHIPREQUESTSTATE', hide: false },

    // { header: 'Z',field:'ZCode',hide: false },
    { header: 'Ship类型', field: 'SHIPREQUESTTYPE', hide: false },
    { header: '移动类型', field: 'SHIPREQUESTDETAILTYPE', hide: false },
    // { header: '物料描述',field:'MatDes',hide: false },
    { header: '客户编号', field: 'CUSTOMERNO', hide: false },
    { header: '交付类型', field: 'DELIVERYTYPE', hide: false },
    { header: 'ERP工厂名称', field: 'ERPFACTORYNAME', hide: false },
    { header: '接收方', field: 'RECEIVER', hide: false },
    { header: '最后操作名称', field: 'LASTEVENTNAME', hide: false },
    { header: '最后操作时间', field: 'LASTEVENTTIME', hide: false },
    { header: '最后操作人员', field: 'LASTEVENTUSER', hide: false },



  ]

  state.column2 = [
    { header: '工厂', field: 'SITENAME', hide: false },
    { header: '发货单号', field: 'SHIPREQUESTNAME', hide: false },
    { header: '行项', field: 'SHIPREQUESTDETAILNAME', hide: false },
    { header: '物料编号', field: 'MATERIALSPECNAME', hide: false },
    { header: 'ERP工厂名称', field: 'ERPFACTORYNAME', hide: false },
    { header: 'ERP库位', field: 'ERPLOCATION', hide: false },
    { header: '级别', field: 'GRADE', hide: false },
    { header: '进口/出口', field: 'DOMESTICEXPORT', hide: false },
    { header: '请求数量', field: 'REQUESTQUANTITY', hide: false },
    { header: '绑定数量', field: 'ASSIGNEDQUANTITY', hide: false }

  ]
  // ,
  // state.pageList = [
  //   {
  //     OperationType: '锻造',
  //     factory: 'B9'
  //   },
  //   {
  //     OperationType: '段造',
  //     factory: 'B9'
  //   }
  // ]
  // handleQuery();
  storageFQuery();
  // storageXQuery();
});
//formdata时间设置
const size = ref<'default' | 'large' | 'small'>('default');
//时间范围函数
const shortcuts2 = [
  {
    text: '',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
// 更新列
function updateList(col: any) {
  state.column = col;
}
//点击触发下面表********
function handle(row: any, event: any, column: any) {
  state.queryParams2.params.SHIPREQUESTNAME = row.SHIPREQUESTNAME;
  getQueryPageList(state.queryParams2)
    .then((res: any) => {
      console.log(res.data);
      state.pageList2 = res.data.list;
      // state.total = res.data.total;
      showCurrentTime();
    })
}
//*****
function handleQuery() {
  let arr = state.queryParams.params.DateValue.toString().split(",");
  let str1 = msToDate(arr[0]);
  let str2 = msToDate(arr[1]);
  state.queryParams.params.FROMDATE = str1.withoutTime;
  state.queryParams.params.TODATE = str2.withoutTime;
  console.log(state.queryParams.params);
  getQueryPageList(state.queryParams)
    .then((res: any) => {

      state.pageList = res.data.list;
      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: 60px;
  height: 20px;
  font-family: PingFangSC-Regular;
  font-size: 12px;
  color: #323233;
  text-align: right;
  line-height: 30px;
  font-weight: 400;
}

.CommonStyle2 {
  width: 72px;
  height: 20px;
  font-family: PingFangSC-Regular;
  font-size: 12px;
  color: #323233;
  text-align: left;
  line-height: 30px;
  font-weight: 400;
}
</style>
