<template>
  <confirm-dialog
    ref="configDialog"
    :title="title"
    @confirm="onSave"
  >
    <div class="c-grid-form mini u-align-self--center">
      <span class="c-grid-form__label u-bold">开启：</span>
      <div class="l-flex--row c-grid-form__option">
        <el-switch
          v-model="tenantServiceConfig.enabled"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </div>
    </div>
  </confirm-dialog>
</template>

<script>
import {
  getTenantServiceConfig,
  addTenantServiceConfig,
  updateTenantServiceConfig
} from '../api'

export default {
  name: 'ServiceConfigDialog',
  data () {
    return {
      title: '',
      tenantServiceConfig: {}
    }
  },
  methods: {
    show (tenant, type) {
      getTenantServiceConfig(tenant, type).then(({ data }) => {
        this.title = type === 1 ? 'AI抽帧检测服务配置' : '媒资AI审核服务配置'
        this.$has = !!data
        this.tenantServiceConfig = {
          tenant,
          type,
          enabled: data ? data.enabled : false
        }
        if (type === 1) {
          this.tenantServiceConfig.collectDevice = 1
        }
        this.$refs.configDialog.show()
      })
    },
    onSave (done) {
      (this.$has ? updateTenantServiceConfig : addTenantServiceConfig)(this.tenantServiceConfig).then(done)
    }
  }
}
</script>
