<template>
  <p-modal
    :title="title"
    :visible="isvisible"
    @ok="handleOk"
    :destroyOnClose="true"
    width="60%"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <p-tabs default-active-key="1" @change="callback">
      <p-tab-pane key="1" :forceRender="false" tab="分配货主">
        <tabsAuth type="1" @selectChange="selectChange" :setRecord="setRecord" />
      </p-tab-pane>
      <p-tab-pane key="2" :forceRender="false" tab="分配机构" force-render>
        <tabsAuth1 type="2" @selectChange="selectChange" :setRecord="setRecord" />
      </p-tab-pane>
      <p-tab-pane key="3" :forceRender="false" tab="贸易伙伴">
        <tabsAuth2 type="3" @selectChange="selectChange" :setRecord="setRecord" />
      </p-tab-pane>
    </p-tabs>
  </p-modal>
</template>
<script>
import tabsAuth from './tabsAuth'
import tabsAuth1 from './tabsAuth1'
import tabsAuth2 from './tabsAuth2'
import { saveDataAuthCompany } from './../api/addAuth.js'
export default {
  data() {
    return {
      isvisible: false,
      confirmLoading: false,
      checkData: ''
    }
  },
  components: {
    tabsAuth,
    tabsAuth1,
    tabsAuth2
  },
  props: {
    visibleDetial: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    setRecord: {
      type: Object,
      default: {}
    }
  },
  watch: {
    visibleDetial(newVal) {
      this.isvisible = newVal
    }
  },
  methods: {
    callback(key) {
      console.log(key)
    },
    handleCancel() {
      // 取消
      this.isvisible = false
      this.$emit('requestAuth', false)
    },
    selectChange(data) {
      // 勾选框
      // this.
      this.checkData = data
    },
    handleOk() {
      // 确认
      this.confirmLoading = true
      if (this.checkData) {
        this.saveData()
      } else {
        this.confirmLoading = false
        this.$message.warn('请选择数据！')
      }
    },
    saveData() {
      // 保存数据
      let params = this.checkData
      params.type = params.type == '1' ? '0' : '1'
      saveDataAuthCompany(params).then(res => {
        this.confirmLoading = false
        if (res.success) {
          console.log(res)
          this.$emit('successAuth', false)
        } else {
          this.$message.warn(res.message)
        }
      })
    }
  }
}
</script>
