<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-form-item label="封样图片">
                <el-image
                    ref="preview"
                    v-for="img in dataInfo.imageUrls.split(',')"
                    :src="getImageUrl(img)"
                    style="width: 120px; height: 120px; margin-right: 15px"
                    @click="onPreview(img)"
                  ></el-image>
            </el-form-item>
            <el-form-item label="封样名称">
                {{ dataInfo.materialName }}
            </el-form-item>
            <el-form-item label="使用部位">
                {{ dataInfo.usePosition }}
            </el-form-item>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="指定品牌">
                        {{ dataInfo.contractBrand }}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="拟用品牌">
                        {{ dataInfo.useBrand }}
                    </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>{{ parseTime(dataInfo.sealDate, "{y}-{m}-{d} {h}:{i}") }}</span>
            </el-form-item>
            <el-form-item label="会签单" v-if="dataInfo.signFiles != null">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-paperclip"
                    @click="handleDownload(dataInfo.signFiles)"
                    >下载会签单</el-button
                >
            </el-form-item>
            <el-form-item label="变更单" v-if="dataInfo.alterationFiles != null">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-paperclip"
                    @click="handleDownload(dataInfo.alterationFiles)"
                    >下载变更单</el-button
                >
            </el-form-item>
            <el-form-item label="封样结果" prop="materialResult">
                <el-radio
                v-model="form.materialResult"
                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">
                <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 {
  getMaterialSeal,
  updateMaterialSeal,
 } from "@/api/project/materialSeal";
  import ElImageViewer from "element-ui/packages/image/src/image-viewer";
  
  export default {
    name: "approveMeasureDrawer",
    components: {
      ElImageViewer,
    },
    props: {
        closeCallBack: {
            type: Function,
        },
    },
    dicts: ["project_check_status","project_measure_type", "project_checking_result", "project_measure_info_type"],
    data() {
      return {
        showViewer: false,
        open: false,
        isOpen: false,
        dataInfo: {
            imageUrls:""
        },
        title: "",
        form: {},
        rules: {
          materialResult: [{ 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;
        getMaterialSeal(options.id)
          .then((res) => {
            this.dataInfo = res.data;
            this.loading = false;
          });
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      /** 下载附件 */
      handleDownload(val) {
        let files = val.split(",");
        files.forEach((item) => {
            this.$download.resource(item);
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            // 结果状态同步到审批状态
            let msg = "合格";
            if(this.form.materialResult=="1"){
                this.form.approveStatus="4";
            }else{
                msg = "不合格";
                this.form.approveStatus="3";
            }
            this.$confirm('是否确认审批'+msg+'?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.loading=true;
                updateMaterialSeal(this.form).then(res => {
                    this.$modal.msgSuccess("审批成功");
                    this.loading = false;
                    //关闭并刷新列表
                    this.$refs.drawer.closeDrawer();
                });
            }).catch(() => {
                console.log("取消操作");         
            });
          }
        });
      },
    },
  };
  </script>
  