<template>
  <div class="content_panel" style="margin-right: 40px">
    <div class="panel_top">
      <div class="top_content">
        <div class="circle">1</div>
        <div class="title_name">问题{{ props.title }}阶段</div>
        <div class="title_explain">问题{{ props.title }}阶段</div>
      </div>
      <el-button
        v-show="editDetails.openEdit === false"
        link
        type="primary"
        size="small"
        @click="openQuestionEdit"
        style="height: 15px; margin-top: 4px; margin-left: auto"
        ><el-tooltip class="box-item" effect="dark" content="编辑" placement="top">
          <el-icon :size="16">
            <EditPen />
          </el-icon>
        </el-tooltip>
      </el-button>
      <el-button
        v-show="editDetails.openEdit === true"
        type="primary"
        style="height: 24px; margin-left: auto; width: 48px"
        @click="saveQuestionEdit"
        >保存</el-button
      >
    </div>
    <div class="subtitle"
      >问题{{ props.title }}时
      <el-switch
        :disabled="!editDetails.openEdit"
        v-model="editDetails.atProcessing"
        size="small"
        style="height: 16px; margin-left: 12px"
      />
    </div>
    <div class="subtitle_explain_disable">自动推送给问题发生地点的所有{{ props.title }}员</div>
    <div v-show="props.issueType != QuestionStatus.finishe" class="subtitle"
      >超期未{{ props.title }}
      <el-switch
        :disabled="!editDetails.openEdit"
        v-model="editDetails.atExpiry"
        size="small"
        style="height: 16px; margin-left: 12px"
      />
    </div>
    <div
      v-show="props.issueType != QuestionStatus.finishe"
      :class="[
        'subtitle_explain_disable',
        { editable: editDetails.openEdit === true && editDetails.atExpiry === true },
      ]"
      style="margin-top: 10px"
      >超过<el-input-number
        v-model="editDetails.expiryTime"
        :disabled="!editDetails.openEdit || !editDetails.atExpiry"
        :min="0"
        size="small"
        style="width: 70px"
        controls-position="right"
      />小时未{{ props.title }}继续推送给{{ props.title }}员</div
    >
    <div v-show="props.issueType != QuestionStatus.finishe" class="subtitle"
      >长期未{{ props.title }}
      <el-switch
        :disabled="!editDetails.openEdit"
        v-model="editDetails.atLongTimeExpiry"
        size="small"
        style="height: 16px; margin-left: 12px"
      />
    </div>
    <div
      v-show="props.issueType != QuestionStatus.finishe"
      :class="[
        'subtitle_explain_disable',
        { editable: editDetails.openEdit === true && editDetails.atLongTimeExpiry === true },
      ]"
      style="margin-top: 10px"
      >超过<el-input-number
        v-model="editDetails.longTimeValue"
        :disabled="!editDetails.openEdit || !editDetails.atLongTimeExpiry"
        :min="0"
        size="small"
        style="width: 70px"
        controls-position="right" />小时未{{ props.title }}再次推送给{{
        props.title
      }}员并抄送给<el-select
        :disabled="!editDetails.openEdit || !editDetails.atLongTimeExpiry"
        v-model="editDetails.copyTo"
        value-key="realname"
        @click="openNameTree"
        placeholder="Select"
        size="small"
        style="width: 105px"
        multiple
        collapse-tags
        collapse-tags-tooltip
      >
        <el-option
          v-for="item in directors"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        /> </el-select
    ></div>
    <div class="subtitle">推送文案 </div>
    <el-input
      :disabled="!editDetails.openEdit"
      v-model="editDetails.textToPush"
      style="width: 364px; margin-left: 30px; margin-top: 4px"
    />
  </div>
  <el-dialog
    v-model="dialogVisible"
    title="添加人员"
    align-center
    :close-on-click-modal="false"
    style="height: 583px"
    :width="731"
    :destroy-on-close="true"
    class="workShopDialog"
  >
    <!-- <SelectTree @cancel="handleCancle" @submit="handleSubmit" :selectedUser="selectedUser" /> -->
    <PersonFilterSelection
      @cancel="handleCancle"
      @submit="handleSubmit"
      :init-selected="selectedUser"
    />
  </el-dialog>
