<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="c-grid-form__option">
        <el-input-number
          v-model="tenantStratConfig.offset"
          :min="10"
          :max="3600"
          step-strictly
        />
        秒（10~3600）
      </div>
      <span class="c-grid-form__label required">开始时间：</span>
      <el-time-picker
        v-model="tenantStratConfig.startTime"
        value-format="HH:mm:ss"
        :clearable="false"
      />
      <span class="c-grid-form__label required">结束时间：</span>
      <el-time-picker
        v-model="tenantStratConfig.endTime"
        value-format="HH:mm:ss"
        :clearable="false"
      />
      <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>
    </div>
  </confirm-dialog>
</template>

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

export default {
  name: 'StratConfigDialog',
  data () {
    return {
      tenantStratConfig: {}
    }
  },
  methods: {
    show (tenant) {
      getTenantStratConfig(tenant).then(({ data }) => {
        this.tenantStratConfig = data || {
          tenant,
          enabled: false,
          offset: 10,
          startTime: '08:00:00',
          endTime: '23:59:59',
          storePic: true,
          suspectedStrategy: 0,
          nonComplianceStrategy: 2,
          creditThreshold: 0
        }
        this.$refs.configDialog.show()
      })
    },
    onSave (done) {
      const { startTime, endTime } = this.tenantStratConfig
      if (!startTime) {
        this.$message({
          type: 'warning',
          message: '请选择开始时间'
        })
        return
      }
      if (!endTime) {
        this.$message({
          type: 'warning',
          message: '请选择结束时间'
        })
        return
      }
      (this.tenantStratConfig.id ? updateTenantStratConfig : addTenantStratConfig)(this.tenantStratConfig).then(done)
    }
  }
}
</script>
