<!--物料主数据修改历史-->
<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">
          <el-col :span="6">
            <el-form-item label="" prop="params.account">
              <el-Input v-model:params="queryParams.params.MATERIALPACKINGNAME" placeholder="BOX ID" clearable
                @keyUpEvent="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2"></el-col>
          <span class="CommonStyle">事件发生时间</span>
          <el-col :span="9">
            <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="column1" @updateList="updateList" />
            </el-popover>
          </div>
        </div>
        <el-table border v-loading="loading" :data="state.userList" highlight-current-row row-key="id"
          style="width: 100%">
          <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>

          <el-table-column width="160" label="操作" align="center" class-name="table-operation" fixed="right">
            <!--<template #default="scope">
            <el-button type="primary" link @click.stop="handleUpdate(scope.row)" v-if="!scope.row.status">
              修改
            </el-button>
            <div class="splitLine" v-if="!scope.row.status" v-has-perm="['sys:user:enable','sys:user:disable']"></div>
            <el-button type="primary" link v-if="!scope.row.status" @click="handleDel(scope.row)">
              删除
            </el-button>
          </template>-->
          </el-table-column>
          <template v-slot:empty>
            <svg-icon icon-class="noData" width="160px" height="160px" />
          </template>
        </el-table>
      </div>
      <pagination v-if="total > 0" :total="total" v-model:pageNumTo="queryParams.pageNum"
        v-model:pageSizeTo="queryParams.pageSize" @pagination="handleQuery" />
    </div>
    <!--  新增或编辑  -->
    <el-drawer :title="dialog.title" v-model="dialog.visible" @close="cancel">
      <el-form ref="formDataRef" :model="formData" :rules="rules" label-position="top">

        <el-form-item label="供应商编号" prop="SUPPLIERNO">
          <el-input v-model="formData.SUPPLIERNO" placeholder="请输入供应商编号" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="SUPPLIERNAME">
          <el-input v-model="formData.SUPPLIERNAME" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="National NO" prop="NATIONALNO">
          <el-input v-model="formData.NATIONALNO" placeholder="请输入National NO" />
        </el-form-item>
        <el-form-item label="物料描述" prop="DESCRIPTION">
          <el-input v-model="formData.DESCRIPTION" placeholder="物料描述不可以为空" />
        </el-form-item>
        <el-form-item label="地址" prop="ADDRESS">
          <el-input v-model="formData.ADDRESS" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="POSTCODE">
          <el-input v-model="formData.POSTCODE" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="城市" prop="CITY">
          <el-input v-model="formData.CITY" placeholder="城市不为空" />
        </el-form-item>
        <el-form-item label="电话" prop="TEL">
          <el-input v-model="formData.TEL" placeholder="请注意格式" />
        </el-form-item>
        <el-form-item label="传真" prop="FAX">
          <el-input v-model="formData.FAX" placeholder="传真很重要" />
        </el-form-item>
        <el-form-item label="Incharge" prop="INCHARGE">
          <el-input v-model="formData.INCHARGE" placeholder="Incharge不为空" />
        </el-form-item>
        <el-form-item label="邮箱" prop="EMAIL">
          <el-input v-model="formData.EMAIL" placeholder="请注意邮箱" />
        </el-form-item>
        <el-form-item label="最后操作时间" prop="LASTEVENTTIME">
          <el-date-picker v-model="formData.LASTEVENTTIME" type="date" placeholder="Pick a day"
            :disabled-date="disabledDate" :shortcuts="shortcuts" :size="size" />

        </el-form-item>

      </el-form>

      <template #footer>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-drawer>
    <!--  分配角色  -->
    <el-drawer v-model="roleDialog.visible" @close="cancelRoleDialog" @opened="roleDialogOpend"
      class="drawer-show-table" modal-class="userRoleModel">
      <template #header>
        <div class="drawer-show-table-title">
          <svg-icon icon-class="close" width="20px" height="20px" @click="cancelRoleDialog" />
          <span>{{ roleDialog.title }}</span>
        </div>
        <el-button type="primary" @click="">确 定</el-button>
      </template>
      <el-table border ref="roleTableRef" :data="allRole" highlight-current-row row-key="dataId" style="width: 100%">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column prop="code" label="角色编码" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" label="角色名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
      </el-table>
    </el-drawer>
  </div>
</template>

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

<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance, nextTick } from 'vue';
import { ElForm, ElMessageBox, ElTable } from 'element-plus';
import { getRSAPublic } from '@/api/auth';
import { JSEncrypt } from 'jsencrypt';
import { getQueryList, getQueryPageList } from '@/api/common';
import { SupplierAdd, SupplierDel, SupplierUpd } from '@/api/basics/supplier'
import { msToDate } from '@/utils/CommonUtil';
const value2 = ref('')



const queryFormRef = ref(ElForm);
const formDataRef = ref(ElForm);
const { proxy }: any = getCurrentInstance();
let statuButton = 0;
const roleTableRef = ref<InstanceType<typeof ElTable>>();

