<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item :label="$t('装置名称')" prop="plantCode">
        <el-select v-model="form.plantCode" :placeholder="$t('请选择') + $t('装置名称')">
          <el-option
            v-for="dict in plantCodeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('事件基本信息')">
        <el-input v-model="form.accidentContent" type="textarea" :rows="2" :placeholder="$t('请输入') + $t('事件基本信息')" />
      </el-form-item>
      <el-form-item :label="$t('事件初步分类')" prop="accidentSort">
        <el-select v-model="form.sortList" multiple :placeholder="$t('请选择') + $t('事件初步分类')">
          <el-option
            v-for="dict in accidentSortOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('是否得到紧急救援') " prop="rescue">
        <el-input v-model="form.rescue" :placeholder="$t('请输入') + $t('是否得到紧急救援')" />
      </el-form-item>
      <el-form-item :label="$t('危害涉及')" prop="involve">
        <el-input v-model="form.involve" :placeholder="$t('请输入') + $t('危害涉及')" />
      </el-form-item>
      <el-form-item :label="$t('归属部门')" prop="deptId">
        <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" :placeholder="$t('请选择') + $t('归属部门')" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">{{ $t('确 定') }}</el-button>
      <el-button @click="empty">{{ $t('清 空') }}</el-button>
    </div>
  </div>
</template>

<script>
import { saveApproveaccident } from "@/api/ehs/approveaccident";
import { treeselect } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Approveaccident",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 事件申请表格数据
      approveaccidentList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: [],
      clientHeight:300,
      // 装置名称字典
      plantCodeOptions: [],
      // 事件初步分类字典
      accidentSortOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        plantCode: null,
        accidentContent: null,
        accidentSort: null,
        rescue: null,
        involve: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        plantCode: [
          { required: true, message: this.$t('装置名称') + this.$t('不能为空'), trigger: "change" }
        ],
        accidentContent: [
          { required: true, message: this.$t('事件基本信息') + this.$t('不能为空'), trigger: "blur" }
        ],
        sortList: [
          { required: true, message: this.$t('事件初步分类') + this.$t('不能为空'), trigger: "blur" }
        ],
        rescue: [
          { required: true, message: this.$t('是否得到紧急救援') + this.$t('不能为空'), trigger: "blur" }
        ],
        involve: [
          { required: true, message: this.$t('危害涉及')  + this.$t('不能为空'), trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
        this.$refs.tree.filter(val);
    }
  },
  created() {
    //设置表格高度对应屏幕高度
    this.$nextTick(() => {
        this.clientHeight = document.body.clientHeight -250
    })
    this.getTreeselect();
    this.getDicts("PLANT_DIVIDE").then(response => {
      this.plantCodeOptions = response.data;
    });
    this.getDicts("ACCIDENT_SORT").then(response => {
      this.accidentSortOptions = response.data;
    });
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 装置名称字典翻译
    plantCodeFormat(row, column) {
      return this.selectDictLabel(this.plantCodeOptions, row.plantCode);
    },
    // 事件初步分类字典翻译
    accidentSortFormat(row, column) {
      return this.selectDictLabel(this.accidentSortOptions, row.accidentSort);
    },
    // 清空按钮
    empty() {
      this.form = {
        id: null,
        plantCode: null,
        recorderId: null,
        accidentContent: null,
        accidentSort: null,
        sortList: null,
        rescue: null,
        involve: null,
        status: 0,
        creattime: null,
        endtime: null,
        processId: null,
        approveNo: null,
        deptId: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.form.status = 1;
      this.$refs["form"].validate(valid => {
        if (valid) {
          saveApproveaccident(this.form).then(response => {
            this.msgSuccess(this.$t('新增成功'));
          });
        }
      });
    },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
          this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
          this.upload.open = false;
          this.upload.isUploading = false;
          this.$refs.upload.clearFiles();
          this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true });
          this.getList();
      },
      // 提交上传文件
      submitFileForm() {
          this.$refs.upload.submit();
      }
  }
};
</script>
