<template>
  <el-dialog
    class="photoSetDialog"
    title="设置"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="720px"
  >
    <div class="setTimeTd">
      <h3>时间表规则</h3>
      <el-descriptions title="" :column="3" border>
        <el-descriptions-item label="开始时间"> 12:00:07</el-descriptions-item>
        <el-descriptions-item label="结束时间"> 16:00:07</el-descriptions-item>
        <el-descriptions-item label="时间间隔（分）">
          <el-tag size="small">60</el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <h3>设置通道</h3>
      <div class="chooseTDBox">
        <label>选择通道</label>
        <div class="checkBox">
          <el-checkbox-group v-model="checkList">
            <el-checkbox label="通道1" border></el-checkbox>
            <el-checkbox label="通道2" border></el-checkbox>
            <el-checkbox label="通道3" border></el-checkbox>
            <el-checkbox label="通道4" border></el-checkbox>
          </el-checkbox-group>
        </div>
      </div>
      <div class="flexnr">
        <div class="w50">通道:</div>
        <el-tree
          :data="listnr"
          show-checkbox
          :props="defaultProps"
          ref="tree"
          node-key="id"
          :default-expand-all="true"
        ></el-tree>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <!-- <el-button @click="getCheckedNodes">获取</el-button> -->
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  setScheduleRulel,
  getScheduleRulelAccessList,
} from "@/utils/api/index";
export default {
  props: {
    title: String,
  },
  data() {
    return {
      isShow: false,
      formdata: {},
      rules: {
        name: [{ required: true, message: "请选择通道", trigger: "blur" }],
      },
      listnr: [],
      defaultProps: {
        children: "list",
        label: "name",
      },
      selid: 0,
      ruleid: "",
      parmsList: [], //选中的通道

      tableData: [
        {
          id: 2,
          date: "220kv",
          name: "线路11",
          address: "asdadadadad",
          children: [
            {
              id: 31,
              date: "220kv",
              name: "线路11",
              address: "asdadadadad",
            },
            {
              id: 32,
              date: "220kv",
              name: "线路11",
              address: "asdadadadad",
            },
          ],
        },
        {
          id: 3,
          date: "220kv",
          name: "线路11",
          address: "asdadadadad",
          children: [
            {
              id: 31,
              date: "220kv",
              name: "线路11",
              address: "asdadadadad",
            },
            {
              id: 32,
              date: "220kv",
              name: "线路11",
              address: "asdadadadad",
            },
          ],
        },
      ],
      multipleSelection: [],

      checkList: [],
    };
  },
  mounted() {},
  methods: {
    //判断
    getdataform(val) {
      this.selid = val.id;
      this.getlistnr();
    },
    //获取所有通道
    getlistnr() {
      getScheduleRulelAccessList({})
        .then((res) => {
          this.listnr = res.data.list;
        })
        .catch((err) => {});
    },

    //获取所选值
    getCheckedNodes() {
      this.parmsList = [];
      const checkedNodes = this.$refs.tree.getCheckedNodes(false, true); //若节点可被选择，则返回目前被选中的节点所组成的数组
      //const checkedParam = []; //定义选中的数组
      let index = -1;
      // console.log(checkedNodes);
      if (checkedNodes.length !== 0) {
        checkedNodes.forEach((item) => {
          // 父节点结构的情况 判断是否有list子节点，如果有定义数组结构
          if (item.list !== undefined) {
            index++;
            this.parmsList[index] = {
              //name: item.name,
              termid: item.id,
              channelidlist: [],
            };
          } else {
            //如果没有list 把子节点id push到 定义的数组channelidlist中
            this.parmsList[index].channelidlist.push(item.id);
          }
          //通过bsManufacturer 判断是否是  线路 获取线路id
          // if (item.bsManufacturer !== undefined) {
          //   this.ruleid = item.id;
          //   console.log(this.ruleid);
          // }
        });
      }
      // console.log(this.parmsList);
      //遍历删除没有channelichilddlist的数据，只留下通道
      this.parmsList = this.parmsList
        .filter((item) => item.channelidlist.length !== 0)
        .map((item) => {
          return {
            termid: item.termid,
            channelidlist: item.channelidlist,
          };
        });
      console.log(this.parmsList);
      return this.parmsList;
    },

    // 保存确定操作
    submitForm() {
      this.getCheckedNodes();
      if (this.parmsList.length == 0)
        return this.$message.error("通道不能为空");
      setScheduleRulel({ list: this.parmsList, ruleid: this.selid })
        .then((res) => {
          this.isShow = false;
          this.$message.success("添加成功");
          this.$parent.deviceList();
        })
        .catch((err) => {
          this.$message.error("添加失败");
        });
    },
    display() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
  },
};
</script>
<style lang="less">
.photoSetDialog {
  .el-dialog__body {
    // height: 360px;
    // overflow: auto;
  }

  .flexnr {
    height: 360px;
    overflow: auto;
    display: flex;
    align-items: flex-start;
    .el-tree {
      flex: 1;
    }
  }
  .w50 {
    width: 50px;
  }
  .setTimeTd {
    h3 {
      margin: 8px 0px;
      font-size: 16px;
      line-height: 24px;
    }
    .setCheckbox {
      .el-table__header .el-table-column--selection .cell .el-checkbox:after {
        color: #333;
        content: "全选";
        font-size: 16px;
        margin-left: 12px;
        font-weight: bold;
      }
    }
  }
  .chooseTDBox {
    display: flex;
    align-items: center;
    label {
      margin-right: 8px;
    }
  }
  .treeTable {
    margin-top: 16px;
  }
}
</style>
