<template>
  <div class="send-notice-wrapper" id="screenSetSNId">
    <!-- <bread-crumb :curmbFirst="curmbFirst" :curmbSecond="curmbSecond"></bread-crumb> -->
    <div class="component-content">
      <el-row :gutter="30" class="row" type="flex" style="margin-top: 10px">
        <el-form
          :model="formData"
          ref="formData"
          :rules="rules"
          label-width="120px"
          class="form-inline"
          style="min-height: 300px"
        >
          <el-form-item
            label="是否开启公告："
            prop="projectIntro"
            style="min-width: 1000px; margin-bottom: 10px"
          >
            <el-col :span="20">
              <el-radio-group size="small" v-model:value="formData.openFlag">
                <el-radio :label="1">开启</el-radio>
                <el-radio :label="2">关闭</el-radio>
              </el-radio-group>
            </el-col>
          </el-form-item>
          <el-form-item
            v-show="formData.openFlag != 2"
            label="标题："
            prop="noticeTitle"
          >
            <el-col :span="20">
              <el-input
                size="small"
                v-model:value="formData.noticeTitle"
                placeholder="请输入公告标题"
                maxlength="20"
              ></el-input>
              <span class="word-num"
                >{{ formData.noticeTitle.replace(/\s+/g, '').length }}/20</span
              >
            </el-col>
          </el-form-item>
          <el-form-item
            v-show="formData.openFlag != 2"
            label="内容："
            prop="noticeContent"
          >
            <el-col :span="20">
              <el-input
                size="small"
                v-model:value="formData.noticeContent"
                placeholder="请输入公告内容"
                type="textarea"
                :autosize="{ minRows: 5 }"
                maxlength="150"
              ></el-input>
              <span class="word-num"
                >{{
                  formData.noticeContent.replace(/\s+/g, '').length
                }}/150</span
              >
            </el-col>
          </el-form-item>
          <!-- <el-form-item size="small" style="padding-top: 300px;">

              </el-form-item>           -->
        </el-form>
      </el-row>
      <el-col :span="20" :offset="2">
        <el-button type="primary" size="small" @click="cancleEdit()"
          >取消</el-button
        >
        <el-button type="primary" size="small" @click="beforeConfirm()"
          >发布</el-button
        >
      </el-col>
    </div>

    <el-dialog
      class="prot-dialog"
      :title="dialogTitle"
      v-model:visible="isShowDialog"
      width="30%"
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <p v-show="isCancleBtn" class="prot-dialog-tips-1">
        取消后，将不保存本次操作内容
      </p>
      <p v-show="!isCancleBtn" class="prot-dialog-tips-1">
        点击发布后将立即生效，是否继续发布？
      </p>
      <!-- <p class="prot-dialog-tips-2">若您选择暂不发送，您只能录入量表的非敏感信息</p> -->
      <!-- <p class="prot-dialog-tips-3">为避免打扰居民，7天内只能发送1次，您已发送过短信，请尽快联系居民同意授权</p> -->
      <template v-slot:footer>
        <span v-show="isCancleBtn" class="dialog-footer">
          <el-button size="small" @click="cancleConfirm"> 确定取消 </el-button>
          <el-button size="small" type="primary" @click="isShowDialog = false">
            继续操作
          </el-button>
        </span>
      </template>
      <template v-slot:footer>
        <span v-show="!isCancleBtn" class="dialog-footer">
          <el-button size="small" @click="openFlagCancle"> 取消 </el-button>
          <el-button size="small" type="primary" @click="openFlagConfirm">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { $on, $off, $once, $emit } from '../../../../utils/gogocodeTransfer'
