<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="700px"
      :before-close="handleClose"
    >
      <el-form
        :inline="true"
        :model="form"
        :rules="rules"
        class="form"
        ref="elform"
        label-width="100px"
      >
        <el-form-item label="终端编号:" prop="id">
          <el-input
            clearable
            :disabled="title == `修改`"
            v-model="form.id"
            placeholder="请输入终端编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="终端名称:" prop="name">
          <el-input
            clearable
            v-model="form.name"
            placeholder="请输入终端名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="送检单位:" prop="inspectionUnit">
          <el-input
            clearable
            v-model="form.inspectionUnit"
            placeholder="请输入送检单位"
          ></el-input>
        </el-form-item>
        <el-form-item label="检测时间:" prop="inspectionTime">
          <el-date-picker
            v-model="form.inspectionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安装位置:" prop="installPlace">
          <el-input
            clearable
            v-model="form.installPlace"
            placeholder="请输入安装位置"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产厂家:" prop="manufacture">
          <el-select
            v-model="form.manufacture"
            placeholder="请选择生产厂家"
            clearable
            class="select"
          >
            <el-option
              v-for="item in manufactorList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form label-width="100px">
        <el-form-item label="原始数据报告:">
          <File
            ref="File"
            :originalName="form.originalName"
            :originalReport="form.originalReport"
            :id="form.id"
          />
          <!-- <el-upload
            ref="upload"
            class="upload-files"
            action=""
            :limit="1"
            :headers="headers"
            name="file"
            multiple
            :auto-upload="false"
            :file-list="fileList"
            :on-change="handleChange"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
          </el-upload> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleClose"
          >取 消</el-button
        >
        <el-button type="primary" size="small" @click="submitForm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertTerminal,
  updateTerminal,
} from "@/api/Process-supervision/Terminaldetection/index.js";
import { dicData } from "@/assets/commjs/dictypeData";
import File from "./File.vue";
export default {
  components: { File },
  props: {
    title: {
      type: String,
    },
    list: {
      type: [Array, Object],
    },
  },
  data() {
    return {
      headers: {
        Authorization: window.sessionStorage.getItem("cntoken"),
      },
      fileList: [],
      dialogVisible: true,
      form: {
        id: "",
        inspectionUnit: "",
        inspectionTime: "",
        installPlace: "",
        manufacture: "",
        name: "",
        originalName: "",
        orgNo: JSON.parse(window.sessionStorage.getItem("Info")).deptId,
        orgName: JSON.parse(window.sessionStorage.getItem("Info")).deptName,
      },
      rules: {
        id: [{ required: true, message: "请输入终端编号", trigger: "blur" }],
        name: [{ required: true, message: "请输入终端名称", trigger: "blur" }],
        installPlace: [
          { required: true, message: "请输入安装位置", trigger: "blur" },
        ],
        inspectionUnit: [
          { required: true, message: "请输入送检单位", trigger: "blur" },
        ],
        inspectionTime: [
          { required: true, message: "请选择时间", trigger: "change" },
        ],
        manufacture: [
          { required: true, message: "请选择生产厂家", trigger: "change" },
        ],
      },
      manufactorList: [],
    };
  },
  created() {},
  mounted() {
    //生产厂家
    this.manufactorList = dicData("Dev_Manufacturers", []);
    this.title == "修改"
      ? (this.form = JSON.parse(JSON.stringify(this.list)))
      : "";
  },
  methods: {
    submitForm() {
      this.form.originalReport = this.$refs.File.fileList.data;

      this.$refs.elform.validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            insertTerminal(this.form).then((res) => {
              if (res.code == "A0000") {
                this.$message({
                  message: res.message,
                  type: "success",
                });
                this.$emit("onSubmit");
                this.$emit("handleClose");
              } else {
                this.$message({
                  message: res.message,
                  type: "warning",
                });
              }
            });
          } else if (this.title == "修改") {
            updateTerminal(this.form).then((res) => {
              if (res.code == "A0000") {
                this.$message({
                  message: res.message,
                  type: "success",
                });
                this.$emit("onSubmit");
                this.$emit("handleClose");
              } else {
                this.$message({
                  message: res.message,
                  type: "warning",
                });
              }
            });
          }
        }
      });
    },
    handleChange(file, fileList) {
      // console.log(file, fileList);
      this.fileList = fileList;
    },
    handleClose() {
      this.$emit("handleClose");
    },
  },

  computed: {},

  watch: {},
};
</script>
<style lang="less" scoped>
@import url("../../../../styles/comStyle.less");
::v-deep .form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .el-form-item {
    display: flex;
    width: 48%;
    margin-bottom: 15px !important;
    .el-form-item__content {
      flex: 1 !important;
      .el-select {
        width: 100%;
      }
      .el-input__inner {
        width: 100% !important;
      }
      .el-date-editor {
        width: 100%;
      }
    }
  }
}
</style>
