<template>
  <div>
    <MOSTY.PageTree :searchType="'03'" @getDeptId="getdeptData" :isOpenTree="true">
      <template #table>
        <MOSTY.Assort title="企业信息管理" />
        <div class="from_box">
          <el-form ref="elform" :model="fromObj" :inline="true" label-position="top" :rules="rules" :disabled='!Isaqry'>
            <el-form-item label="安全生产标准化达标级别：" prop="level">
              <div style="width: 320px;">
                <MOSTY.DictRidioAndSelect :dictData="D_BZ_AQSCBZHDBJB" v-model="fromObj.level" />
              </div>
            </el-form-item>
            <el-form-item label="达标得分：" :prop="fromObj.level == '4' ? '' : 'score'">
              <el-input v-model.number="fromObj.score" type='number' @blur="fromObj.score = JEblurFn(fromObj.score)"
                style="width: 150px" placeholder="请输入达标得分" />
            </el-form-item>
            <el-form-item label="有效期至：" prop="duetime"
              :rules="fromObj.level == '4' ? undefined : [{ required: true, message: '请选择有效期', trigger: 'chanage' }]">
              <el-date-picker v-model="fromObj.duetime" type="date" value-format="YYYY-MM-DD" style="width: 200px"
                placeholder="请选择有效期" />
            </el-form-item>
            <el-form-item label="职业健康安全管理体系-认证标识：" prop="healthSfrz">
              <div style="width: 273px;">
                <MOSTY.DictRidioAndSelect :dictData="D_BZ_RZBZ" v-model="fromObj.healthSfrz" />
              </div>
            </el-form-item>
            <el-form-item label="职业健康安全管理体系-有效截至日期：" prop="healthDuetime"
              :rules="fromObj.healthSfrz == '0' ? undefined : [{ required: true, message: '有效截至日期', trigger: 'chanage' }]">
              <el-date-picker v-model="fromObj.healthDuetime" type="date" value-format="YYYY-MM-DD" style="width: 205px"
                placeholder="有效截至日期" />
            </el-form-item>
            <el-form-item style="width: 68px;">
              <div class="flex just-end ml5">
                <el-button type="primary" @click="savefrom(elform)" :loading="butLoading">保存</el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
        <MOSTY.TabList :tablist="pageData.tableList" @handleChange="changelicolor2" :showCount="false"
          :obj="listQuery" />
      </template>
    </MOSTY.PageTree>
  </div>
</template>

<script setup>
// 引入
import * as MOSTY from "@/components/MyComponents/index";
import { onMounted, ref, reactive, defineAsyncComponent, getCurrentInstance } from "vue";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { JEblurFn, filterqx } from "@/utils/auth.js";
import { getaqbm } from "@/api/sys.js";
import * as rule from "@/utils/rules.js";
const { proxy } = getCurrentInstance();
const { D_BZ_AQSCBZHDBJB, D_BZ_RZBZ } = proxy.$dict("D_BZ_AQSCBZHDBJB", "D_BZ_RZBZ");
const elform = ref()
const Isaqry = ref(false)
const butLoading = ref(false);
const rules = reactive({
  level: [{ required: true, message: "请选择安全生产标准化达标级别", trigger: "chanage" }],
  ...rule.percentageRule({ validator: true, require: true }, "score"),
  healthSfrz: [{ required: true, message: "职业健康安全管理体系-认证标识", trigger: "chanage" }]
})
const pageData = reactive({
  tableList: [{
    title: '年度安全生产标准化', num: 0, icon: 'article', actives: true,
    components: defineAsyncComponent(() => import("@/views/decisionCenter/dataMaintain/manage/qyxxgl/component/Ndaqscbzh.vue"))
  },
  {
    title: '单位荣誉情况', num: 0, icon: 'article-ranking', actives: false,
    components: defineAsyncComponent(() => import("@/views/decisionCenter/dataMaintain/manage/qyxxgl/component/Dwryjk.vue"))
  }],//模板数据
})
const fromObj = ref({
  level: '2',
  score: '',
  duetime: '',
  healthSfrz: '',
  healthDuetime: '',
});

const listQuery = reactive({
  orgCode: "",
  orgname: "",
})

