<template>
  <MOSTY.PageTree :pageTitle="'车管员列表'" :searchType="'03'" @getDeptId="getdeptId">
    <template #serach>
      <el-divider />
      <div class="search">
        <el-form ref="ruleFormRef" :model="fromObj" label-width="170px" status-icon :inline="true">
          <div style="display: flex">
            <el-form-item :label="`${orgLevel == '1' ? '省' : '市'}安全部门负责人:`" prop="principalheadname">
              <el-input v-model="fromObj.principalheadname" clearable style="width: 250px" readonly disabled="true">
                <!-- <template #append>
                  <el-button class='cursor' @click="ChooesDdld('01')" readonly disabled="true">选择</el-button>
                </template> -->
              </el-input>
            </el-form-item>
          </div>
          <div style="display: flex">
            <el-form-item :label="`${orgLevel == '1' ? '省' : '市安全科'}安全管理员`" prop="name">
              <el-input v-model="fromObj.name" clearable style="width: 250px" readonly :disabled="isxgs">
                <template #append><el-button class='cursor' @click="ChooesDdld('02')"
                    :disabled="isxgs">选择</el-button></template></el-input>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </template>
    <template #table>
      <el-divider />
      <section class="eltab3">
        <MOSTY.Table :table-columns="data.tableColumn" :tableData="data.tableData" :tableloading="tableloading"
          :tableHeight="'100%'" :isSelect="false" style="width: 100%;" :isSort="false">
          <template #actions="{ row }">
            <span class="operedit" @click="editInfo(row)"><el-icon>
                <EditPen />
              </el-icon>修改</span>
            <!-- <span class="operesee" @click="seeInfo(row)">
              v-if="row.orglevel == '30' && row.orgBizType == '05'"
              <el-icon>
                <View />
              </el-icon>查看</span> -->
          </template>
        </MOSTY.Table>
      </section>
    </template>
  </MOSTY.PageTree>
  <ToDetails v-if="shownewwb" :shownewwb="shownewwb" :type="'dw'" :saveBtnShow='saveBtnShow' :wbobjtitle="wbobjtitle"
    :obj="fromData" @changeclose='closeaddwb' />
  <ChooseUser v-model="chooseUserVisible" :Single="singleShow" :leaderid="leaderid" @choosedUsers="getSonValue"
    :deptId="deptId">
  </ChooseUser>
</template>
<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import ToDetails from "../ToDetails/index.vue";
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { reactive, onMounted, ref } from "vue";
import { getItem } from "@/utils/storage";
const emit = defineEmits("changePage");

const shownewwb = ref(false)
const saveBtnShow = ref(false)
const wbobjtitle = ref('')
const deptId = ref('')
const leaderid = ref("");
const ruleFormRef = ref();
const sjjtaqgl = ref('')
const aqgldm = ref('')
const fromObj = ref({})
const fromData = ref({})
const ssorgid = ref('')
const zzlx = ref(null);
const orgLevel = ref('1')

const data = reactive({
  deptId: "",
  aqglyobj: {},
  searchObj: {},
  tableData: [],
  tableColumn: [
    {
      title: "单位",
      prop: "orgname"
    },
    // {
    //   title: "单位分管领导",
    //   prop: "fgname"
    // },
    // {
    //   title: "单位安全部门负责人",
    //   prop: "fzrname"
    // },
    // {
    //   title: "单位车队队长",
    //   prop: "cdzname"
    // },
    {
      title: "单位车辆管理部门负责人",
      prop: "cdzname"
    },
    {
      title: "单位车管员",
      prop: "cgyname"
    },
    {
      title: "操作",
      prop: "actions",
      slot: true
    }
  ],

});
const listQuery = reactive({});
//选中的数据
const tableloading = ref(false);
const singleShow = ref(true);
const chooseUserVisible = ref(false);
const isxgs = ref(false)
const isAqy = ref(false)
// 初始化
onMounted(() => {
  // 判断是什么部门 县部门市交通员不能更改
  if (getItem('deptId').ssxgsid) {
    isxgs.value = true
  } else {
    // 判断是否是安全部门 
    if (getItem('deptId').orgBizType === '04') {
      isxgs.value = false
    } else {
      isxgs.value = true
    }
  }
  if (orgLevel.value == '1') {
    ssorgid.value = getItem('deptId').ssdwid
  } else {
    ssorgid.value = getItem('deptId').sssgsid
  }
});
const getSafeOwner = (params) => {
  if (params.endsWith("XN")) {
    params = params.slice(0, -2);
  }
  getApi({ orgCode: params }, '/mosty-jcgl/securitychild/getAqbmOwner')
    .then(res => {
      if (res) {
        fromObj.value.principalheadname = res
      }
    })
  getApi({ orgcode: params }, '/mosty-base/tbSysDictItem/queryCarSafeDictByOrgcode').then((res) => {
    if (res) {
      sjjtaqgl.value = res.bz ? res.bz.split('-')[0] : ''
      aqgldm.value = res.dm
      fromObj.value.name = sjjtaqgl.value
      fromObj.value.bz = res.bz
    }
  })
}
//获取部门数据
const getdeptId = (val) => {
  orgLevel.value = val.orglevel.substring(0, 1)
  if (val.id.indexOf("XN") != -1 && val.orglevel == '20') {
    orgLevel.value = '1'
  }
  listQuery.orgcode = val.id;
  getTableData();
  getSafeOwner(val.id)
};
// 左侧收缩

