<template>
    <div class="eContainer">
      <c-page :title="title">
        <el-form
          ref="modelForm"
          label-width="120px"
          size="small"
          label-position="right"
          :model="model"
          :rules="rules"
          :validate-on-rule-change="false"
          :disabled="isDisabled"
        >
          <c-tabs v-model="tabVal" ref="elment" type="card">
            <el-tab-pane label="交易备忘录查询" name="dia">
              <c-content>
                <m-dia-info :model="model" :type="type" />
              </c-content>
            </el-tab-pane>
          </c-tabs>
        </el-form>
        <div style="text-align: center">
          <c-button
            type="primary"
            style="margin-right: 10px"
            @click="commitAdd"
            v-if="type === 'add'"
            >提 交</c-button
          >
          <c-button
            type="primary"
            style="margin-right: 10px"
            @click="commitEdit"
            v-if="type === 'edit'"
            >提 交</c-button
          >
          <c-button
            type="primary"
            style="margin-right: 10px"
            @click="commitDelete"
            v-if="type === 'delete'"
            >提 交</c-button
          >
          <c-button type="primary" @click="goBack">返 回</c-button>
        </div>
      </c-page>
    </div>
  </template>
  
  <script>
  import Utils from "~/utils";
  import Dia, { Pattern } from "./Dia.js";
  
  import DiaInfo from "./DiaInfo.vue";
  
  import { queryById, add, edit, deleteById, queryDetailById } from "~/service/manage/dia.js";
  
  export default {
    name: "StaticsDbidia",
    components: {
      "m-dia-info": DiaInfo,
    },
    provide() {
      return {
        root: this,
      };
    },
    props: {
          type: {
              type: String,
              default: "info"
          },
          title: {
              type: String,
              default: "dbidia"
          }
      },
    data() {
      return {
        model: new Dia().data,
        tabVal: "dia",
        rules: Pattern,
      };
    },
    computed: {
      isDisabled() {
        return this.type === "info" || this.type === "delete";
      },
    },
    created() {
      if (this.type !== "add") {
        const data = {"inr":this.$route.params.inr};
        queryDetailById(data).then((res) => {
          if (res.respCode == SUCCESS) {  
            this.model = res.data;
          } else {
            this.$notify.error("数据不存在")
          }
        });
      }
    },
    methods: {
      commitAdd() {
        this.$refs.modelForm.validate((validated) => {
          if (validated) {
            add(this.model)
              .then((res) => {
                if (res.messsage !== null && Object.keys(res.messsage).length > 0) {
                  this.$notify.error(res.messsage)
                } else {
                  this.$notify.success("保存成功!")
                  this.goBack(true)
                }
              })
              .catch((err) => {
                this.$notify.error("保存失败!");
              });
          } else {
            Utils.formValidateTips(this.$refs.modelForm.fields)
          }
        });
      },
      commitEdit() {
        this.$refs.modelForm.validate((validated) => {
          if (validated) {
            edit(this.model)
              .then((res) => {
                this.$notify.success("保存成功!");
                this.goBack(true)
              })
              .catch((err) => {
                this.$notify.error("保存失败!");
              });
          } else {
            Utils.formValidateTips(this.$refs.modelForm.fields)
          }
        });
      },
      commitDelete() {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let data = {"inr":this.model.inr}
            deleteById(data)
              .then((res) => {
                this.$notify.success("删除成功!");
                this.goBack(true)
              })
              .catch((err) => {
                this.$notify.error("删除失败!");
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      /**
       * update 是否更新infdia的查询列表
       */
      goBack(update) {
        this.$store.dispatch("TagsView/delView", this.$route);
        this.$router.push({ name: "StaticsDbldia", params: { update } });
      },
  
    },
  };
  </script>
  
  <style>
  </style>