import BreadCrumb from '@/components/breadcrumb.vue'
import { openLoading, closeLoading } from '@/utils/utils'
import * as commonUtil from '@/utils/utils'
import * as operationData from '@/utils/operation'
let vm = null
export default {
  components: {
    BreadCrumb,
  },
  props: {
    formData: {
      type: Object,
    },
  },
  data() {
    // let checkNoticeTitleStr = (rule, value, callback) => {
    //   if(value.indexOf("\\") != -1) {//存在
    //     callback(new Error('请勿输入字符“ \\ ”'));
    //   } else if (value.indexOf(".") != -1) {
    //     callback(new Error('请勿输入字符“ . ”'));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      curmbFirst: '教培项目',
      curmbSecond: '公告管理',
      projectId: '',
      dialogTitle: '',
      // formData: {
      //   noticeContent: "",
      //   noticeId: 0,
      //   noticeTitle: "",
      //   openFlag: 0,
      //   projectId: 0
      // },
      isShowDialog: false,
      isCancleBtn: true,
      changeNum: 1,
      rules: {
        noticeTitle: [
          { required: true, message: '请输入公告标题', trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: '输入长度为2-20的内容，可包含中英文、数字及特殊符号',
            trigger: 'blur',
          },
          // { validator: checkNoticeTitleStr, trigger: 'blur' }
        ],
        noticeContent: [
          { required: true, message: '请输入公告内容', trigger: 'blur' },
          {
            min: 2,
            max: 150,
            message: '输入长度为2-150的内容，可包含中英文、数字及特殊符号',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  watch: {
    formData: {
      handler(newVal) {
        this.changeNum++
        $emit(this, 'changeNum', this.changeNum)
      },
      deep: true,
    },
  },
  created() {
    vm = this
  },
  // 挂载到Dom完成时
  mounted: function () {
    commonUtil.resizeHeight(300, 500, 'slidebar-container', 'screenSetSNId')
  },
  methods: {
    beforeConfirm() {
      vm.dialogTitle = '确认要发布吗？'
      if (this.formData.openFlag == 1) {
        let flag = false
        vm.$refs['formData'].validate((valid) => {
          if (valid) {
            flag = true
          } else {
            flag = false
          }
        })
        if (!flag) return
      }
      vm.isCancleBtn = false
      vm.isShowDialog = true
    },

    //提交公告信息
    confirmEdit(needJump) {
      openLoading(vm)
      // vm.formData.projectId = vm.projectId
      vm.POST('portal/noticeInfo/insertOrUpdate', vm.formData).then((res) => {
        closeLoading(vm)
        if (res.code == '000000') {
          // if(needJump) {
          vm.$router.push('/item-manager')
          vm.$message.success('发布成功')
          // }
        }
      })
    },
    // 点击取消按钮
    cancleEdit() {
      vm.dialogTitle = '确定取消吗？'
      vm.isCancleBtn = true
      vm.isShowDialog = true
    },

    // 切换开启状态
    changeOpenFlag(value) {
      if (vm.formData.noticeId && value == 1) {
        vm.isCancleBtn = false
        vm.isShowDialog = true
      }
    },

    // 确认取消
    cancleConfirm() {
      vm.isShowDialog = false
      vm.$router.push('/item-manager')
    },

    // 确认开启的取消
    openFlagCancle() {
      vm.isShowDialog = false
      // vm.formData.openFlag = 2;
      // vm.$forceUpdate();
    },

    // 确认开启
    openFlagConfirm() {
      vm.isShowDialog = false
      vm.confirmEdit()
    },
  },
  emits: ['changeNum'],
}
</script>

<style lang="scss" scoped>
.send-notice-wrapper {
  .component-content {
    background: #fff;
    padding: 20px 30px;
    .header-title {
      padding: 10px 12px;
      font-size: 12px;
      color: #449284;
      border-bottom: 1px solid #efefef;
    }
    .complete {
      float: right;
    }
    .text-black {
      color: #d51f35;
    }
  }
  .word-num {
    font-size: 12px;
    color: #999;
    padding-top: 5px;
  }
  .el-input {
    width: 80%;
  }
  .el-textarea {
    width: 80%;
  }
  .el-form-item__error {
    left: 13px;
  }
  .prot-dialog-tips-1 {
    text-align: center !important;
  }
}
</style>
