<template>
  <el-dialog :title="props.title" width="1200px" v-model="dialogVisible" :before-close="onClose">
    <div class="config">
      <el-form ref="ruleFormRef" label-width="auto"
        style="display: flex;width: 100% !important;flex-wrap: wrap;min-height: 140px;max-height: calc(100vh - 300px);"
        :model="info" :disabled="!props.savebtn" label-position="right" :rules="rules" label-suffix=":" :inline="true">
        <el-form-item label="值班日期" prop="day" style="width: 100%;">
          <el-input v-model="info.day" disabled style="width: 100%;" />
        </el-form-item>
        <el-form-item label="值班人员" prop="userid" style="width: 100%;">
          <el-input v-model="info.username" disabled style="width: 100%;" />
        </el-form-item>
        <el-form-item label="值班情况" prop="zbqk" style="width: 100%;">
          <el-input v-model="info.zbqk" type="textarea" :rows="4"  show-word-limit style="width: 100%;" maxlength="500" />
        </el-form-item>


      </el-form>

      <div>

      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">

        <el-button type="primary" style="margin-right: 16px;" v-loading="loadingShow" @click="onSave"
          v-if="props.savebtn"> <el-icon>
            <DocumentChecked />
          </el-icon>
          <span>保存</span> </el-button>
        <el-button @click="onClose"> <el-icon>
            <DocumentDelete />
          </el-icon>
          <span>关闭</span></el-button>
      </span>
    </template>
  </el-dialog>
  <!-- 部门 -->
  <Choosebm :showDdld="showDept" @closezzjg="closezzjg" @listdata="bmChange" :zzlx="zzlx" :searchType="'01'"
    :deptId='deptIdbm' :Isstrictly="false" />
  <!-- 选择责任人 -->
  <ChooseUser v-model="chooseUserVisible" :Single="false" @choosedUsers="getSonValue" :deptId="currDept.deptid"
    :data="memberList"> </ChooseUser>
  <!-- 选择部门 -->
  <!-- <dwhasbjMult :showDdld="showDept" @closezzjg="closezzjg" Isstrictly :deptId="deptIdbm" v-model="bmList"
      searchType="03" @listdata="bmChange" /> -->
</template>

<script setup>
import { onMounted, ref, reactive, getCurrentInstance, computed } from "vue";
import * as MOSTY from "@/components/MyComponents/index";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
import * as rule from "@/utils/rules.js";
import Choosebm from "@/components/MyComponents/ChooseDept/index.vue";
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue"
import dwhasbjMult from "@/components/MyComponents/ChooseDept/dwhasbjMult.vue";

const { proxy } = getCurrentInstance();
// 数据

const props = defineProps({
  title: String,
  id: String,
  savebtn: Boolean,
  isDetail: {
    type: Boolean,
    default: false
  },
  rowItem: {
    type: Object,
    default: () => ({})
  },
  zbFrom: {
    type: Object,
    default: () => ({})
  },
});
const emits = defineEmits(["closeDetail"]);
/** 添加部门dis */
const addBmDisBtn = computed(() => {
  if (!props.savebtn) return true
  if (props.id && bmList?.value?.length) return true
})
const dialogVisible = ref(true);
const loadingShow = ref(false)
const ruleFormRef = ref()
const info = ref({})
const showDept = ref(false)
const deptIdbm = ref()
const zzlx = ref(null)
const memberList = ref([])
const currDept = ref({})
const isEdit = ref(props.savebtn && props.id)
const isAdd = ref(props.savebtn && !props.id)
/** 选中的部门 */
const bmList = ref([])
//选择人员弹窗
const chooseUserVisible = ref(false)
const rules = ref({
  zbqk: [{ required: true, message: "请填写值班情况", trigger: "blur" }],
});
onMounted(() => {
  // if (props.id) {

  info.value = {
    day: props.rowItem.day,
    // username: props.rowItem.username,	//值班人员
    username: props.rowItem.username,	//值班人员
    userid: props.rowItem.userid,	//值班人员
    zbqk: props.rowItem.zbqk,	//值班情况
  }
  // }

  info.value.day = props.zbFrom.day
});
const onClose = () => {
  if (props.savebtn) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emits('closeDetail')
      })
      .catch(() => {
        // catch error
      })
  } else {
    emits('closeDetail')
  }

};
const chooseRy = (item, index) => {

  // zzlx.value = "01"
  currDept.value = item
  chooseUserVisible.value = true
}
const getSonValue = (value) => {

  value = Array.isArray(value) ? value : []
  currDept.value.username = value.map(item => item.userName).join(',')
  currDept.value.userid = value.map(item => item.id).join(',')
  chooseUserVisible.value = false
}
const onSave = async () => {
  // loadingShow.value = true

  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let params = {
        ...info.value,
      }
      postApi(params, "/mosty-jcgl/onDuty/saveZbqk")
        .then((res) => {
          ElMessage({
            message: "修改成功",
            type: "success"
          });
          emits("closeDetail", true);
        })
        .finally(() => {
          loadingShow.value = false
        });


    } else {
      ElMessage.warning('请输入必填项！！！')
      loadingShow.value = false
    }
  });
}
//关闭弹窗
const closezzjg = (val) => {
  showDept.value = val;
};
const bmChange = (val) => {
  if (val.length == 0) return;
  // 相同部门不添加
  if (bmList.value.some(item => item.deptid == val.orgCode)) {
    ElMessage.warning('部门已存在！')
    return
  }
  bmList.value.push({
    deptid: val.orgCode,
    deptname: val.orgName,
  })
};
const chooseBm = () => {
  showDept.value = true;
}
const delBm = (index) => {
  bmList.value.splice(index, 1)
}
const getDetail = () => {
  // let _url = props.isDetail ? "/data/riskUnitDist/getDetailById" : "/data/riskUnitDist/getLsjlDetail"
  // getApi({ id: props.id }, `/mosty-jcgl${_url}`)
  //   .then((res) => {
  //     info.value = res
  //   })
}

</script>

<style lang="scss" scoped>
.config {
  width: 740px;
}

.content {
  .topper {
    display: flex;

    .sub-le {
      flex: 1;
      padding: 15px 15px 5px 15px;
      border-right: 1px solid #ebeef5;
    }
  }

  .bottom {
    padding: 0 15px 10px 15px;
  }
}
</style>
