import routerUserMenu from '@/mixins/router-user-menu'
import breadcrumbComponent from '@/components/breadcrumb.vue'
/**
 * 页面组件设置
 *
 *  version = "1.0.0";
 *  author = "徐飞";
 *  email = "xu.fei@data-u.com";
 * */
export default {
  name: 'detail',
  components:{ breadcrumbComponent },
  mixins: [routerUserMenu],
  data () {
    return {
      // 组件id（修改和查看）
      id: false,
      // 新建对象
      interfaceInfo: {
        checkCode: '',
        projectId: '',
        projectName: '',
      },
      // 页面打开类型，0：新建，1：查看，2：修改
      dialogState: 0,
    }
  },
  computed: {
    // 组件设置是否只读
    readonly: {
      // getter
      get: function() {
        return (this.dialogState === 1)
      },
      // setter
      set: function (newValue) {
      }
    },
  },
  mounted () {
    // 初始化
    this.init()
  },
  methods: {
    //  如果有id，则获取id对应的部门信息赋值给formInfo
    init () {
      // 获取url参数
      let params = this.$route.params
      if (params.hasOwnProperty('id')) {
        // 如果有id，则加载组件对象
        this.id = params.id
        this.getDetail()
      }
      // 根据页面参数设置当前页面类型
      this.dialogState = params.id ? 2 : 0
    },
    // 获取回显详情信息
    getDetail () {
      let params = { id: this.id }
      let _this = this
      this.$api.interfaceDb.getInterfaceProjectById(params)
        .then(res => {
          if (res.code == 1) {
            if (res.data) {
              _this.interfaceInfo = res.data
              _this.$nextTick(()=> {
                _this.$refs.interfaceInfo.clearValidate()
              })
            }
          }
        })
    },
    // 提交保存
    add () {
      let _this = this
      // 判断组件设置是否通过验证
      this.$refs.interfaceInfo.validate((valid) => {
        if (valid) {
          // 提交后台并在成功后回到上一页
          let params = _this.interfaceInfo
          _this.$api.interfaceDb.addInterfaceProject(params)
            .then(res => {
              if (res.code == 1) {
                _this.$message.success(res.msg)
                _this.goBack()
              }
            })
        } else {
          return false
        }
      })
    },

    // 更新
    update () {
      let _this = this
      // 判断组件设置是否通过验证
      this.$refs.interfaceInfo.validate((valid) => {
        if (valid) {
          // 提交后台并在成功后回到上一页
          let params = _this.interfaceInfo
          params.projectId = _this.id
          params.id = _this.id
          _this.$api.interfaceDb.updateInterfaceProjectById(params)
            .then(res => {
              if (res.code == 1) {
                _this.$message.success(res.msg)
                _this.goBack()
              }
            })
        } else {
          return false
        }
      })
    },
    goBack () {
      this.go('InterfaceClassify')
    }
  }
}
