<!--预案管理-->
<template>
  <div class="app-container control">
    <div class="scrollbar-box" v-if="pageType == 0">
      <div class="page-form-title-box">
        <el-form :model="param" ref="queryForm" :inline="true" label-width="80px">
          <el-form-item label="" prop="name">
            <el-input
              maxlength="10"
              v-model="param.searchValue"
              placeholder="预案名称/创建人"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="" prop="startCondition" label-width="100px">
            <el-select v-model="param.startCondition" placeholder="请选择启动条件" clearable size="small">
              <el-option
                v-for="dict in attributeOptions"
                :key="dict.code"
                :label="dict.name"
                :value="dict.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="float: right;" v-hasPermiRouter="['lab:risk:add']">
            <p class="page-submit-common-style-button" @click="tableClickButton(4)">定制预案</p>
          </el-form-item>
          <el-form-item>
            <p class="page-inquire-common-style-button" @click="onSearch">查询</p>
            <p class="page-reset-common-style-button" @click="resetForm">重置</p>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="tableData">
          <el-table-column label="预案名称" align="center" prop="name" :show-overflow-tooltip="true"/>
          <el-table-column label="启动条件" align="center" prop="startCondition" :show-overflow-tooltip="true" width="340"/>
          <el-table-column label="创建人" align="center" prop="createName" width="180"/>
          <el-table-column label="创建时间" align="center" prop="createTime" width="200">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p" v-hasPermiRouter="['lab:riskSubjectRelation:list']" @click="tableClickButton(1,scope.row)">关联实验室</p>
                <p class="table-button-p" v-show="userId==scope.row.createBy"
                   v-hasPermiRouter="['lab:risk:edit']"
                   @click="tableClickButton(2,scope.row)">编辑</p>
                <p class="table-button-p" v-show="userId==scope.row.createBy"
                   v-hasPermiRouter="['lab:risk:del']"
                   @click="tableClickButton(3,scope.row)">删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :page-sizes="[20, 30, 40, 50]"
          v-show="total>0"
          :total="total"
          layout="total, prev, pager, next, sizes, jumper"
          :page.sync="param.page"
          :limit.sync="param.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
     <!--添加编辑-->
    <add-plan-page v-if="pageType == 1" :addPlanData="addPlanData"></add-plan-page>
    <!--关联实验室-->
    <association-page v-if="pageType == 4" :associationData="associationData"></association-page>
    <!--  关联记录  -->
    <el-dialog title='关联记录' @close="handleClose"  :close-on-click-modal="false" :visible.sync="dialogVisible2" width="80%">
      <div class="scrollbar-box" style="padding:0;margin:0 auto;height:700px;overflow-y: scroll;display: flex;flex-direction: column;flex:1;">
        <relevance-record-page v-if="pageType == 3" :pageData3="pageData3" style="display: flex;flex-direction: column;flex:1;"></relevance-record-page>
      </div>
    </el-dialog>
    <!--新增预案-->
    <el-dialog title="定制预案" v-if="addFormOpen" :close-on-click-modal="false" :visible.sync="addFormOpen" width="500px" append-to-body>
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="100px">
        <el-form-item label="预案名称" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入预案名称" maxLength="15" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="预案音乐" prop="musicId">
          <el-select v-model="addForm.musicId" placeholder="请选择疏散音乐" style="width:320px;">
            <el-option
              v-for="dict in musicOptions"
              :key="dict.id"
              :label="dict.musicName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p >
        <p class="dialog-footer-button-info" @click="cancel">取消</p >
        <p class="dialog-footer-button-primary" style="width:100px;" @click="submitForm">配置规则</p >
        <p class="dialog-footer-button-null"></p >
      </div>
    </el-dialog>
  </div>
</template>

<script>
//                        V3
import { laboratoryMusicFindByType,laboratoryPlanList,laboratoryPlanAdd,
  laboratoryPlanDelete,laboratoryRiskPlanLevelGetRiskPlanLeveList } from "@/api/emergencyManagement/index";
import { iotAttributeGetByTypeKey } from "@/api/iotDevice/index";
import addPlanPage from "./addPlan.vue";
import associationPage from "./associatedPage.vue";

