<template>
  <el-dialog title="设置规则" :visible.sync="visiable" width="900px" :before-close="close">
    <div class="ruleContainer">
      <div class="left">
        <el-tabs tab-position="left" @tab-click="onTabsClick" v-model="activeName">
          <el-tab-pane :label="item.chainNodeName" v-for="item in conditionTypeList" :key="item.esScreeningWidgetChainId" :name="item.esScreeningWidgetChainId">
            <div v-if="templateCode == 'tag001'" class="leftContent">
              <el-checkbox-group v-model="item.selectValue" @change="val => handleChange(val, item.esScreeningWidgetChainId)" class="checkBoxContainer">
                <el-checkbox v-for="row in item.selectList" :key="row.key" :label="row.key">{{ row.value }}</el-checkbox>
              </el-checkbox-group>
            </div>
            <div v-if="templateCode == 'com026'" class="leftContent">
              <div class="line" v-for="(value, index) in item.selectList" :key="index">
                <h3>{{ value.title }}</h3>
                <el-checkbox-group v-model="item.selectValue" @change="val => handleChange(val, item.esScreeningWidgetChainId)" class="checkBoxContainer">
                  <el-checkbox v-for="row in value.data" :key="row.key" :label="row.key">{{ row.value }}</el-checkbox>
                </el-checkbox-group>
              </div>
            </div>
            <div v-if="templateCode == 'com020'" class="leftContent">
              <vue-gic-store-new :isAdd="isAdd" :creatorId="creatorId" :scenesVal="scenes" scenes="auth" :uuid="item.value" ref="storeNew" @store-change="storeChange"></vue-gic-store-new>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="right">
        <div class="right-top">
          已选条件
        </div>
        <ul class="right-content">
          <li class="contact-li" v-for="(item, index) in selectData" :key="index">
            <div class="li-cell cursor-pointer">{{ item.chainNodeName }} <i v-if="!readonly" class="el-icon-close" @click="deleteRow(item)"></i></div>
            <div>
              <span v-for="row in item.selectValue" :key="row"> {{ row }}</span>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import qs from 'qs';
