<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预案标题" prop="planTitle">
        <el-input
          v-model="queryParams.planTitle"
          placeholder="请输入预案标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预案类型" prop="planType">
        <el-select v-model="queryParams.planType" placeholder="请选择预案类型" clearable size="small">
          <el-option
            v-for = "dict in planTypeList"
            :key = "dict.dictValue"
            :label = "dict.dictLabel"
            :value = "dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预案等级" prop="planLevel">
        <el-select v-model="queryParams.planLevel" placeholder="请选择预案等级" clearable size="small">
          <el-option
            v-for = "dict in planLevelList"
            :key = "dict.dictValue"
            :label = "dict.dictLabel"
            :value = "dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属企业" prop="beyondEnterpriseId">
        <el-select v-model="queryParams.beyondEnterpriseId" placeholder="请选择预案等级" clearable size="small">
          <el-option
            v-for = "dict in enterpriseList"
            :key = "dict.enterpriseId"
            :label = "dict.enterpriseName"
            :value = "dict.enterpriseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:planInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:planInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:planInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:planInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="预案id" align="center" prop="planId" />-->
      <el-table-column label="预案标题" align="center" prop="planTitle" />
      <el-table-column label="预案类型" align="center" prop="planType" :formatter="planTypeFormat"/>
      <el-table-column label="预案等级" align="center" prop="planLevel"  :formatter="planLevelFormat"/>
      <el-table-column label="所属企业名称" align="center" prop="beyondEnterpriseName" />
      <el-table-column label="应急方案" align="center" prop="planContents" />
      <el-table-column label="应急设备及车辆" align="center" prop="planEquipment" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:planInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:planInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应急预案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <div class="division">
          <div style="width: 50%;">
            <el-form-item label="预案标题" prop="planTitle">
              <el-input v-model="form.planTitle" placeholder="请输入预案标题" />
            </el-form-item>
            <el-form-item label="预案类型" prop="planType">
              <el-select v-model="form.planType" placeholder="请选择预案类型">
                <el-option
                  v-for = "dict in planTypeList"
                  :key = "dict.dictValue"
                  :label = "dict.dictLabel"
                  :value = "dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="预案等级" prop="planLevel">
              <el-select v-model="form.planLevel" placeholder="请选择预案等级">
                <el-option
                  v-for = "dict in planLevelList"
                  :key = "dict.dictValue"
                  :label = "dict.dictLabel"
                  :value = "dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="预案图片" prop="iconUrl">
              <el-upload
                :action="uploadImgUrl"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-success ="uploadsuccess"
                :on-remove="handleRemove"
                :file-list="fileList">
                <i class="el-icon-plus"></i>
                <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
              </el-upload>

              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
            </el-form-item>
            <el-form-item label="应急方案" prop="planContents">
              <el-upload
                class="upload-demo"
                :action="uploadImgUrl"
                :on-success="uploadsuccess2"
                :on-remove="handleRemove2"
                :file-list="fileList2"
                list-type="text">
                <el-button size="small" id = "uploadfang" type="primary">点击上传</el-button>
                <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
              </el-upload>
            </el-form-item>
          </div>
          <div style="width: 50%;">
            <el-form-item label="所属企业" prop="beyondEnterpriseId">
              <el-select v-model="form.beyondEnterpriseId" placeholder="请选择预案等级" @change="qiyechang">
                <el-option
                  v-for = "dict in enterpriseList"
                  :key = "dict.enterpriseId"
                  :label = "dict.enterpriseName"
                  :value = "dict.enterpriseId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="应急设备车辆" prop="planEquipment">
              <el-input v-model="form.planEquipment" placeholder="请输入应急设备及车辆" />
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" />
            </el-form-item>
          </div>
        </div>


        <!--<el-form-item label="所属企业名称" prop="beyondEnterpriseName">-->
          <!--<el-input v-model="form.beyondEnterpriseName" placeholder="请输入所属企业名称" />-->
        <!--</el-form-item>-->




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlanInfo, getPlanInfo, delPlanInfo, addPlanInfo, updatePlanInfo, exportPlanInfo,enterpriseList } from "@/api/system/planInfo";

