<template>
  <div class="projectect-attendance-drawer">
    <el-drawer
      v-if="isOpen"
      :visible.sync="isOpen"
      ref="drawer"
      direction="rtl"
      @close="closeCallBack"
      size="50%"
    >
      <template slot="title">
        <div>{{ title + " 【处理审批】" }}</div>
      </template>
      <el-form
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="padding-right: 20px; padding-left: 20px"
      >
        <el-form-item label="项目名称">
          {{ dataInfo.projectName }}
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="送检类型">
              <dict-tag
                :options="dict.type.check_detection_check_type"
                :value="dataInfo.checkType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合格证">
              <el-tag v-if="dataInfo.qualifiedFlag == 'Y'" type="success">已提供</el-tag>
              <el-tag v-if="dataInfo.qualifiedFlag == 'N'" type="danger">未提供</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="材料名称">
              {{ dataInfo.materialName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取样数量">
              {{ dataInfo.sampleNum }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="使用部位">
              {{ dataInfo.usePosition }}
            </el-form-item>
            <el-form-item label="监理专员">
              {{ dataInfo.witnessUserName }}
              <el-tag type="info" size="mini">{{ dataInfo.witnessUser }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提交用户">
              {{ dataInfo.createBy }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提交单位">
              {{ dataInfo.deptName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="实验室名">
          <span>{{ dataInfo.laboratoryName }}</span>
        </el-form-item>
        <el-form-item label="送检时间">
          <span>{{ parseTime(dataInfo.checkTime, "{y}-{m}-{d} {h}:{i}") }}</span>
        </el-form-item>
        <el-form-item label="检测报告" v-if="dataInfo.checkState == '2'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-paperclip"
            @click="handleDownload()"
            >下载检测报告</el-button
          >
        </el-form-item>
        <el-form-item label="送检结果" prop="detectionResult">
          <el-radio
            v-model="form.detectionResult"
            v-for="dict in dict.type.project_checking_result"
            :label="dict.value"
            :key="dict.value"
            border
            size="small"
            >{{ dict.label }}</el-radio
          >
        </el-form-item>
        <el-form-item label="审批意见" prop="comment">
          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入审批意见（200字内）"
            v-model="form.comment"
          />
        </el-form-item>
        <div style="text-align: center;margin-bottom:20px;">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-form>
    </el-drawer>
    <el-image-viewer
      v-if="showViewer"
      :on-close="closeViewer"
      :url-list="previewList"
      style="z-index: 2050"
    />
  </div>
</template>

<script>
import { getCheckDetection, updateCheckDetection } from "@/api/project/checkDetection";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";

export default {
  name: "approveCheckDetectionDrawer",
  components: {
    ElImageViewer,
  },
  props: {
    closeCallBack: {
      type: Function,
    },
  },
  dicts: ["project_checking_result", "check_detection_check_type"],
  data() {
    return {
      showViewer: false,
      open: false,
      isOpen: false,
      dataInfo: {},
      title: "",
      form: {},
      rules: {
        detectionResult: [{ required: true, message: "请选择送检结果", trigger: "blur" }],
        comment: [
          { required: true, message: "请输入审批意见", trigger: "blur" },
          { max: 200, message: "审批意见最多200字符", trigger: "blur" },
        ],
      },
      // 遮罩层
      loading: true,
      previewList: [],
    };
  },
  computed: {},
  mounted() {},
  methods: {
    getImageUrl(url) {
      return process.env.VUE_APP_BASE_API + url + ".min.jpg";
    },
    onPreview(urls) {
      this.previewList = [];
      urls.split(",").forEach((item) => {
        this.previewList.push(process.env.VUE_APP_BASE_API + item);
      });
      this.showViewer = true;
    },
    closeViewer() {
      this.showViewer = false;
    },
    show(options) {
      this.title = options.typeName;
      this.isOpen = true;
      this.form = {};
      this.form.id = options.id;
      getCheckDetection(options.id).then((res) => {
        this.dataInfo = res.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.isOpen = false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // 结果状态同步到审批状态
          let msg = "合格";
          if (this.form.detectionResult == "1") {
            this.form.approveStatus = "4";
          } else {
            msg = "不合格";
            this.form.approveStatus = "3";
          }
          this.$confirm("是否确认审批" + msg + "?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.loading = true;
              updateCheckDetection(this.form).then((res) => {
                this.$modal.msgSuccess("审批成功");
                this.loading = false;
                //关闭并刷新列表
                this.$refs.drawer.closeDrawer();
              });
            })
            .catch(() => {
              console.log("取消操作");
            });
        }
      });
    },
    /** 下载附件 */
    handleDownload() {
      let files = this.dataInfo.measureFiles.split(",");
      files.forEach((item) => {
        this.$download.resource(item);
      });
    },
  },
};
</script>
