<template>
  <el-dialog
    title="主从策略配置"
    custom-class="ems-dialog"
    :visible.sync="dialogFormVisible"
    width="420px"
    destroy-on-close
    lock-scroll
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="cancel"
  >
    <el-form ref="form" :model="form" :rules='rule' label-width="100px" v-loading="loading > 0">
      <el-form-item label="主策略" prop="mainStrategyId">
        <el-select v-model="form.mainStrategyId" placeholder="请选择">
          <el-option :label="item.strategyName" :value="item.id" v-for="(item,index) in mainStrategyList" :key="index+'mainStrategyList'"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="辅助策略" prop="auxStrategyId">
        <el-select v-model="form.auxStrategyId" placeholder="请选择" clearable>
          <el-option :label="item.strategyName" :value="item.id" v-for="(item,index) in auxStrategyList" :key="index+'auxStrategyList'"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" :loading="loading > 0" @click="sure">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getMainStrategyList, getAuxStrategyList, configStrategy} from '@/api/ems/dzjk'
export default {
  inject:['$home'],
  data(){
    return {
      info:{},
      loading:0,
      dialogFormVisible:false,
      mainStrategyList:[],
      auxStrategyList:[],
      form:{
        mainStrategyId:'',
        auxStrategyId:''
      },
      rule:{
        mainStrategyId:[
          { required: true, message: '请选择主策略', trigger: ['blur','change'] },
        ]
      }
    }
  },
  methods:{
    showSettingDialog(item){
      console.log('打开弹窗',item)
      this.dialogFormVisible = true
      this.info = item || {}
      const {auxStrategyId = '',mainStrategyId = ''} = item
      this.form.auxStrategyId=auxStrategyId
      this.form.mainStrategyId=mainStrategyId
      this.init()
    },
    init(){
      this.loading+=2
      this.mainStrategyList=[]
      this.auxStrategyList=[]
      getMainStrategyList().then(response => {
        this.mainStrategyList=response?.data || []
      }).finally(()=>{this.loading-=1})
      getAuxStrategyList().then(response => {
        this.auxStrategyList=response?.data || []
      }).finally(()=>{this.loading-=1})
    },
    cancel(){
      this.$refs.form.resetFields()
      this.form={
        mainStrategyId:'',
        auxStrategyId:''
      }
      this.dialogFormVisible = false;
    },
    sure(){
      this.$refs['form'].validate(valid => {
        if (!valid) return
        console.log('this.$home',this.$home)
        this.loading +=1
        const {mainStrategyId='', auxStrategyId=''} = this.form
        configStrategy({mainStrategyId,auxiliaryStrategyId:auxStrategyId,siteId:this.$home.siteId,id:this.info?.id || ''}).then(response => {
          if(response?.code === 200){
            this.$home.init()
            this.cancel()
          }
        }).finally(()=>{
          this.loading -=1
        })
      })

    }
  }
}
</script>

<style scoped lang="scss">

</style>
