<template>
  <confirm-dialog
    ref="configDialog"
    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="tenantStratConfig.enabled"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </div>
      <span class="c-grid-form__label">图片保存</span>
      <div
        class="l-flex--row c-grid-form__option c-grid-form__info"
        data-info="开启将保存至多1000张合格图片"
      >
        <el-switch
          v-model="tenantStratConfig.storePic"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </div>
      <span class="c-grid-form__label">库存阈值</span>
      <div
        class="l-flex--row c-grid-form__option c-grid-form__info"
        data-info="库存低于阈值后将关闭抽帧检测"
      >
        <el-input-number
          v-model="tenantStratConfig.creditThreshold"
          :min="0"
          :max="999999"
          step-strictly
        />
      </div>
      <span class="c-grid-form__label u-bold">疑似处理</span>
      <div class="l-flex--row c-grid-form__option">
        <schema-select
          v-model="tenantStratConfig.suspectedStrategy"
          :schema="suspectedStrategySelectSchema"
        />
      </div>
      <span class="c-grid-form__label u-bold">不合规处理</span>
      <div class="l-flex--row c-grid-form__option">
        <schema-select
          v-model="tenantStratConfig.nonComplianceStrategy"
          :schema="nonComplianceStrategySelectSchema"
        />
      </div>
    </div>
  </confirm-dialog>
</template>

<script>
import {
  getTenantStratConfig,
  addTenantStratConfig,
  updateTenantStratConfig
} from '../api'

export default {
  name: 'StratConfigDialog',
  data () {
    return {
      suspectedStrategySelectSchema: { options: [
        { value: 0, label: '预警' },
        { value: 1, label: '关机' },
        { value: 2, label: '预警+关机' }
      ] },
      nonComplianceStrategySelectSchema: { options: [
        { value: 0, label: '预警' },
        { value: 1, label: '关机' },
        { value: 2, label: '预警+关机' }
      ] },
      tenantStratConfig: {}
    }
  },
  methods: {
    show (tenant) {
      getTenantStratConfig(tenant).then(({ data }) => {
        this.tenantStratConfig = data || {
          tenant,
          enabled: false,
          storePic: true,
          suspectedStrategy: 0,
          nonComplianceStrategy: 2,
          creditThreshold: 0
        }
        this.$refs.configDialog.show()
      })
    },
    onSave (done) {
      (this.tenantStratConfig.id ? updateTenantStratConfig : addTenantStratConfig)(this.tenantStratConfig).then(done)
    }
  }
}
</script>
