<template>
  <div class="examination">
    <el-dialog
      @close="closeDialog"
      :visible.sync="dialogTableVisible"
      destroy-on-close
    >
      <div slot="title" class="titleBox">
        <i
          :class="tableType === 0 ? 'el-icon-document' : 'el-icon-edit-outline'"
          style="font-size: 20px"
        ></i>
        <span style="margin-left: 10px; font-size: 18px">{{
          tableTypeDsc
        }}</span>
      </div>
      <el-form
        ref="baseData"
        label-width="100px"
        :rules="ruleValidate"
        :model="baseData"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item prop="matName" label="物料名称">
              <el-input
                v-model="baseData.matName"
                readonly
                disabled
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="printType" label="字段类型">
              <el-radio-group v-model="baseData.printType" disabled>
                <el-radio :label="0">系统带出</el-radio>
                <el-radio :label="1">自定义</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="fieldKeyName" label="字段名称">
              <el-input
                v-model="baseData.fieldKeyName"
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="fieldValue" label="字段默认值">
              <el-input
                v-model="baseData.fieldValue"
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="isCheck" label="是否勾选">
              <el-radio-group v-model="baseData.isCheck">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          @click="tableType === 0 ? addData() : modifyData()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="cancel"
          >取消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getCookie } from "@/utils/util.js";
import { cookieUserId, cookieUserName } from "@/config/config.js";
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ["showFlag2", "Params", "Params2"],
  data() {
    return {
      dialogTableVisible: false,
      tableTypeDsc: "",
      tableType: 0,
      tableData: [],
      baseData: {
        matNo: "",
        matName: "",
        printType: 0,
        fieldKey: "",
        fieldKeyName: "",
        isCheck: 1,
        matClassName: "",
        matClassNo: "",
        fieldValue: "",
      },
      ruleValidate: {
        matName: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
        fieldKeyName: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
        printType: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
        isCheck: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    cancel() {
      this.tableData = [];
      this.baseData = {
        matNo: "",
        matName: "",
        printType: 0,
        fieldKey: "",
        fieldKeyName: "",
        isCheck: 1,
        matClassName: "",
        matClassNo: "",
        fieldValue: "",
      };
      this.dialogTableVisible = false;
    },
    getCode(code, arr, codeKey, nameKey) {
      if (!code) {
        return "";
      }
      let targetRow = arr.find((item) => {
        return item[codeKey] === code;
      });
      return targetRow ? targetRow[nameKey] : "";
    },
    addData() {
      this.tableData = [];
      this.$refs["baseData"].validate((valid) => {
        if (valid) {
          this.axios
            .request({
              url: "pass/testManagement/v1/limssampleprinttemplates/",
              method: "post",
              data: {},
              params: {
                ...this.baseData,
                createUsername: userName,
                createTime: this.getCurrentTime(),
                fieldKey: Date.now(),
              },
            })
            .then((res) => {
              if (res.succeed) {
                this.dialogTableVisible = false;
                this.$message.success("添加成功");
                this.$emit("refresh");
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        }
      });
    },
    modifyData() {
      this.tableData = [];
      this.$refs["baseData"].validate((valid) => {
        if (valid) {
          this.axios
            .put(
              `pass/testManagement/v1/limssampleprinttemplates/${this.baseData.id}`,
              {
                ...this.baseData,
                updateUsername: userName,
                updateTime: this.getCurrentTime(),
              },
              { individualType: "json" }
            )
            .then((res) => {
              if (res.succeed) {
                this.dialogTableVisible = false;
                this.$message.success("修改成功");
                this.$emit("refresh");
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        }
      });
    },
    getCurrentTime() {
      // 创建一个Date对象
      var now = new Date();

      // 获取年、月、日、时、分、秒
      var year = now.getFullYear();
      var month = now.getMonth() + 1; // getMonth() 返回的月份是从0开始的，所以需要加1
      var day = now.getDate();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();

      // 格式化月份、日期、小时、分钟、秒，确保它们是两位数
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      hours = hours < 10 ? "0" + hours : hours;
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;

      // 组合成字符串
      var formattedDate =
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds;

      return formattedDate;
    },
    closeDialog() {
      this.tableData = [];
      this.baseData = {
        matNo: "",
        matName: "",
        printType: 0,
        fieldKey: "",
        fieldKeyName: "",
        isCheck: 1,
        matClassName: "",
        matClassNo: "",
        fieldValue: "",
      };
    },
  },
  watch: {
    showFlag2(arg) {
      this.dialogTableVisible = true;
      if (this.Params2 !== null) {
        this.tableType = 1;
        this.tableTypeDsc = "修改二维码字段信息";
        this.baseData = Object.assign({}, this.Params2[0]);
      } else {
        this.tableType = 0;
        this.tableTypeDsc = "新增二维码字段信息";
        this.baseData.matNo = this.Params[0].matNo;
        this.baseData.matName = this.Params[0].matName;
        this.baseData.matClassName = this.Params[0].matClassName;
        this.baseData.matClassNo = this.Params[0].matClassNo;
        this.baseData.printType = 1;
      }
    },
  },
};
</script>
<style lang="less">
.customWidth_syst {
  width: 600px !important;
}
</style>
