<template>
  <el-form
    class="content-main"
    label-width="200px"
    ref="formQuery"
    :rules="rules"
    :model="formQuery"
  >
    <el-row>
      <el-col :span="24">
        <h3 class="h3">油罐基本信息</h3>
        <!-- <el-button type="primary" @click="comeOn">带出上一条信息</el-button> -->
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="油罐编码:" prop="realYgbh">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.realYgbh"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="油罐名称:" prop="ygmc">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.ygmc"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="罐容(吨):" prop="sjgr">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.sjgr"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="附属设施是否完好:" prop="ygjfssssfwh">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.ygjfssssfwh"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ygjfssssfwhList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="油罐类型:" prop="yglx">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.yglx"
            placeholder="请选择"
          >
            <el-option
              v-for="item in cxOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="有无加热装置:" prop="ywjrzz">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.ywjrzz"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ygjfssssfwhList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="单罐内直径(米):" prop="ygnzj">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.ygnzj"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="高度(米):" prop="yggd">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.yggd"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="检定方式:" prop="jdfs">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.jdfs"
            placeholder="请选择"
          >
            <el-option
              v-for="item in jdfsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="焊接方式:" prop="hjfs">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.hjfs"
            placeholder="请选择"
          >
            <el-option
              v-for="item in hjfsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="油罐状态:" prop="ygzt">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.ygzt"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ygztList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="底面积(平方米):" prop="ygdmj">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.ygdmj"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="设计储油高度(米):" prop="zygd">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.zygd"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="出油口高度(米):" prop="ygcykgd">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.ygcykgd"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="检查口高度(米):" prop="ygjckgd">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.ygjckgd"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="建造日期:" prop="sjnd">
          <el-date-picker
            clearable
            class="inputWidth"
            style="width: 335px"
            :disabled="showLock"
            v-model="formQuery.sjnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          ></el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="建设单位:" prop="jsdw">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.jsdw"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设计单位:" prop="sjdw">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.sjdw"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="监理单位:" prop="jldw">
          <el-input
            clearable
            class="inputWidth"
            :disabled="showLock"
            placeholder="请输入"
            v-model="formQuery.jldw"
          ></el-input>
        </el-form-item>
      </el-col>
      <!-- <el-col :span="12">
        <el-form-item label="出入库状态:" prop="crkStatus">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.crkStatus"
            placeholder="请选择"
          >
            <el-option
              v-for="item in crkStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="储存粮油类型:" prop="lylx">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.lylx"
            placeholder="请选择"
          >
            <el-option
              v-for="item in lylxList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="油罐属性:" prop="status">
          <el-select
            clearable
            class="inputWidth"
            :disabled="showLock"
            v-model="formQuery.status"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ygsxList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          clearable
          label-width="208px"
          label="变更原因(油罐状态变更时，必须填写):"
        >
          <el-input
            clearable
            style="width: 96%"
            :disabled="showLock"
            v-model="formQuery.changeReason"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-form-item label="油罐图：">
        <el-upload
          :action="fileApi"
          list-type="picture-card"
          accept="image/*"
          :limit="1"
          :with-credentials="true"
          :disabled="showLock"
          :on-preview="handleYGPictureCardPreview"
          :on-remove="handleYGRemove"
          :on-success="handleYGSuccess"
          :on-error="handlerError"
          :fileList="ygpmtList"
          :on-exceed="handleExceed"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogYGVisible" title="查看图片" width="40%">
          <img width="100%" :src="ygpmt" alt />
        </el-dialog>
      </el-form-item>
    </el-row>
    <el-row>
      <el-form-item label="油罐库区图：">
        <el-upload
          :action="fileApi"
          list-type="picture-card"
          accept="image/*"
          :limit="1"
          :with-credentials="true"
          :disabled="showLock"
          :on-preview="handleKQPictureCardPreview"
          :on-remove="handleKQRemove"
          :on-exceed="handleExceed"
          :on-error="handlerError"
          :on-success="handleKQSuccess"
          :fileList="ygkqtList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogKQVisible" title="查看图片" width="40%">
          <img width="100%" :src="ygkqt" alt />
        </el-dialog>
      </el-form-item>
    </el-row>
    <el-row v-if="isChecked">
      <el-col :span="24">
        <h3 class="h3">审批信息</h3>
      </el-col>
    </el-row>
    <el-row v-if="isChecked">
      <el-col :span="24">
        <el-form-item label="审批:">
          <el-radio v-model="formQuery.approvalResult" disabled label="1"
            >通过</el-radio
          >
          <el-radio v-model="formQuery.approvalResult" disabled label="0"
            >驳回</el-radio
          >
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="审核意见:">
          <el-input
            style="width: 90%"
            :disabled="$route.query.state != 'audit'"
            v-model="formQuery.approvalComments"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="审核人:">
          <el-input
            disabled
            style="width: 80%"
            v-model="formQuery.approver"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="审核时间:">
          <el-input
            disabled
            style="width: 80%"
            v-model="formQuery.approvalDate"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-col :span="24">
      <div style="text-align: center; margin-top: 20px">
        <el-button type="primary" icon="iconfont iconfanhui" @click="cancel"
          >返回</el-button
        >
      </div>
    </el-col>
  </el-form>
