<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="usr">
            <c-content>
              <m-usr-info :model="model" />
            </c-content>
          </el-tab-pane>
          <el-tab-pane label="柜员权限信息" name="ubr">
            <c-content>
              <m-ubr-list :model="model" />
            </c-content>
          </el-tab-pane>
          <el-tab-pane label="柜员组" name="ucl">
            <c-content>
              <m-ucl-list :model="model" />
            </c-content>
          </el-tab-pane>
<!--          <el-tab-pane label="联系人信息" name="ptc">-->
<!--            <c-content>-->
<!--              <m-ptc-info :model="model" />-->
<!--            </c-content>-->
<!--          </el-tab-pane>-->
<!--          <el-tab-pane label="汇率/费率信息" name="ptyrat,fec,fee">-->
<!--            <c-content>-->
<!--              <m-rat-info :model="model" />-->
<!--            </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 Usr, { Pattern } from "./Usr.js";
import UsrInfo from "./UsrInfo";
import UBrInfo from "./UbrList";
import UclInfo from "./UclList";

import { add, edit, deleteById, queryDetailById } from "~/service/test/usr";

export default {
  name: "StaticsDbiusr",
  components: {
    "m-usr-info": UsrInfo,
    "m-ubr-list": UBrInfo,
    "m-ucl-list": UclInfo,
  },
  provide() {
    return {
      root: this,
    };
  },
  props: {
    type: {
      type: String,
      default: "info"
    },
    title: {
      type: String,
      default: "dbiusr"
    }
  },
  data() {
    return {
      model: new Usr().data,
      tabVal: "usr",
      rules: Pattern,
    };
  },
  computed: {
    isDisabled() {
      return this.type === "info" || this.type === "delete";
    },
  },
  created() {
    if (this.type !== "add") {
      const inr = this.$route.params.inr;
      // this.model.inr = this.$route.params.inr;
      console.log(inr);
      queryDetailById(inr).then((res) => {
        if (res.inr) {
          this.model = res;
        } else {
          this.$message.error("柜员不存在")
        }
      });
    }
  },
  methods: {
    commitAdd() {
      this.$refs.modelForm.validate((validated) => {
        if (validated) {
          add(this.model)
              .then((res) => {
                this.$message.success("保存成功!");
                this.goBack(true)
              })
              .catch((err) => {
                this.$message.error("保存失败!");
              });
        } else {
          Utils.formValidateTips(this.$refs.modelForm.fields)
        }
      });
    },
    commitEdit() {
      this.$refs.modelForm.validate((validated) => {
        if (validated) {
          edit(this.model)
              .then((res) => {
                this.$message.success("保存成功!");
                this.goBack()
              })
              .catch((err) => {
                this.$message.error("保存失败!");
              });
        } else {
          Utils.formValidateTips(this.$refs.modelForm.fields)
        }
      });
    },
    commitDelete() {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            deleteById(this.model.inr)
                .then((res) => {
                  this.$message.success("删除成功!");
                  this.goBack(true)
                })
                .catch((err) => {
                  this.$message.error("删除失败!");
                });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
    },
    /**
     * update 是否更新infusr的查询列表
     */
    goBack(update) {
      this.$store.dispatch("TagsView/delView", this.$route);
      this.$router.push({ name: "StaticsInfusr", params: { update } });
    },

  },
};
</script>

<style>
</style>