export default {
  name: "PlanInfo",
  components: {
  },
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急预案表格数据
      planInfoList: [],
      planTypeList:[],
      planLevelList:[],
      enterpriseList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogImageUrl: '',
      dialogVisible: false,
      fileList:[],
      fileList2:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planTitle: null,
        planType: null,
        planLevel: null,
        beyondEnterpriseId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        beyondEnterpriseId: [
          { required: true, message: "所属企业不能为空", trigger: "blur" }
        ],
        planType: [
          { required: true, message: "预案类型不能为空", trigger: "blur" }
        ],
        planLevel: [
          { required: true, message: "预案等级不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList()
    this.getDicts("t_yuan_type").then(response =>{
      this.planTypeList = response.data;
    })
    this.getDicts("t_plan_level").then(response =>{
      this.planLevelList = response.data;
    })
  },
  methods: {
    /** 查询应急预案列表 */
    getList() {
      this.loading = true;
      listPlanInfo(this.queryParams).then(response => {
        this.planInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //公司列表
    getEnterpriseList() {
      enterpriseList(this.queryParams).then(response => {
        this.enterpriseList = response.data;
      });
    },
    qiyechang(value){
      let obj = {};
       obj = this.enterpriseList.find((item)=>{
             return item.enterpriseId === value;
       });
       this.form.beyondEnterpriseName = obj.enterpriseName;
    },
    //上传图片
    uploadsuccess(response){
      this.form.iconUrl=response.url;
      //console.log(document.getElementsByClassName("el-upload--picture-card"))
      document.getElementsByClassName("el-upload--picture-card")[0].style.display="none";
    },
    handleRemove(file, fileList) {
      document.getElementsByClassName("el-upload--picture-card")[0].style.display="";
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //上传方案
    handleRemove2(file, fileList) {
      document.getElementById("uploadfang").style.display="";
    },
    uploadsuccess2(response){
      this.form.planContents=response.url;
      document.getElementById("uploadfang").style.display="none";
    },
    // 取消按钮
    cancel() {
      this.fileList = [];
      this.fileList2 = [];
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        planTitle: null,
        planType: null,
        planLevel: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        planContents: null,
        planEquipment: null,
        iconUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.fileList = [];
      this.fileList2 = [];
      this.reset();
      this.open = true;
      this.title = "添加应急预案";
      document.getElementsByClassName("el-upload--picture-card")[0].style.display="";
      document.getElementById("uploadfang").style.display="";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.fileList = [];
      this.fileList2 = [];
      this.reset();
      const planId = row.planId || this.ids
      getPlanInfo(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急预案";
        if(row.iconUrl!=null||row.iconUrl==""){
          this.fileList = [{name: 'file', url: row.iconUrl}];
          this.$nextTick(()=>{
            document.getElementsByClassName("el-upload--picture-card")[0].style.display="none"
        })
        }
      if(row.planContents!=null||row.planContents==""){
        this.fileList2 = [{name: 'file2', url: row.planContents}];
        this.$nextTick(()=>{
          document.getElementById("uploadfang").style.display="none"
      })
      }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updatePlanInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.fileList = [];
            });
          } else {
            addPlanInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.fileList = [];
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$confirm('是否确认删除应急预案编号为"' + planIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlanInfo(planIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    // 事件类型：1.泄漏 2.火灾 3.爆炸字典翻译
    planTypeFormat(row, column) {
      return this.selectDictLabel(this.planTypeList, row.planType);
    },
    // 事件等级字典翻译
    planLevelFormat(row, column) {
      return this.selectDictLabel(this.planLevelList, row.planLevel);
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有应急预案数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPlanInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
  .division{
    display:flex;
    flex-direction:row;
    justify-content:flex-start;
  }
</style>
