<template>
  <div class="item-shield">
    <bread-crumb :curmbFirst="curmbFirst" :curmbSecond="curmbSecond"></bread-crumb>
    <div class="component-content screenSet" id="screenSet">
      <div class="header-title" style="position:relative;">
        公告
        <el-button
          type="primary"
          style="position: absolute;right: 30px;bottom: 8px;"
          size="small"
          @click="confirmEdit()"
        >确定</el-button>
        <el-button
          type="primary"
          style="position: absolute;right: 120px;bottom: 8px;"
          size="small"
          @click="cancleEdit()"
        >取消</el-button>
      </div>
      <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">
          <el-col :span="22" style="min-width: 600px;">
            <el-form-item label="是否开启公告：">
              <el-radio-group size="small" v-model="formData.openFlag" @change="changeOpenFlag">
                <el-radio :label="1">开启</el-radio>
                <el-radio :label="2">关闭</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col v-show="formData.openFlag != 2" :span="22">
            <el-form-item label="标题：" prop="noticeTitle">
              <el-input size="small" v-model="formData.noticeTitle" placeholder="请输入公告标题"></el-input>
            </el-form-item>
          </el-col>
          <el-col v-show="formData.openFlag != 2" :span="22">
            <el-form-item label="内容：" prop="noticeContent">
              <el-input size="small" v-model="formData.noticeContent" placeholder="请输入公告内容"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>

    <el-dialog class="prot-dialog" title="" :visible.sync="isShowDialog" width="30%" center :show-close=true>
      <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> -->
      <span v-show="isCancleBtn" slot="footer" class="dialog-footer">
        <el-button size="small" @click="cancleConfirm"> 确定取消 </el-button>
        <el-button size="small" type="primary" @click="isShowDialog = false"> 继续操作 </el-button>
      </span>
      <span v-show="!isCancleBtn" slot="footer" class="dialog-footer">
        <el-button size="small" @click="openFlagCancle"> 取消 </el-button>
        <el-button size="small" type="primary" @click="openFlagConfirm"> 确定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
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
  },
  data() {
    return {
      curmbFirst: "教培项目",
      curmbSecond: "公告管理",
      projectId: "",
      formData: {
        noticeContent: "",
        noticeId: 0,
        noticeTitle: "",
        openFlag: 0,
        projectId: 0
      },
      isShowDialog: false,
      isCancleBtn: true,
      rules: {
        noticeTitle: [
          { required: true, message: "请输入公告标题", trigger: "blur" },
        ],
        noticeContent: [
          { required: true, message: "请输入公告内容", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    vm = this;
    vm.projectId = vm.getUrlSearch(window.location.href, "projectId");
    vm.formData.projectId = vm.projectId
    vm.getNoticeInfo();
  },
  // 挂载到Dom完成时
  mounted: function() {
    commonUtil.resizeHeight();
  },
  methods: {
    //获取公告信息
    getNoticeInfo() {
      let req = {
        projectId: vm.projectId
      };
      openLoading(vm);
      vm.GET("portal/noticeInfo/getNoticeInfo", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          vm.formData = res.data;
        }
      });
    },

    //提交公告信息
    confirmEdit() {
      let flag = false;
      vm.$refs['formData'].validate(valid => {
        if (valid) {
          flag = true;
        } else {
          flag = false;
        }
      });
      if(!flag) return;

      openLoading(vm);
      vm.formData.projectId = vm.projectId
      vm.POST("portal/noticeInfo/insertOrUpdate", vm.formData).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          vm.$router.push('/item-manager');
        }
      });
    },
    // 点击取消按钮
    cancleEdit() {
      vm.isCancleBtn = true;
      vm.isShowDialog = true;
    },

    // 切换开启状态
    changeOpenFlag(value) {
      if(vm.formData.noticeId && vm.formData.getNoticeInfo && 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();
    }
  }
};
</script>
<style lang="scss">
.item-shield {
  .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;
    }
  }
}
</style>