</template>
<script lang="ts" setup>
  import { panelDetails, QuestionStatus, issueDetilasType, PushTypeStatus } from '../type';
  import { EditPen } from '@element-plus/icons-vue';
  import { ref, watch } from 'vue';
  import { SelectedFilterPersonInfo } from '@/api/message/person-group';

  const props = defineProps<{
    savedData: Array<issueDetilasType>;
    issueType: number;
    title: string;
    saveUpdate: (data: panelDetails, issueType: number) => unknown;
  }>();

  watch(
    () => props.savedData, //监听状态值,，当拉取的数据发生变化时，对editDetails进行赋初值
    (newValue) => {
      //问题审核阶段，后端拉取保存数据
      let subData = newValue.find((item) => item.issuePhase === props.issueType);
      let atonceData = subData?.issueProcessMessageList.find(
        (item) => item.pushType === PushTypeStatus.atonce,
      );
      editDetails.value.atProcessing = atonceData?.isEnabled === 1 ? true : false;
      let expireData = subData?.issueProcessMessageList.find(
        (item) => item.pushType === PushTypeStatus.expire,
      );
      editDetails.value.atExpiry = expireData?.isEnabled === 1 ? true : false;
      editDetails.value.expiryTime = expireData!.overtime;
      let longTimeData = subData?.issueProcessMessageList.find(
        (item) => item.pushType === PushTypeStatus.longtime,
      );
      editDetails.value.atLongTimeExpiry = longTimeData?.isEnabled === 1 ? true : false;
      editDetails.value.longTimeValue = longTimeData!.overtime;
      editDetails.value.copyTo = longTimeData!.ccRecipients;
    },
    { deep: true, immediate: true }, //deep，深度监听。immediate，在程序开始运行时即马上监听
  );

  const editDetails = ref<panelDetails>({
    atProcessing: false,
    atExpiry: false,
    atLongTimeExpiry: false,
    expiryTime: 0,
    longTimeValue: 0,
    copyTo: [],
    textToPush: '',
    openEdit: false,
  });
  const openQuestionEdit = () => {
    editDetails.value.openEdit = true;
  };
  const saveQuestionEdit = () => {
    props.saveUpdate(editDetails.value, props.issueType);
    editDetails.value.openEdit = false;
  };

  const directors = [
    {
      value: '陆顶',
      label: '陆顶',
    },
  ];
  const dialogVisible = ref<boolean>(false);
  const selectedUser = ref<SelectedFilterPersonInfo[]>([]);

  const openNameTree = () => {
    selectedUser.value = editDetails.value.copyTo!;
    dialogVisible.value = true;
  };

  const handleSubmit = (selectedData: SelectedFilterPersonInfo[]) => {
    // selectedUser.value = selectedData;
    editDetails.value.copyTo = selectedData;
    dialogVisible.value = false;
  };
  const handleCancle = () => {
    dialogVisible.value = false;
  };
</script>
<style lang="scss" scoped>
  .content_panel {
    width: 530px;
    height: 326px;
    // background-color: pink;
    border-radius: 8px;
    border: 1px solid #e4e7ed;
    .panel_top {
      width: 100%;
      height: 50px;
      display: flex;
      flex-direction: row;
      background-color: #fafafa;
      border-radius: 8px;
      padding: 13px 16px 0px 16px;
      .top_content {
        // width: 100%;
        height: 24px;
        display: flex;
        flex-direction: row;
        .circle {
          width: 24px;
          height: 24px;
          border-radius: 12px;
          background: #1890ff;
          font-weight: 600;
          font-size: 14px;
          color: #ffffff;
          line-height: 24px;
          text-align: center;
        }
        .title_name {
          height: 24px;
          margin-left: 8px;
          font-weight: 600;
          font-size: 16px;
          color: rgba(0, 0, 0, 0.88);
          line-height: 24px;
          text-align: left;
        }
        .title_explain {
          height: 14px;
          margin-left: 8px;
          margin-top: 6px;
          font-weight: 400;
          font-size: 10px;
          color: #999999;
          line-height: 14px;
          text-align: left;
        }
      }
    }
    .subtitle {
      height: 17px;
      margin-left: 30px;
      margin-top: 17px;
      font-weight: 600;
      font-size: 12px;
      color: #303133;
      line-height: 17px;
      text-align: left;
      display: flex;
      flex-direction: row;
    }
    .subtitle_explain_disable {
      height: 21px;
      margin-left: 30px;
      // margin-top: 4px;
      // margin-top: 3px;
      font-weight: 400;
      font-size: 10px;
      color: #a8abb2;
      line-height: 21px;
      text-align: left;
      display: flex;
      flex-direction: row;
    }
    .editable {
      color: #303133 !important;
    }
  }
</style>
