<template>
  <div class="sys-init-set">
    <MOSTY.Assort :title="'系统初始设定'" />
    <el-divider>上报国家局相关</el-divider>
    <div class="btn-group">
      <el-button type="primary" @click="init('Aqgldw', '安全管理队伍')">初始化 安全管理队伍</el-button>
      <el-button type="primary" @click="init('SanTs', '三同时管理')">初始化 三同时管理</el-button>
      <el-button type="primary" @click="init('jianZw', '建筑物情况')">初始化 建筑物情况</el-button>
    </div>
    <div class="btn-group">
      <el-button type="primary" @click="init('xiaoFzdbw', '消防重点部位')">初始化 消防重点部位</el-button>
      <el-button type="primary" @click="init('cangKss', '仓储设施')">初始化 仓储设施</el-button>
      <el-button type="primary" @click="init('jiDcl', '机动车辆')">初始化 机动车辆</el-button>
    </div>
    <div class="btn-group">
      <el-button type="primary" @click="init('zhuanZjsy', '专职驾驶员')">初始化 专职驾驶员</el-button>
      <el-button type="primary" @click="init('fengXdy', '风险单元')">初始化 风险单元</el-button>
      <el-button type="primary" @click="init('yingJya', '应急预案')">初始化 应急预案</el-button>
      <el-button type="primary" @click="init('shiGsj', '事故事件')">初始化 事故事件</el-button>
    </div>
    <el-divider>其他</el-divider>
    <div class="btn-group">
      <el-button type="danger" v-if="getItem('USERID') == '000000'"
        @click="init('chongZsymm', '所有用户密码')">重置所有密码</el-button>
      <el-button type="primary" @click="getHoliday('getHoliday', '获取节假日')">获取节假日</el-button>
      <!-- <el-button type="primary" @click="init('handleNC', '重新发起预算审核任务')">重新发起预算审核任务</el-button> -->
      <el-button type="primary" @click="syncXgf('syncXgf', '同步相关方信息')">同步相关方信息</el-button>
    </div>
    <div class="btn-group">
      <el-button type="primary" @click="getXgfProgress('getXgfprogress', '获取相关方进度')">相关方进度 {{ xgfProgress > 0 ?
        ` ${xgfProgress}%` : '' }}</el-button>
      <el-button type="primary" @click="driverpostsSyn('driverpostsSyn', '同步驾驶员上岗证信息')">同步驾驶员上岗证信息</el-button>
      <el-button type="primary" @click="correctDriverData('correctDriverData', '修正驾驶员上岗证信息')">修正驾驶员上岗证信息</el-button>
      <el-button type="primary" @click="fix('fix', '修复危险作业审批流程')">修复危险作业审批流程</el-button>
      <el-button type="primary" :loading="loadingObj.updateUserLevel"
        @click="updateLev('/common/updateUserLevel', '更新用户层级')">更新用户层级</el-button>
    </div>
    <div class="btn-group">
      <el-button type="primary" @click="updateDept('/obj/checkAndUpdate', '更新obj责任部门')">更新obj责任部门</el-button>
      <el-button type="primary" @click="syncObjData('/obj/syncObjAll', '同步obj表数据')">同步obj表数据</el-button>
      <el-button type="primary" @click="sendMsg('/rw/sendTestMsg', '发送消息测试')">发送消息测试</el-button>
    </div>
  </div>
</template>

<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance();
import { getApi, deleteApi, putApi, postApi } from "@/api/tobAcco_api.js";
import { getItem } from "@/utils/storage.js";

const xgfProgress = ref('0')
const xgfProgressTimer = ref()

const loadingObj = ref({
  updateUserLevel: false
})
const restAllPwd = () => {
  getApi({ citycode: '5400' }, "/mosty-base/userFeign/updateAllPwd").then(res => {
    ElMessage.success("密码重置成功");
  })
}
const onCxfaqysshrw = () => {

}

/**
 *
 * @param btnStr 按钮名称
 * @param title 按钮中文名
 */