import transform from './createParams.js';
export default {
  data() {
    return {
      activeName: '',
      conditionTypeList: [], // 第一层级
      checkboxList: [],
      arr: [],
      uuid: '',
      templateCode: '', //当前控件类型
      echoFlag: false
    };
  },
  props: {
    visiable: {
      type: Boolean,
      default: false
    },
    sceneCode: {
      type: String,
      default: 'member02'
    },
    requestProject: {
      type: String,
      default: 'gic-web'
    },
    memberCrowdWidgetId: {
      type: [String, Number],
      default: ''
    }
  },
  mounted() {
    this.getDataList();
    this.getEchoData();
  },
  watch: {
    memberCrowdWidgetId: {
      immediate: true,
      handler(newval) {
        if (newval && this.echoFlag) {
          /**
           * 回显数据请求 echoFlag 为真且memberCrowdWidgetId不为空 然后使用memberCrowdWidgetId来查回显的数据
           **/
          this.getEchoData();
        }
      }
    }
  },
  computed: {
    selectData() {
      const data = [];
      this.conditionTypeList.forEach(item => {
        data.push({ chainNodeName: item.chainNodeName, selectValue: item.selectValue });
      });
      return data;
    }
  },
  methods: {
    getEchoData() {
      // "ff8080817fab027b017fce8c4cac26ab"
      const data = { memberCrowdWidgetId: 'ff8080817fab027b017fcf080dd8275d' };
      this.axios.post(`api-plug/query-member-crowd-new?requestProject=${this.requestProject}`, data).then(res => {
        console.log(res);
      });
    },
    handleChange(val, id) {
      this.conditionTypeList.find(item => item.esScreeningWidgetChainId == id).value = val.join(',');
      console.log(this.conditionTypeList);
    },

    storeChange(val) {
      console.log(val);
    },
    onTabsClick(val) {
      console.log(val);
      const { name } = val;
      this.getNode(name);
    },
    close() {
      this.$emit('update:visiable', false);
    },
    confirm() {
      const data = transform(this.conditionTypeList, this.sceneCode);
      console.log(data);
      this.axios.post(`api-plug/save-member-crowd-new?requestProject=${this.requestProject}`, data).then(res => {
        let resData = res.data;
        if (resData.errorCode == 0) {
          this.$emit('save', resData.result);
          this.echoFlag = true;
        }
      });
    },

    // 获取列表
    getDataList() {
      let para = {
        sceneCode: this.sceneCode,
        requestProject: this.requestProject
      };
      this.axios
        .post('/api-plug/get-screening-init-data', qs.stringify(para))
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 0) {
            resData.result.forEach(item => {
              const { chainNodeName, templateCode, esScreeningWidgetChainId, columnKey, computeCharacter, dealKey, property } = item;
              this.conditionTypeList.push({
                chainNodeName,
                templateCode,
                esScreeningWidgetChainId,
                columnKey,
                computeCharacter,
                dealKey,
                property,
                selectValue: [],
                selectList: []
              });
            });
            this.activeName = this.conditionTypeList.length && this.conditionTypeList[0].esScreeningWidgetChainId;
            this.getNode(this.activeName);
            return;
          }
          this.$message.error({
            duration: 1000,
            message: resData.message
          });
        })
        .catch(error => {
          this.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    getNode(widgetChainId) {
      // 第二层节点的控件类型
      this.axios.get(`/api-plug/get-screening-widget-chain-detail?requestProject=${this.requestProject}&widgetChainId=${widgetChainId}`).then(res => {
        const {
          esScreeningWidgetChainId,
          widget: { sourceFlag, templateCode, widgetParam, widgetValues },
          widgetFieldKey
        } = res.data.result; // 返回的结果 从第一层开始
        this.templateCode = templateCode;
        if (sourceFlag == 1) {
          // 有接口
          const url = JSON.parse(widgetParam)[0].value;
          this.getChildNode(url, widgetFieldKey, esScreeningWidgetChainId);
        } else {
          if (templateCode == 'tag001') {
            this.conditionTypeList.find(item => item.esScreeningWidgetChainId == esScreeningWidgetChainId).selectList = JSON.parse(widgetValues);
          }
        }
      });
    },
    // 使用接口获取 第二层控件类型的内容
    getChildNode(url, key, id) {
      const param = {
        requestProject: this.requestProject,
        key
      };
      this.axios.post(`${url}`, qs.stringify(param)).then(res => {
        let resData = res.data;
        console.log(resData.result);
        if (resData.errorCode == 0) {
          this.conditionTypeList.find(item => item.esScreeningWidgetChainId == id).selectList = resData.result;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.ruleContainer {
  background: #ffffff;
  display: flex;
}
.left {
  width: 566px;
  height: 550px;
  background: #ffffff;
  border-radius: 4px;
  padding: 10px 0;
  box-sizing: border-box;
  border: 1px solid #dcdfe6;
  .leftContent {
    width: 100%;
    padding: 0 15px 0 5px;
    box-sizing: border-box;
    height: 550px;
    overflow-y: auto;
  }
}
.right {
  width: 274px;
  height: 550px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  margin-left: 12px;

  .right-top {
    padding: 20px 15px;
    display: flex;
    justify-content: space-between;
    color: #303133;
    box-sizing: border-box;
    line-height: 20px;
    background: #fff;
  }
  .right-content {
    color: #303133;
    overflow-y: auto;
    height: 89%;
  }
  .contact-li {
    padding: 0 12px 0 16px;
    margin-top: 4px;
    &:hover {
      background: #f7f8fa;
    }
    &:first-child {
      margin-top: 0;
    }
    .li-cell {
      border-radius: 2px;
      line-height: 32px;
      display: inline-flex;
      width: 100%;
      justify-content: space-between;
      padding: 0 4px;
      align-items: center;
      box-sizing: border-box;

      i {
        width: 20px;
        height: 20px;
        border-radius: 10px;
        cursor: pointer;
        font-size: 12px;
        display: inline-flex;
        justify-content: center;
        align-items: center;
        &:hover {
          background: #e5e6eb;
        }
      }
    }
  }
  .contact-li + .contact-li {
    margin-top: 4px;
  }
}
.checkBoxContainer {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  .el-checkbox + .el-checkbox {
    margin: 0;
  }
}
</style>