</template>

<script>
import {
  selectEmptyPotApplyListnumber,
  addEmptyPotApplyList,
  updateEmptyPotApplyList,
} from "@/api/grainReserves/accEmptywarehouse/applyManymentMaintain/index";
import { FileApi } from "@/api/index";
import { getNowDate } from "@/utils/index";

export default {
  data() {
    return {
      fileApi: FileApi + "/file/upload",
      downloadPicUrl: `${FileApi}/file/download?id=`,
      ygkqt: "", // 油罐库区图
      ygpmt: "", // 油罐平面图
      dialogYGVisible: false,
      dialogKQVisible: false,
      ygpmtList: [],
      ygkqtList: [],
      pageForm: {
        page: 1,
        size: 10,
        total: 0,
      },
      showLock: true,
      formQuery: {
        cfbh: "",
        realYgbh: "",
        ygmc: "",
        sjgr: "",
        ygjfssssfwh: "",
        yglx: "",
        ywjrzz: "",
        ygnzj: "",
        yggd: "",
        jdfs: "",
        hjfs: "",
        ygzt: "",
        ygdmj: "",
        zygd: "",
        ygcykgd: "",
        ygjckgd: "",
        sjnd: "",
        jsdw: "",
        sjdw: "",
        jldw: "",
        crkStatus: "",
        lylx: "",
        sfba: "",
        approver: "",
        approvalDate: "",
        approvalComments: "",
        approvalDate: "",
        approvalResult: "",
        status: 1,
      },
      tableKey: "",
      isChecked: false,
      ygjfssssfwhList: [
        {
          value: "1",
          label: "是",
        },
        {
          value: "0",
          label: "否",
        },
        {
          value: "9",
          label: "其他或不确定",
        },
      ],
      options: [
        {
          value: 1,
          label: "能",
        },
        {
          value: 0,
          label: "否",
        },
      ],
      optionsStr: [
        {
          value: "1",
          label: "有",
        },
        {
          value: "0",
          label: "无",
        },
      ],
      lylxList: [
        {
          value: "0",
          label: "原油",
        },
        {
          value: "1",
          label: "成品油",
        },
      ],
      ygsxList: [
        {
          value: 1,
          label: "在用",
        },
        {
          value: 0,
          label: "弃用",
        },
      ],
      crkStatusList: [
        {
          value: "1",
          label: "空仓",
        },
        {
          value: "2",
          label: "入库中",
        },
        {
          value: "3",
          label: "封仓",
        },
        {
          value: "4",
          label: "出库中",
        },
      ],
      ygztList: [
        {
          value: "1",
          label: "完好",
        },
        {
          value: "2",
          label: "需小修",
        },
        {
          value: "3",
          label: "需大修",
        },
        {
          value: "4",
          label: "待报废",
        },
        {
          value: "5",
          label: "待拆除",
        },
        {
          value: "6",
          label: "长期闲置",
        },
        {
          value: "9",
          label: "其他",
        },
      ],
      hjfsList: [
        {
          value: "1",
          label: "对接",
        },
        {
          value: "2",
          label: "搭接",
        },
        {
          value: "9",
          label: "其他或不确定",
        },
      ],
      jdfsList: [
        {
          value: "1",
          label: "标定",
        },
        {
          value: "0",
          label: "非标定",
        },
        {
          value: "9",
          label: "其他或不确定",
        },
      ],
      cxOptions: [
        {
          value: "1",
          label: "立式油罐",
        },
        {
          value: "2",
          label: "卧式油罐",
        },
        {
          value: "3",
          label: "油槽(池)",
        },
        {
          value: "4",
          label: "地下油罐",
        },
        {
          value: "5",
          label: "其他",
        },
      ],
      rules: {
        realYgbh: [
          { required: true, message: "请输入油罐编码", trigger: "blur" },
          {
            pattern: /^YG\d{3}?$/,
            message: "油罐编号格式为YG+三位顺序号，如YG001",
            trigger: "blur",
          },
        ],
        ygmc: [{ required: true, message: "请输入油罐名称", trigger: "blur" }],
        sjgr: [{ required: true, message: "请输入设计罐容", trigger: "blur" }],
        ygjfssssfwh: [
          { required: true, message: "请选择是否完好", trigger: "change" },
        ],
        yglx: [
          { required: true, message: "请选择油罐类型", trigger: "change" },
        ],
        ywjrzz: [{ required: true, message: "请选择有无", trigger: "change" }],
        ygnzj: [{ required: true, message: "请输入直径", trigger: "blur" }],
        yggd: [{ required: true, message: "请输入高度", trigger: "blur" }],
        jdfs: [
          { required: true, message: "请选择检定方式", trigger: "change" },
        ],
        hjfs: [
          { required: true, message: "请选择焊接方式", trigger: "change" },
        ],
        ygzt: [
          { required: true, message: "请选择油罐状态", trigger: "change" },
        ],
        ygdmj: [{ required: true, message: "请输入底面积", trigger: "blur" }],
        zygd: [
          { required: true, message: "请输入设计储油高度", trigger: "blur" },
        ],
        ygcykgd: [
          { required: true, message: "请输入出油口高度", trigger: "blur" },
        ],
        sjnd: [{ required: true, message: "请输入建造日期", trigger: "blur" }],
      },
    };
  },
  methods: {
    getInfo(code) {
      selectEmptyPotApplyListnumber({ ygbh: code }).then((res) => {
        this.formQuery = res.data;
        if (res.data.ygpmt) {
          this.parseFileList(res.data.ygpmt, "ygpmtList");
        }
        if (res.data.ygkqt) {
          this.parseFileList(res.data.ygkqt, "ygkqtList");
        }
        this.formQuery.sjgr =
          String(this.formQuery.sjgr).indexOf(".") > -1
            ? Number(this.formQuery.sjgr).toFixed(3)
            : this.formQuery.sjgr;
        if (this.$route.query.state == "audit") {
          if (!res.data.approvalResult) {
            this.formQuery.approvalResult = "1";
            this.formQuery.approver = sessionStorage.getItem("userName");
            this.formQuery.approvalDate = getNowDate(new Date());
          }
        }
      });
    },
    parseFileList(id, type) {
      if (!id) return;
      this[type] = [{ url: this.downloadPicUrl + id }];
    },
    saveSubmit(status) {
      // this.$refs.dialogForm.validate((valid) => {
      // 	if (valid) {
      // 		this.saves()
      // 	} else {
      // 		this.$message.warning('请填写正确的必填信息')
      // 	}
      // })
      this.$refs.formQuery.validate((valid) => {
        if (valid) {
          if (this.$route.query.state == "edit") {
            if (status == "save") {
              this.formQuery.sfba = "1";
              this.formQuery.tyshxydm = sessionStorage.getItem("tyshxydm");
              updateEmptyPotApplyList({
                ...this.formQuery,
              }).then((res) => {
                if (res.infoCode == 200) {
                  this.$message({
                    message: "编辑成功",
                    type: "success",
                  });
                  this.$router.go(-1);
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            } else if (status == "send") {
              this.formQuery.tyshxydm = sessionStorage.getItem("tyshxydm");
              this.formQuery.sfba = "2";
              let data = {};
              updateEmptyPotApplyList({
                ...this.formQuery,
              }).then((res) => {
                if (res.infoCode == 200) {
                  this.$message({
                    message: "提交成功",
                    type: "success",
                  });
                  this.$router.go(-1);
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            }
          } else {
            if (status == "save") {
              this.formQuery.tyshxydm = sessionStorage.getItem("tyshxydm");
              this.formQuery.sfba = "1";
              this.formQuery.kddm = `${sessionStorage.getItem("tyshxydm")}001`;
              addEmptyPotApplyList({
                ...this.formQuery,
              }).then((res) => {
                if (res.infoCode == 200) {
                  this.$message({
                    message: "保存成功",
                    type: "success",
                  });
                  this.$router.go(-1);
                } else {
                  // alert(res.msg)
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            } else if (status == "send") {
              this.formQuery.tyshxydm = sessionStorage.getItem("tyshxydm");
              this.formQuery.sfba = "2";
              this.formQuery.kddm = `${sessionStorage.getItem("tyshxydm")}001`;
              addEmptyPotApplyList({
                ...this.formQuery,
              }).then((res) => {
                if (res.infoCode == 200) {
                  this.$message({
                    message: "提交成功",
                    type: "success",
                  });
                  this.$router.go(-1);
                } else {
                  // alert(res.msg)
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            }
          }
        } else {
          this.$message.warning("请填写正确的必填信息");
        }
      });
    },
    // toNumLim(val) {
    //   return String(val).indexOf(".") > -1 ? Number(val).toFixed(3) : val;
    // },
    auditSubmit() {
      if (this.formQuery.approvalResult == "1") {
        this.formQuery.sfba = "5";
      } else {
        this.formQuery.sfba = "3";
      }

      updateEmptyPotApplyList({
        ...this.formQuery,
      }).then((res) => {
        if (res.infoCode == 200) {
          this.$message({
            message: "提交成功",
            type: "success",
          });
          this.$router.go(-1);
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    cancel() {
      this.$router.go(-1);
    },
    //上传图片相关
    handleYGSuccess(res, file) {
      if (res.infoCode === 200) {
        this.formQuery.ygpmt = res.data.id;
      }
    },
    handleYGRemove(file, fileList) {
      this.formQuery.ygpmt = "";
    },
    handleYGPictureCardPreview(file) {
      this.ygpmt = file.url;
      this.dialogYGVisible = true;
    },
    handleExceed(file, fileList) {
      this.$message.warning(`当前限制选择 1 个图片`);
    },
    handlerError() {
      this.$message.error("图片上传失败,请重试");
    },
    handleKQSuccess(res, file) {
      if (res.infoCode === 200) {
        this.formQuery.ygkqt = res.data.id;
      }
    },
    handleKQRemove(file, fileList) {
      this.formQuery.ygkqt = "";
    },
    handleKQPictureCardPreview(file) {
      this.ygkqt = file.url;
      this.dialogKQVisible = true;
    },
  },
  created() {
    if (this.$route.query.state == "detail") {
      if (this.$route.query.sfba == 3 || this.$route.query.sfba == 5) {
        this.isChecked = true;
      }
      this.showLock = true;
      this.getInfo(this.$route.query.ygbh);
    }
    // else if (this.$route.query.state == "edit") {
    //   this.showLock = false;
    //   this.getInfo(this.$route.query.ygbh);
    // } else if (this.$route.query.state == "audit") {
    //   this.isChecked = true;
    //   this.showLock = true;
    //   this.getInfo(this.$route.query.ygbh);
    // } else {
    //   this.showLock = false;
    // }
  },
};
</script>

<style scoped lang="scss">
.content-main {
  margin: 10px 10px 0;
  padding-top: 20px;
}

.span-inline-block {
  float: left;
  margin-right: 5px;
  width: 80px;
  text-align: right;
  line-height: 30px;
}

.inputWidth {
  width: 335px;
}

.h3 {
  width: 98%;
  text-align: center;
  font-size: 1em;
  line-height: 2.5;
  color: #fff;
  background-color: #49a2fa;
  margin-bottom: 20px;
}

.el-form {
  overflow: hidden;
  overflow-y: auto;
}

.el-row {
  .el-tooltip {
    float: left;
    padding: 0 12px 0 0;
    width: 118px;
    white-space: nowrap;
    text-align: right;
    vertical-align: middle;
    color: #525357;
    font-size: 14px;
    line-height: 40px;
    box-sizing: border-box;
  }
}

.warehouseDoorVOList .el-form-item {
  margin: 0 !important;
}
</style>