const init = (btnStr, title, message = '') => {
  if (!btnStr) return
  const urlObj = {
    Aqgldw: "/data/safeManageTeamStat/initData",// 安全管理队伍
    SanTs: "/data/StsFileEscalationStat/initData",// 三同时管理
    jianZw: "/data/BuildSafeStat/initData",// 建筑物情况

    xiaoFzdbw: "/data/importantPartStat/initData",// 消防重点部位
    cangKss: "/data/StorageEscalationStat/initData", // 仓储设施
    jiDcl: "/data/CarStat/initData", // 机动车辆
    zhuanZjsy: "/data/driverStat/initData", // 专职驾驶员

    fengXdy: "/data/riskUnitStat/initStat", // 风险单元
    yingJya: "/data/emergencyPlanInfo/initStat", // 应急预案
    shiGsj: "/data/accidentEventsStat/initStat", // 事故事件
    handleNC: "/budgets/handleNC", // 重新发起预算审核任务
  }
  if (!message) message = btnStr === 'chongZsymm' ? '重置所有人密码' : `执行【${title}】`
  ElMessageBox.prompt('输入操作密码并确认', message, {
    // inputType: 'password',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
    .then(async ({ value }) => {
      let isPass = await verifyPassword(value)
      // console.log('isPass: ', isPass);
      if (isPass === true) {
        if (btnStr === 'chongZsymm') {
          restAllPwd()
        } else {
          const url = urlObj[btnStr]
          const apiFun = btnStr === 'yingJya' ? postApi : getApi
          apiFun({}, `/mosty-jcgl${url}`).then((res) => {
            ElMessage.success(`【${title}】操作成功`)
          })
        }

      } else {
        ElMessage.warning('密码错误!');
      }

    })
    .catch(() => {
      // ElMessage({
      //   type: 'info',
      //   message: '已',
      // })
    })
};
const verifyPassword = (value) => {
  return postApi({ pd: value }, '/mosty-jcgl/common/verifyPassword')

};
/** 通用密码确认
 * @param {String} message 弹框内容
 * @param {Function} callback 回调方法
 */
const toConfirm = (message, callback) => {
  ElMessageBox.prompt('输入操作密码并确认', message, {
    // inputType: 'password',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
    .then(async ({ value }) => {
      let isPass = await verifyPassword(value)
      // console.log('isPass: ', isPass);
      if (isPass === true) {
        callback();
      } else {
        ElMessage.warning('密码错误!');
      }

    })
    .catch(() => {
    })
}
const getHoliday = () => {
  let message = '获取节假日'
  toConfirm(message, () => {
    let year = new Date().getFullYear()
    getApi({ year: year }, "/mosty-jcgl/task/getHoliday").then(res => {
      ElMessage.success("节假日获取成功");
    })
  })
}
/**同步相关方信息 */
const syncXgf = () => {
  let message = '同步相关方信息'
  toConfirm(message, () => {
    getApi({}, "/mosty-jcgl/common/iniObjXgfXX").then(res => {
      getXgfProgress()
      ElMessage.success("已开始同步相关方信息...");
    })
  })
}
/** 同步驾驶员上岗证 */
const driverpostsSyn = () => {
  let message = '同步驾驶员上岗证信息'
  toConfirm(message, () => {
    postApi({}, "/mosty-jcgl/driverposts/sync").then(res => {
      ElMessage.success("同步驾驶员上岗证信息成功");
    })
  })
}
/** 同步驾驶员上岗证 */
const correctDriverData = () => {
  let message = '修正驾驶员上岗证信息'
  toConfirm(message, () => {
    postApi({}, "/mosty-jcgl/driverposts/correct").then(res => {
      ElMessage.success("修正驾驶员上岗证信息成功");
    })
  })
}
/** 同步驾驶员上岗证 */
const fix = () => {
  let message = '修复危险作业审批流程'
  toConfirm(message, () => {
    getApi({}, "/mosty-jcgl/dangerJob/fix").then(res => {
      ElMessage.success("修复危险作业审批流程成功");
    })
  })
}
/** 做某事方法 getApi
 * @param {String} url
 * @param {String} message 信息
 */
const doFuncPost = (url, message, loadingkey) => {
  toConfirm(message, () => {
    if (loadingkey) loadingObj.value[loadingkey] = true // 显示loading
    postApi({}, `/mosty-jcgl${url}`).then(res => {
      ElMessage.success(`${message}成功!`);
    }).finally(() => {
      if (loadingkey) loadingObj.value[loadingkey] = false
    })
  })
}
/** 做某事方法 getApi
 * @param {String} url
 * @param {String} message 信息
 */
const doFuncGet = (url, message, loadingkey) => {
  toConfirm(message, () => {
    if (loadingkey) loadingObj.value[loadingkey] = true // 显示loading
    getApi({}, `/mosty-jcgl${url}`).then(res => {
      ElMessage.success(`${message}成功!`);
    }).finally(() => {
      if (loadingkey) loadingObj.value[loadingkey] = false
    })
  })
}
/** 更新层级 */
const updateLev = (url, message) => {
  doFuncGet(url, message, 'updateUserLevel')
}

const updateDept = (url, message) => {
  doFuncGet(url, message, url)
}
const syncObjData = (url, message) => {
  doFuncPost(url, message, url)
}
const sendMsg = (url, message) => {
  doFuncGet(url, message, url)
}

const setXgfProgress = () => {
  getApi({}, "/mosty-jcgl/common/getiniObjXgfXXProgress").then(res => {
    if (res > 0 && res < 100) {
      xgfProgress.value = res
      xgfProgressTimer.value = setTimeout(() => {
        setXgfProgress()
      }, 1000)
    } else if (res == 100) {
      xgfProgress.value = res
      ElMessage.success("同步相关方信息完成!");
    } else {

    }
    console.log('当前进度', `${res}%`)
  })
}
const getXgfProgress = () => {
  if (xgfProgressTimer.value) clearTimeout(xgfProgressTimer.value)
  setXgfProgress()
}
onMounted(() => {
});

onUnmounted(() => {
  clearTimeout(xgfProgressTimer.value)
});
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";

.sys-init-set {
  height: calc(100vh - 142px);
  width: 100%;
  background: #fff;
  padding: 0 10px;

  .btn-group {
    // margin: 20px;
    padding: 20px;
  }
}
</style>