export default {
  components: {
    addPlanPage,
    associationPage
  },
  name: "Plan",
  data() {
    const exportconstvalidateContacts  = (rule, value, callback) => {
      if(!/^[\u0391-\uFFE5_a-zA-Z0-9]+$/.test(value)) {
        callback(new Error("只可输入中文/英文与数字"));
      } else {
        callback()
      }
    };
    return {
      tableButtonType:this.hasPermiDom(['lab:risk:del','lab:risk:edit','lab:riskSubjectRelation:list']),
      //页面状态
      pageType:0,
      // 遮罩层
      loading: false,
      dateRange: [],
      //启动条件
      attributeOptions:[],
      //疏散音乐
      musicOptions:[],
      // 查询参数
      param: {
        page: 1,
        pageSize:20,
        searchValue: null,
        startCondition: null,
      },
      tableData: [],
      total: 0,
      pageData2:{},
      pageData3:{},
      //关联实验室
      dialogVisible:false,
      //关联记录
      dialogVisible2:false,
      dialogTable:[],
      checkedLabList:[],//需要选中的实验室
      joinSubOper:[],
      userId:localStorage.getItem('userId'),
      //新增预案
      addFormOpen:false,
      addForm:{},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入预案名称", trigger: "change" },
          { required: true, message: "请输入预案名称", validator: this.spaceJudgment, trigger: "change" },
          { required: true, validator: exportconstvalidateContacts, trigger: "blur" },
        ],
        musicId: [
          { required: true, message: "请选择疏散音乐", trigger: "change" },
          { required: true, message: "请选择疏散音乐", validator: this.spaceJudgment, trigger: "change" },
        ],
      },
      //关联页面传参数据
      associationData:{},
      addPlanData:{},
    };
  },
  mounted(){
    this.iotAttributeGetByTypeKey();
    this.getList();
    this.laboratoryMusicFindByType();
  },
  methods: {
    // 操作按钮 1.关联 2.编辑 3.删除 4.新增
    tableClickButton(type,item){
      let self = this;
      if(type == 1){
        laboratoryRiskPlanLevelGetRiskPlanLeveList({riskPlanId:item.id}).then((response) => {
          if(!response.data[0]){
            this.msgError('请先添加预案规则');
          }else{
            this.$set(this,'associationData',JSON.parse(JSON.stringify(item)));
            this.$set(this,'pageType',4);
          }
        });
      }else if(type == 2){
        this.$set(this,'addPlanData',JSON.parse(JSON.stringify({id:item.id,name:item.name,musicId:item.musicId})));
        this.$set(this,'pageType',1);
      }else if(type == 3){
        this.$confirm('是否确认删除该预案?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          laboratoryPlanDelete({id:item.id}).then(response => {
            self.msgSuccess(response.message)
            self.getList();
          });
        }).then(() => {
        }).catch(() => {});
      }else if(type == 4){
        this.$set(this,'addFormOpen',true);
        this.$set(this,'addForm',{});
      }else if(type == 5){
        this.$set(this,'pageType',0);
        this.getList();
      }else if(type == 6){
        this.$set(this,'pageType',1);
      }
    },
    //新增预案-关闭
    cancel(){
      this.$set(this,'addFormOpen',false);
      this.$set(this,'addForm',{});
    },
    //新增预案-提交
    submitForm(){
      let self = this;
      this.$refs["addForm"].validate(valid => {
        if (valid) {
          let obj = {
            name:this.addForm.name,
            musicId:this.addForm.musicId
          }
          laboratoryPlanAdd(obj).then((response) => {
            this.msgSuccess(response.message)
            this.$set(this,'addFormOpen',false);
            setTimeout(function(){
              self.tableClickButton(2,{id:response.data,name:self.addForm.name,musicId:self.addForm.musicId});
            },500);
          });
        }
      })
    },
    //查询
    onSearch() {
      this.getList();
    },
    //重置
    resetForm() {
      this.$set(this.param,'searchValue',null);
      this.$set(this.param,'startCondition',null);
      this.onSearch();
    },
    //监听关联记录弹窗关闭
    handleClose(){
      this.pageType=0
    },
    /** 查询启动条件 */
    iotAttributeGetByTypeKey() {
      iotAttributeGetByTypeKey({typeKey:'sensor'}).then(response => {
        this.$set(this, 'attributeOptions', response.data)
      });
    },

    //预案管理-列表
    getList(){
      laboratoryPlanList(this.param).then(response => {
        this.$set(this,'tableData',response.data.records)
        this.$set(this,'total',response.data.total)
        this.loading = false;
      });
    },
    laboratoryMusicFindByType(){
      laboratoryMusicFindByType({musicType:1}).then(response => {
        this.$set(this,'musicOptions',response.data)
      });
    },
  },
}
</script>
<style scoped lang="scss">
/*#app .app-container{
  background: #fff;
  border-radius: 0px;
  overflow: hidden;
  padding: 10px;
  margin: 0;
  box-shadow:none;

}*/


  .control{
    .scrollbar-box{
      overflow: hidden;
      display: flex!important;
      flex-direction: column;
      height: 100%;
      width: 100%;
      box-sizing: border-box;
     /* box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
      padding:20px!important;*/
    }
    .button-box{
      width:326px;
      display: flex;
    }
    .form-box{
      display:flex;
      .null-p{
        flex:1;
      }
    }
  }
</style>

