<template>
  <!-- 添加或修改可靠性申请对话框 -->
  <el-dialog  :close-on-click-modal="false" :title="$t('提交申请')" :visible.sync="visible" width="1200px" append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="" label-width="80px">
    <el-form-item :label="$t('申请设备')">
      <el-table
        v-show="dataForm.approveType != 7 && dataForm.devType == 1"
        :data="devList"
        border
        style="width: 100%;">
        <el-table-column
          prop="plantCode"
          header-align="center"
          align="center"
          :label="$t('装置名称')">
        </el-table-column>
        <el-table-column
          prop="devno"
          header-align="center"
          align="center"
          :label="$t('位号')">
        </el-table-column>
        <el-table-column
          prop="devname"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          width="150"
          :label="$t('设备名称')">
        </el-table-column>
        <el-table-column
          prop="useno"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('使用证号码')">
        </el-table-column>
        <el-table-column
          prop="medium"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('介质')">
        </el-table-column>
        <el-table-column
          prop="desPressure"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('设计压力')+'（MPa）'">
        </el-table-column>
        <el-table-column
          prop="desTemp"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('设计温度')+'（℃）'">
        </el-table-column>
      </el-table>
      <el-table
        v-show="dataForm.approveType != 7 && dataForm.devType == 2"
        :data="devList"
        border
        style="width: 100%;">
        <el-table-column
          prop="plantCode"
          header-align="center"
          align="center"
          width="100"
          :label="$t('装置名称')">
        </el-table-column>
        <el-table-column
          prop="devno"
          header-align="center"
          align="center"
          width="120"
          :label="$t('位号')">
        </el-table-column>
        <el-table-column
          prop="starting"
          header-align="center"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          :label="$t('起点')">
        </el-table-column>
        <el-table-column
          prop="ending"
          header-align="center"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          :label="$t('终点')">
        </el-table-column>
        <el-table-column
          prop="medium"
          header-align="center"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          :label="$t('介质')">
        </el-table-column>
        <el-table-column
          prop="desPressure"
          header-align="center"
          align="center"
          :label="$t('设计压力')">
        </el-table-column>
        <el-table-column
          prop="desTemp"
          header-align="center"
          align="center"
          :label="$t('设计温度')">
        </el-table-column>
        <el-table-column
          prop="optPressure"
          header-align="center"
          align="center"
          :label="$t('工作压力')">
        </el-table-column>
        <el-table-column
          prop="optTemp"
          header-align="center"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          :label="$t('工作温度')">
        </el-table-column>
      </el-table>
      <el-table
        v-show="dataForm.devType == 3"
        :data="devList"
        border
        style="width: 100%;">
        <el-table-column
          prop="plantCode"
          header-align="center"
          align="center"
          :label="$t('装置名称')">
        </el-table-column>
        <el-table-column
          prop="docno"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('档案号')">
        </el-table-column>
        <el-table-column
          prop="devname"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          width="150"
          :label="$t('设备名称')">
        </el-table-column>
        <el-table-column
          prop="capacity"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('起吊重量')+'(ton)'">
        </el-table-column>
        <el-table-column
          prop="location"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('使用地点')">
        </el-table-column>
      </el-table>
      <el-table
        v-show="dataForm.devType == 4"
        :data="devList"
        border
        style="width: 100%;">
        <el-table-column
          prop="plantCode"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('装置名称')">
        </el-table-column>
        <el-table-column
          prop="devno"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('位号')">
        </el-table-column>
        <el-table-column
          prop="devname"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          width="120"
          :label="$t('设备名称')">
        </el-table-column>
        <el-table-column
          prop="useno"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          :label="$t('使用证号码')">
        </el-table-column>
        <el-table-column
          prop="desTemp"
          header-align="center"
          align="center"
          :label="$t('设计温度')+'（℃）'">
        </el-table-column>
        <el-table-column
          prop="desPressure"
          header-align="center"
          align="center"
          :label="$t('设计压力')+'（MPa）'">
        </el-table-column>
      </el-table>

    </el-form-item>
      <el-form-item :label="$t('审批类型')" prop="approveType">
        <el-select v-model="dataForm.approveType" :placeholder="$t('审批类型')" @change="handleChange">
          <el-option
            v-for="dict in approveOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="!this.showDelay" :label="$t('原因')+$t('空格')+$t('描述')" prop="content">
        <el-input type="textarea" :placeholder="$t('原因')+$t('空格')+$t('描述')"
                  v-model="dataForm.content"
                  maxlength="200" show-word-limit
                  :autosize="{ minRows: 2, maxRows: 10}"></el-input>
      </el-form-item>


      <el-form-item :label="$t('可靠性经理')" prop="kkxjl">
        <el-select v-model="dataForm.kkxjl" filterable :placeholder="$t('请选择') + $t('可靠性经理')">
          <el-option
            v-for="item in gyzjOption"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>

      <template v-if="dataForm.approveType == 5">
        <el-form-item :label="$t('延期日期')" prop="delayDate">
          <el-form-item prop="endTime">
            <el-date-picker
              v-model="dataForm.delayDate"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('selectDate')" >
            </el-date-picker>
          </el-form-item>
        </el-form-item>
        <el-form-item :label="$t('延期原因')" prop="delayReason">
          <el-input type="textarea" :placeholder="$t('延期原因')"
                    v-model="dataForm.delayReason"
                    :autosize="{ minRows: 2, maxRows: 50}" ></el-input>
        </el-form-item>
        <el-form-item :label="$t('延期措施')" prop="delayMeasure">
          <el-input type="textarea" :placeholder="$t('延期措施')"
                    v-model="dataForm.delayMeasure"
                    :autosize="{ minRows: 2, maxRows: 50}" ></el-input>
        </el-form-item>
        <el-form-item :label="$t('延期责任安全声明')" prop="delayNotice">
          <el-input type="textarea" :placeholder="$t('延期责任安全声明')"
                    v-model="dataForm.delayNotice"
                    :autosize="{ minRows: 2, maxRows: 50}" ></el-input>
        </el-form-item>
      </template>
    </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>
  </el-dialog>
