<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('开项分类')" prop="dangerSort">
        <el-select v-model="form.dangerSort" :placeholder="$t('请选择') + $t('开项分类')">
          <el-option
            v-for="dict in dangerSortOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('发现隐患时间')" prop="discoveryTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="form.discoveryTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('请选择') + $t('发现隐患时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('隐患描述')">
        <el-input v-model="form.hiddendangerContent" :placeholder="$t('请输入') + $t('隐患描述')"/>
      </el-form-item>
      <el-form-item :label="$t('执行人')" prop="name">
        <el-select v-model="form.executorId" filterable :placeholder="$t('请选择') + $t('执行人')">
          <el-option
            v-for="dict in userOptions"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId">
            <span style="float: left">{{ dict.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dept.deptName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('改造措施')" prop="measures">
        <el-input v-model="form.measures" :placeholder="$t('请输入') + $t('改造措施')" />
      </el-form-item>
      <el-form-item :label="$t('预计完成时间')" prop="completeTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="form.completeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('请选择') + $t('预计完成时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('参会人员')" prop="participants">
        <el-select v-model="participantIds" multiple :placeholder="$t('请选择') + $t('参会人员')">
          <el-option
            v-for="dict in userOptions"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId">
            <span style="float: left">{{ dict.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dept.deptName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('上传附件')" prop="annex">
        <el-upload
          ref="upload"
          :limit="50"
          :headers="upload.headers"
          :action="upload.url + '?pType=' + upload.pType + '&pId=' + upload.pId"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            {{ $t('将文件拖到此处，或') }}
            <em>{{ $t('点击上传') }}</em>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item :label="$t('备注')" prop="remarks">
        <el-input v-model="form.remarks" :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="cancel">{{ $t('取 消') }}</el-button>
    </div>
  </div>
</template>

<script>
  import { listApprovedanger, getApprovedanger, delApprovedanger, addApprovedanger, updateApprovedanger, exportApprovedanger, importTemplate, saveApprovedanger } from "@/api/ehs/approvedanger";
  import { listUser } from "@/api/system/user";
  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: "Approvalcommon",
    components: { Treeselect },
    data() {
      return {
        // 隐患申请表格数据
        approvedangerList: [],
        // 部门树选项
        deptOptions: [],
        clientHeight:300,
        // 装置名称字典
        plantCodeOptions: [],
        //人员表联查
        userOptions: [],
        // 开项分类字典
        dangerSortOptions: [],
        //人员表查询条件
        userQueryParams: {},
        // 用户导入参数
        upload: {
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/commonfile/uploadFile",
          // 报告附件上传位置编号
          ids: 0,
          commonfileList: null,
          queryParams: {
            pId: null,
            pType: 'approvalmajor'
          },
          pType: 'approvalmajor',
          pId: null
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          plantCode: null,
          recorderId: null,
          executorId: null,
          hiddendangerLevel: null,
          hiddendangerContent: null,
          measures: null,
          completeTime: null,
          status: null,
          creattime: null,
          endtime: null,
          processId: null,
          approveNo: null,
          deptId: null,
          discoveryTime: null,
          remarks: null,
          participants: null,
          dangerSort: null
        },
        // 表单参数
        form: {},
        //参会人员id
        participantIds: [],
        // 表单校验
        rules: {
          plantCode: [
            { required: true, message: this.$t('装置名称') + this.$t('不能为空'), trigger: "change" }
          ],
          executorId: [
            { required: true, message: this.$t('执行人') +"ID" +this.$t('不能为空'), trigger: "blur" }
          ],
          hiddendangerContent: [
            { required: true, message: this.$t('隐患描述') + this.$t('不能为空'), trigger: "blur" }
          ],
          measures: [
            { required: true, message: this.$t('改造措施') + this.$t('不能为空'), trigger: "blur" }
          ],
          completeTime: [
            { required: true, message: this.$t('预计完成时间') + this.$t('不能为空'), trigger: "blur" }
          ],
        }
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getTreeselect();
      this.getUser();
      this.getDicts("PLANT_DIVIDE").then(response => {
        this.plantCodeOptions = response.data;
      });
      this.getDicts("DANGER_SORT").then(response => {
        this.dangerSortOptions = response.data;
      });
    },
    methods: {
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 装置名称字典翻译
      plantCodeFormat(row, column) {
        return this.selectDictLabel(this.plantCodeOptions, row.plantCode);
      },
      // 开项分类字典翻译
      dangerSortFormat(row, column) {
        return this.selectDictLabel(this.dangerSortOptions, row.dangerSort);
      },
      //获取用户表
      getUser() {
        listUser(this.userQueryParams).then(response => {
          this.userOptions = response.rows;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          plantCode: null,
          recorderId: null,
          executorId: null,
          hiddendangerLevel: null,
          hiddendangerContent: null,
          measures: null,
          completeTime: null,
          status: 0,
          creattime: null,
          endtime: null,
          processId: null,
          approveNo: null,
          deptId: null,
          delFlag: null,
          discoveryTime: null,
          remarks: null,
          participants: null,
          dangerSort: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.form.hiddendangerLevel = 2;
        this.form.status = 16;
        var id = null;
        this.participantIds.forEach(function (value,key,arr) {
          if (key != 0) {
            id = id + "," + value;
          }else if (key == 0) {
            id = value;
          }
        })
        this.form.participants = id;
        this.$refs["form"].validate(valid => {
          if (valid) {
            saveApprovedanger(this.form).then(response => {
              this.msgSuccess(this.$t('新增成功'));
              this.open = false;
            });
          }
        });
      },
      // 文件上传中处理
      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>