onMounted(() => {
  getStandard()
});
//获取基本配置数据
const getStandard = () => {
  let id = ''
  if (listQuery.orgCode === '620001') {
    id = '620001XN'
  } else {
    id = listQuery.orgCode
  }
  getApi({ orgid: id }, "/mosty-jcgl/standard/getStandard").then(res => {
    fromObj.value = res || {}
    fromObj.value.level = fromObj.value.level ? fromObj.value.level : '2'
  })
}
//模块选择
const changelicolor2 = (val) => {
  pageData.tableList.forEach(item => {
    if (item.title == val.title) {
      item.actives = true
    } else {
      item.actives = false
    }
  })
}
//选择右侧部门
function getdeptData(val) {
  pageData.tableList = [{
    title: '年度安全生产标准化', num: 0, icon: 'article', actives: true,
    components: defineAsyncComponent(() => import("@/views/decisionCenter/dataMaintain/manage/qyxxgl/component/Ndaqscbzh.vue"))
  },
  {
    title: '单位荣誉情况', num: 0, icon: 'article-ranking', actives: false,
    components: defineAsyncComponent(() => import("@/views/decisionCenter/dataMaintain/manage/qyxxgl/component/Dwryjk.vue"))
  }]
  // if (val.id === '620001') val.id = '620001XN'
  listQuery.orgCode = val.id.replace("XN", "")
  // listQuery.orgCode = val.id
  listQuery.orgname = val.orgname
  listQuery.isEdit = false
  pageData.tableList[0].actives = true
  pageData.tableList[1].actives = false
  if (["20"].includes(val.orglevel)) {
    getApi({ orgId: val.id }, "/mosty-base/deptFeign/getLogisticsOrg").then(res => {
      // 市州级别物流中心填写年配送量，市州只做查询
      if (res) pageData.tableList.push({ title: '年配送量', num: 0, icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Delivery.vue")) })
      if (filterqx("isaqbm") == "1") listQuery.isEdit = true
    })
  } else if (["10"].includes(val.orglevel)) {
    pageData.tableList.push({ title: '年配送量', num: 0, icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Delivery.vue")) })
  }
  // 只有烟叶复烤公司才展示年复烤量
  if (val.parentid == '529914520001' && !val.id.includes('XN')) {
    pageData.tableList.push({ title: '年复烤量', num: 0, icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Redrying.vue")) })
  }
  // 市州级别烟叶中心填写年收购量，庆阳市、陇南市 622601 621001
  if (["622601", "621001"].includes(val.id) && ["20"].includes(val.orglevel)) {
    pageData.tableList.push({ title: '年收购量', num: 0, icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Acquisition.vue")) })
    if (filterqx("isaqbm") == "1" && ["20"].includes(val.orglevel)) {
      listQuery.isEdit = true
    }
  }

  //获取单位下的安全部门
  getStandard()
  getaqbm({ orgcode: listQuery.orgCode }).then((res) => {
    if (listQuery.orgCode == res.parentId) {
      Isaqry.value = true
    } else {
      Isaqry.value = false
    }
  })
}

//保存
const savefrom = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      butLoading.value = true
      fromObj.value.orgid = listQuery.orgCode
      if (fromObj.value.orgid === '620001') fromObj.value.orgid = '620001XN'
      fromObj.value.score = fromObj.value.score ? parseFloat(fromObj.value.score) : null
      postApi(fromObj.value, "/mosty-jcgl/standard/setStandard").then(res => {
        proxy.$message.success("成功")
        getStandard()
      }).finally(() => {
        butLoading.value = false
      })
    }
  })
}





</script>

<style lang="scss" scoped>
.perNum {
  height: 271px;
}

// overflow: auto;
// width: calc(100% - 300px);
.head {
  display: flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px dashed var(--el-color-primary);
  height: 60px;
  color: #000;

  .el-select {
    width: 150px;
  }
}

.from_box {

  // margin-left: 10%;
  .el-form {
    width: 100%;
    // margin: 10px auto;
    display: flex;
    align-items: center;
    justify-content: center;

    .el-form-item {
      display: flex;
      align-items: center;
    }
  }
}





::v-deep .el-form--label-top .el-form-item .el-form-item__label {
  margin-bottom: 0px;
}
</style>