</template>

<script>
  import {getToken} from "@/utils/auth";
  import {addApprove, getApprove} from "@/api/reliability/approve";
  import {listPostUser} from "@/api/system/user";

  export default {
  name: "tapprove-add",
  data() {
    return {
      visible: false,
      devList: [],
      wxjlOption:[],
      gyjlOption:[],
      gyzjOption:[],
      wxzjOption:[],
      zfOption:[],
      fileTips: '',
      showDelay: false,
      dataForm: {
        id: 0,
        userId: '',
        devId: '',
        devType: '',
        approveType: '',
        content: '',
        fileUrls: '',
        reUrls: '',
        status: '',
        createTime: '',
        plantCode: '',
        unit: '',
        devname: '',
        devno: '',
        files: [],
        delayDate: '',
        delayReason: '',
        delayMeasure: '',
        delayNotice: '',
        wxjl: '',
        kkxjl: '',
        gyjl: '',
        wxzj: '',
        zf: ''
      },
      doc: {
        file: "",
        // 是否显示弹出层（报告附件）
        open: false,
        // 弹出层标题（报告附件）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 报告附件上传位置编号
        ids: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sems/specfile/uploadFile",
        commonfileList: null,
        pType: 'reliability',
        pId: null
      },
      dataRule: {
        approveType: [
          { required: true, message: this.$t('approveType') + this.$t('notEmpty'), trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.$t('content') + this.$t('notEmpty'), trigger: 'blur' }
        ],
        wxjl: [
          { required: true, message: this.$t('content') + this.$t('notEmpty'), trigger: 'blur' }
        ],
        wxzj: [
          { required: true, message: this.$t('content') + this.$t('notEmpty'), trigger: 'blur' }
        ],
        gyjl: [
          { required: true, message: this.$t('content') + this.$t('notEmpty'), trigger: 'blur' }
        ],
        kkxjl: [
          { required: true, message: this.$t('content') + this.$t('notEmpty'), trigger: 'blur' }
        ],
        zf: [
          { required: true, message: this.$t('content') + this.$t('notEmpty'), trigger: 'blur' }
        ]
      },
      approveOption: [],
      belong: '',
      fileList: [],
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {

  },
  methods: {
    init (rows) {
      this.fileList = []
      this.devList = rows

      listPostUser({
        // postCode: 'kkxjl'
      }).then(response => {
        this.gyzjOption = response;
      });



      this.getDicts("INTACT_APPROVE_TYPE").then(response => {
        this.approveOption = response.data;
        for (let i = 0; i < this.approveOption.length ; i++) {
          if(this.approveOption[i].dictValue == this.devList[0].status){
            this.approveOption.splice(i ,1)
          }
        }
      });

      this.visible = true

      for (let i = 0; i < rows.length; i++) {
        if (i == 0){
          this.dataForm.devId = rows[i].id
        }else {
          this.dataForm.devId += ',' + rows[i].id
        }
      }
    //  this.belong = type + '_'  + '/' + new Date().getTime()
    },
    // 设备类型：字典翻译
    devTypeFormat(row, column) {
      return this.selectDictLabel(this.devTypeOptions, row.devType);
    },
    // 审批类型字典翻译
    approveTypeFormat(row, column) {
      return this.selectDictLabel(this.approveTypeOptions, row.approveType);
    },
    // 取消按钮
    cancel() {
      this.$emit('refreshDataList')
      this.visible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        devId: null,
        devType: null,
        approveType: null,
        content: null,
        fileUrls: null,
        reUrls: null,
        status: 0,
        creattime: null,
        endtime: null,
        processId: null,
        govDate: null,
        delayDate: null,
        delayReason: null,
        delayMeasure: null,
        delayNotice: null,
        apNo: null,
        checkDate: null,
        reportId: null,
        monthId: null,
        delFlag: null,
        deptId: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加特种设备申请');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApprove(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('修改特种设备申请');
      });
    },
    //附件上传中处理
    handleFileDocProgress(event, file, fileList) {
    },
    handleRemove (file, fileList) {
      this.fileList = fileList
    },
    //附件上传成功处理
    handleFileDocSuccess(response, file, fileList) {
      this.fileList = fileList
      if (response.code == 200){
        this.$alert(this.$t('导入成功'), this.$t('导入结果'), { dangerouslyUseHTMLString: true });
      }else {
        this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true });
      }

    },
    /** 提交按钮 */
    submitForm() {
      this.dataForm.files = []
      console.log(this.fileList)
      if (this.fileList.length > 0) {
        for (let i = 0; i < this.fileList.length; i++) {
          let obj = {}
          obj.fileName = this.fileList[i].name
          obj.fileUrl = this.fileList[i].response.msg
          this.dataForm.files.push(obj)
        }
      }
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
            addApprove(this.dataForm).then(response => {
              this.msgSuccess(this.$t('新增成功'));
              this.visible = false;
            });
        }
      });
      this.$emit('refreshDataList')
    },
    handleChange(value) {
      this.showDelay = false
      if (value == -1) {
        this.fileTips = '提示：请提供相关支持文件'
      }else if (value == 1) {
        this.fileTips = '提示：请提供启用前检查报告'
      }else if (value == 2) {
        this.fileTips = ''
      }else if (value == 3) {
        this.fileTips = '提示：请提供设备主体破坏后的照片，防止被其他单位买走重新使用'
      }else if (value == 4) {
        this.fileTips = '提示：请提供相关支持文件'
      }else if (value == 5) {
        this.showDelay = true
        this.fileTips = '提示：延期发起的会议纪要等背景情况'
        this.dataForm.delayReason=
          '根据2015年压力管道检验报告，清单中的相关管道材质中度球化，安全等级定为3级，下次检验日期定为2018年5月。\n' +
          '由于这些管道连接电厂蒸汽母管，无法单独隔绝，只有在2020年扬巴大修期间才有机会停车检验，因此提出延期申请。\n'
        this.dataForm.delayMeasure=
          '（生产部门，应该根据实际情况，组织机械，电仪各专业，制定具体的运行监控措施描述）\n' +
          '\n' +
          '工艺部门：\n' +
          '维持相关管道操作稳定\n' +
          '持续监控相关管道操作压力和温度\n' +
          '按照法规要求，定期开展应急演练\n' +
          '\n' +
          '\n' +
          '机修部门：\n' +
          '每月对相关管道进行走线巡检，对管道支吊架，保温目视检查。如有问题，记录在巡检记录中\n' +
          '及时报告管道使用情况及需要解决的问题\n' +
          '发生事故及时组织调查处理，并配合写出事故报告书\n' +
          '\n' +
          '电仪部门：\n' +
          'XXXXXXXXXXX描述电仪方面的维护或监控措施\n'
        this.dataForm.delayNotice=
          '我单位/装置，已制定相应预防措施和应急预案，并保证落实安全管理责任、操作人员持证上岗、安全附件和安全保护装置在有效期内。在检验延期期间，将对以上设备加强巡查和实施有效安全监控，做好监控记录，对延期期间的设备安全负完全责任；在延期期满前，我单位将申报并安排以上设备实施定期检验。'
      }else if (value == 6) {
        this.fileTips = '提示：请提供安全评估报告'
      }else {
        this.fileTips = ''
      }
    },
}
}


</script>

<style scoped>

</style>
