<template>
  <el-dialog title="按条件批量配置" :visible.sync="dialog.config" @close="handleClose" width="900px">
    <el-form ref="dataForm" :model="form" label-width="90px">
      <el-form-item label="方案：">
        <el-radio-group v-model="form.schemeId">
          <el-radio label="schemeZero">全新设定</el-radio>
          <el-radio label="schemeOne">预置方案</el-radio>
          <el-radio label="schemeTwo">万达专用方案</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="form.schemeId == 'schemeZero'" label="优先级：">
        <el-checkbox-group v-model="form.priorityList">
          <template v-for="item in priorityList">
            <el-checkbox :key="item.priority" :label="item.priority" name="priority" v-if="item.name">
              {{ item.name }}
            </el-checkbox>
          </template>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item v-show="form.schemeId == 'schemeZero'" label="一级标签：">
        <el-checkbox-group v-model="form.firstNameList">
          <el-checkbox style="width:99px;" v-for="(item, index) in firstNameList" :key="index" :label="item.firstName"
                       name="firstTag"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item v-show="form.schemeId == 'schemeZero'" label="二级标签：">
        <span style="color:#bbb;font-size:12px;">二级标签暂未提供，如确实需要请联系产品添加。</span>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="dialog.config = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确 认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {mapGetters, mapActions} from "vuex"
import {setInfoPoint, getDataDictionary} from "@/api/dict";

export default {
  props: {
    dialog: {
      type: Object,
      default: function () {
        return {
          config: false
        };
      }
    }
  },
  data() {
    return {
      form: {
        schemeId: 'schemeZero',
        priorityList: [],
        firstNameList: []
      },
      firstNameList: [],
      priorityList: [],
      loading: false
    };
  },
  created() {
    this.getInfos()
  },
  computed: {
    ...mapGetters("layout", ["projectId", "secret", "userId"])
  },
  methods: {
    getInfos() {
      let params1 = {
        distinct: true,
        filters: "firstName != ''",
        orders: "firstName desc",
        pageNumber: 1,
        pageSize: 1000,
        projection: ["firstName"]
      }, params2 = {
        distinct: true,
        filters: "priority != '';priority != 'null'",
        orders: "priority desc",
        pageNumber: 1,
        pageSize: 1000,
        projection: ["priority"]
      };
      let promise1 = new Promise((resolve, reject) => {
        getDataDictionary(params1, res => {
          resolve(res)
        })
      })
      let promise2 = new Promise((resolve, reject) => {
        getDataDictionary(params2, res => {
          resolve(res)
        })
      })
      Promise.all([promise1, promise2]).then(values => {
        this.firstNameList = values[0].content
        this.priorityList = values[1].content.map(item => {
          switch (item.priority) {
            case "S":
              item.name = `(${item.priority})关键信息`;
              break;
            case "M":
              item.name = `(${item.priority})次要信息`
              break
            case "R":
              item.name = `(${item.priority})次要信息`;
              break
            default:
              break
          }
          return item
        })
      })
    },
    //确定
    onSubmit() {
      let params = {}
      this.loading = true
      if (this.form.schemeId == 'schemeZero') {
        params = this.form
      } else {
        params = {
          schemeId: this.form.schemeId
        }
      }
      setInfoPoint(params, res => {
        this.form.schemeId = 'schemeZero'
        this.form.firstNameList = []
        this.form.priorityList = []
        this.dialog.config = false
        this.loading = false
        this.$emit('configInfo')
      })
    },
    //弹窗关闭回调
    handleClose() {
      this.form.schemeId = 'schemeZero'
      this.form.firstNameList = []
      this.form.priorityList = []
      this.loading = false
    }
  }
};
</script>
<style lang="scss">
.el-dialog__body {
  max-height: 420px;
  overflow-y: auto;
}
</style>
