<template>
  <div class="main">
    <div class="login">
      <div class="title">AMES 工位登录</div>
      <div class="login-form">
        <div class="form-item">
          <el-input v-model="userform.userName" :prefix-icon="User" clearable placeholder="请输入用户名" size="large"
            class="login-input" />
        </div>
        <div class="form-item">
          <el-input v-model="userform.password" :prefix-icon="Lock" clearable placeholder="请输入密码" show-password
            size="large" class="login-input" @keyup.enter="login" />
        </div>
        <div class="form-item">
          <el-select v-model="userform.opName" placeholder="请选择台位号" size="large" class="login-select">
            <el-option v-for="item in opNameList" :key="item.工位号" :label="item.工位号" :value="item.工位号" />
          </el-select>
        </div>
        <div class="form-item">
          <el-button type="danger" size="large" class="login-button" @click="login">
            登 录
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { inject, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAppStore } from "@/store/app.js";
import { storeToRefs } from 'pinia';
import { encrypt, cryptoDecrypt, generateSalt } from "../../utils/AES-Pkcs7";
import Cookies from 'js-cookie'

const router = useRouter()
const CreateData = inject('CreateData')
const ExecDatabase = inject('ExecDatabase')
const store = useAppStore();
// storeToRefs 响应式解构赋值 pinia
const { user, isLoading } = storeToRefs(store);

const opNameList = reactive([])
const userform = reactive({
  userName: '',
  password: '',
  opName: '',
  opNameText: ''
})

onMounted(() => {
  getOpName()
})

const getOpName = () => {
  opNameList.length = 0
  // 获取当前操作员名称
  var param = []
  var Data = CreateData('11', 'MES_计划BOM_工位与名称_PDA用_查询', param)
  ExecDatabase(Data).then(response => {
    if (response.data.length > 0) {
      opNameList.push(...response.data)
      // 获取上次登录的工位号
      const lastOpName = Cookies.get('lastOpName')
      if (lastOpName && opNameList.some(op => op.工位号 === lastOpName)) {
        userform.opName = lastOpName
        userform.opNameText = opNameList.find(op => op.工位号 === lastOpName).工位名称
      } else {
        userform.opName = opNameList[0].工位号
        userform.opNameText = opNameList[0].工位名称
      }
    }
    console.log(response)
  })
}

const login = () => {
  console.log(user)
  var param = []
  param.push(['用户名', userform.userName])
  param.push(['密码', userform.password])
  param.push(['功能类型', 0])
  var Data = CreateData('11', '菜单模块系统_用户名密码_查询数据_改造新增', param)
  ExecDatabase(Data).then(response => {
    const data = response.data
    if (data.length === 0 || data.result === '0' || data[0].result === '0') {
      ElMessage.warning('账号或密码错误，请重试!')
    } else {
      const param2 = []
      param2[0] =  ['工位号', userform.opName]
      param2[1] =  ['操作者工号', userform.userName]
      param2[2] =  ['班次名称', 'A'] // this.loginForm.class]
      const Data = CreateData('12', '测量数据工位_监控系统_操作者工号_增加', param2)
      ExecDatabase(Data).then(() => { })

      // 存储当前工位号到 cookie
      Cookies.set('lastOpName', userform.opName, { expires: 365 }) // 保存一年

      Cookies.set('AID', data[0].人员编号, { expires: 1 })
      Cookies.set('name', data[0].姓名, { expires: 1 })
      Cookies.set('role', data[0].角色编号, { expires: 1 })
      Cookies.set('userName', data[0].考勤编号, { expires: 1 })
      Cookies.set('opName', userform.opName, { expires: 1 })
      Cookies.set('opNameText', userform.opNameText, { expires: 1 })

      user.value.AID = response.data[0].人员编号
      user.value.name = response.data[0].姓名
      user.value.role = response.data[0].角色编号
      user.value.userName = response.data[0].考勤编号
      user.value.opName = userform.opName
      user.value.opNameText = userform.opNameText
      ElMessage.success('登录成功')
      store.isLoading = true
      router.push('/')
    }
  })
}

</script>

<style lang="scss" scoped>
.main {
  position: relative;
  height: 100vh;
  width: 100vw;
  background-image: url("/public/login.png");
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;

  .login {
    width: 85vw;
    padding: 40px 20px;
    background-color: rgba(255, 255, 255, 0.8);
    border: 2px solid #730215;
    border-radius: 16px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);

    .title {
      color: #e43852;
      font-size: 42px;
      text-align: center;
      font-weight: bold;
      margin-bottom: 40px;
    }

    .login-form {
      width: 90%;
      margin: 0 auto;

      .form-item {
        margin-bottom: 24px;

        &:last-child {
          margin-bottom: 0;
          margin-top: 40px;
        }

        .login-input {
          height: 50px;
          font-size: 16px;

          :deep(.el-input__wrapper) {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 8px;
            padding: 0 15px;
            backdrop-filter: blur(5px);
          }

          :deep(.el-input__inner) {
            height: 50px;
          }
        }

        .login-select {
          width: 100%;
          height: 50px;

          :deep(.el-input__wrapper) {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 8px;
            padding: 0 15px;
            backdrop-filter: blur(5px);
          }

          :deep(.el-input__inner) {
            height: 50px;
            font-size: 16px;
          }
        }

        .login-button {
          width: 100%;
          height: 50px;
          font-size: 18px;
          font-weight: bold;
          border-radius: 8px;
          background-color: #e43852;
          border: none;

          &:hover {
            background-color: darken(#e43852, 10%);
          }

          &:active {
            background-color: darken(#e43852, 20%);
          }
        }
      }
    }
  }
}

// 适配不同屏幕尺寸
@media screen and (min-width: 768px) {
  .main {
    .login {
      width: 70vw;
      max-width: 600px;
    }
  }
}

@media screen and (max-width: 767px) {
  .main {
    .login {
      width: 90vw;

      .title {
        font-size: 36px;
      }
    }
  }
}
</style>