//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]
    },
  },
]
const state = reactive({
  rsa: '',
  queryParams: {
    queryId: 'GetMaterialBoxHistoryList',
    version: 'WEB00001',
    params: {
      MATERIALPACKINGNAME: '',
      FROMDATE: '',
      TODATE: '',
      DateValue: '',
      SITENAME: localStorage.getItem('siteName') || ''
    },
    pageNum: 1,
    pageSize: 10
  },
  total: 0,
  loading: false,
  userList: [] as any,
  dialog: {
    title: '新增用户',
    visible: false
  },

  formData: {
    SUPPLIERNO: '',
    SUPPLIERNAME: '',
    NATIONALNO: '',
    DESCRIPTION: '',
    ADDRESS: '',
    POSTCODE: '',
    CITY: '',
    TEL: '',
    FAX: '',
    INCHARGE: '',
    EMAIL: '',
    LASTEVENTTIME: '',
  } as any,
  rules: {
    account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
    pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  },
  column: [] as any,
  selectItem: undefined as any,
  currentTime: '',
  roleDialog: {
    title: '分配角色',
    visible: false
  },
  allRole: [] as any

})
const {
  rsa,
  queryParams,
  total,
  loading,
  userList,
  dialog,
  formData,
  rules,
  column,
  selectItem,
  currentTime,
  roleDialog,
  allRole,
} = toRefs(state);

onMounted(() => {
  state.column = [
    { header: '工厂', field: 'SITENAME', hide: false },
    { header: 'BOXID', field: 'MATERIALPACKINGNAME', hide: false },
    { header: '物料编号', field: 'MATERIALPACKINGNAME', hide: false },
    { header: '最后操作名称', field: 'LASTEVENTNAME ', hide: false },
    { header: '最后操作时间', field: 'LASTEVENTTIME', hide: false },
    { header: '最后的操作员', field: 'LASTEVENTUSER', hide: false },
    { header: '最后操作的注解', field: 'LASTEVENTCOMMENT ', hide: false },
    { header: '生产日期', field: 'MAKEDATE', hide: false },
    { header: 'Packing状态', field: 'PACKINGSTATE', hide: false },
    { header: 'ERP库位', field: 'ERPLOCATION', hide: false },


  ]
  //handleQuery();
  //getRSA();
});


const shortcuts = [
  {
    text: 'Today',
    value: new Date(),
  },
  {
    text: 'Yesterday',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24)
      return date
    },
  },
  {
    text: 'A week ago',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
      return date
    },
  },
]

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}
// 更新列
function updateList(col: any) {
  state.column = col;
}
function getRSA() {
  getRSAPublic().then((res: any) => {
    state.rsa = res.data;
  }).catch(() => { });
}


function handleQuery() {
  state.userList = [];
  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;
  //state.queryParams.params.SITENAME=localStorage.getItem;
  //console.log(state.queryParams.params.MATERIALPACKINGNAME+","+state.queryParams.params.FROMDATE+","+state.queryParams.params.TODATE+","+state.queryParams.params.DateValue);
  getQueryPageList(state.queryParams)
    .then((res: any) => {
      for (let i = 0; i < res.data.list.length; i++) {
        var TemporArr = {};
        TemporArr = { ...res.data.list[i] };
        state.userList.push(TemporArr);
        showCurrentTime();
      }
      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 enable(row: any) {
  state.selectItem = row;
  ElMessageBox.confirm(
    '确认启用?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
    })
    .catch(() => {

    })
}
/**
 *  禁用
 */
function disable(row: any) {
  state.selectItem = row;
  ElMessageBox.confirm(
    '确认禁用?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
    })
    .catch(() => {

    })
}

/**
 * 新增供应商用户 dialog
 */
function handleAdd() {
  selectItem.value = undefined;
  dialog.value = {
    title: '新增供应商',
    visible: true
  };
  state.rules.pwd[0].required = true;
  statuButton = 2;
}

/**
 * 删除供应商用户 dialog
 */
function handleDel(row: any) {
  SupplierDel(row)
    .then((res: any) => {
      showCurrentTime();
      handleQuery();
    }).then(() => proxy.$ElMessage.success('删除成功'))
    .catch(() => { });
}

/**
 * 修改供应商用户 dialog
 */
function handleUpd() {

}
/**
 * 编辑用户 dialog
 */
function handleUpdate(row: any) {
  state.dialog = {
    title: '编辑供应商',
    visible: true
  };
  state.selectItem = row;
  state.rules.pwd[0].required = false;
  statuButton = 1;
}



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

/**
 * 保存
 */
function submitForm() {

  switch (statuButton) {
    case 0: break;
    case 1: SupplierUpd(state.formData)
      .then((res: any) => {

        dialog.value.visible = false;
        handleQuery();
      }).then(() => proxy.$ElMessage.success('修改成功'))
      .catch(() => { }); break;
    case 2: SupplierAdd(state.formData)
      .then((res: any) => {

        dialog.value.visible = false;
      }).then(() => proxy.$ElMessage.success('创建成功'))
      .catch(() => { }); break;
  }
  statuButton = 0;
}

/****************************** 分配角色 ***************************/
function handleRole(row: any) {
  state.roleDialog.visible = true;
  state.allRole = [];
  state.selectItem = row;
}
function cancelRoleDialog() {
  state.roleDialog.visible = false;
}
function roleDialogOpend() {

}
// function submitUserRole() {
//   let checkedRole = [] as any;
//   roleTableRef.value!.getSelectionRows().forEach((item:any)=>{
//     checkedRole.push(item.dataId);
//   })
//   let params = {
//     dataId: state.selectItem.dataId,
//     relIds: checkedRole,
//   }
//   saveUserRole({
//     data:params
//   })
//     .then((res:any)=>{
//       proxy.$ElMessage.success('关联成功');
//       cancelRoleDialog();
//       handleQuery();
//     })
//     .catch(() => {});
// }

</script>

<style scoped lang="scss">
.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

.el-overlay.userRoleModel {}

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