function ChooesDdld(val) {
  zzlx.value = val;
  leaderid.value = "";
  if (val == "01") {
    leaderid.value = fromObj.value.principalheadid;
  } else if (val == "02") {
    leaderid.value = fromObj.value.principalheadid;
  }
  singleShow.value = true;
  chooseUserVisible.value = true;
}
const getSonValue = (value) => {
  if (zzlx.value == "01") {
    fromObj.value.principalheadname = value[0].userName;
    fromObj.value.principalheadid = value[0].id;
    // 保存省市安全部门负责人
    fromObj.value.orgid = fromObj.value.orgid ? fromObj.value.orgid : ssorgid.value
    postApi(fromObj.value, "/mosty-jcgl/car/updateSecuritychild").then(() => {
      ElMessage.success("保存成功");
    })
  }
  if (zzlx.value == "02") {
    fromObj.value.name = value[0].userName;
    fromObj.value.bz = value[0].userName + '-' + value[0].id
    // 保存交通安全管理员
    postApi({ dm: aqgldm.value, bz: fromObj.value.bz }, "/mosty-base/tbSysDictItem/updateCarSafeDict").then(() => {
      ElMessage.success("保存成功");
      // sjjtaqgl.value = res.bz.split('-')[0]
      // aqgldm.value = res.dm
      // fromObj.value.name = sjjtaqgl.value
      // fromObj.value.bz = res.bz
    })
  }

};
function getTableData() {
  tableloading.value = true;
  getApi(listQuery, `/mosty-jcgl/sprole/querySprole`).then((res) => {
    if (res) {
      data.tableData = res;
      tableloading.value = false;
    } else {
      data.tableData = [];
      tableloading.value = false;
    }
  }).finally(() => {
    tableloading.value = false;
  });
  // getApi({}, `/mosty-jcgl/car/querySecuritychild`).then((res) => {
  //   if (res) {
  //     fromObj.value = res;
  //     fromObj.value.name = sjjtaqgl.value
  //     getApi({}, '/mosty-base/tbSysDictItem/queryCarSafeDict').then((res) => {
  //       sjjtaqgl.value = res.bz ? res.bz.split('-')[0] : ''
  //       aqgldm.value = res.dm
  //       fromObj.value.name = sjjtaqgl.value
  //       fromObj.value.bz = res.bz
  //     })
  //   }
  // });
}
// 修改
const editInfo = (row) => {
  shownewwb.value = true
  saveBtnShow.value = true
  wbobjtitle.value = '车管员修改'
  fromData.value = row
  fromData.value.fgldname = row.fgname;
  fromData.value.fgldid = row.fgid;
  fromData.value.principalheadname = row.fzrname;
  fromData.value.principalheadid = row.fzrid;
};
//查看
const seeInfo = (row) => {

  let arr = {
    orgid: row.orgid ? row.orgid : null
  };
  emit("changePage", "AddOrEdite", arr);
};
function closeaddwb() {
  shownewwb.value = false
  saveBtnShow.value = false
  wbobjtitle.value = ''
  getTableData()
}


</script>
<style lang="scss" scoped>
.eltab3 {
  height: calc(100vh - 293px);
}

.search {
  width: 100%;
  display: flex;
  margin-bottom: -17px;
  justify-content: center;
  color: black;
  font-size: 20px;
}
</style>