<template>
  <div>
    <d2-container>
      <!-- <template slot="header">首页/扫描参数</template> -->
      <div class="main">
        <div class="left">
          <!-- <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input> -->

          <el-tree
            class="filter-tree"
            :filter-node-method="filterNode"
            v-loading="loading"
            :data="leftList"
            node-key="id"
            :props="defaultProps"
            :expand-on-click-node="false"
            @node-click="handleNodeClick"
            default-expand-all
            highlight-current
            :default-expanded-keys="this.defaultExpanded"
            ref="tree"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span v-if="node.level == 1">
                <i :class="node.icon"></i>{{ node.label }}
              </span>
              <span v-if="node.level == 2">
                <i :class="node.icon"></i>{{ node.label }}
              </span>
              <span v-if="node.level == 3">
                <i :class="node.icon"></i>{{ node.label }}
              </span>
              <span v-if="node.level == 4"> <i></i>{{ node.label }} </span>
            </span>
          </el-tree>
        </div>
        <div class="right">
          <div class="nav">当前所在位置:首页>扫描参数</div>
          <div class="right_main">
            <div class="right_title">
              <i
                class="fa fa-search-minus"
                aria-hidden="true"
                style="padding-right: 5px"
              ></i
              >扫描参数
            </div>
            <div class="right_table">
              <el-form
                :inline="true"
                :model="formInline"
                class="demo-form-inline"
                id="formLabel"
              >
                <el-form-item label="设备名称:">
                  <el-input
                    v-model="formInline.sbName"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="search"
                    >查询</el-button
                  >
                </el-form-item>

                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-refresh-left"
                    @click="reset"
                    >重置</el-button
                  >
                </el-form-item>
              </el-form>

              <el-table
                :data="hardwareList"
                border
                style="width: 100%"
                :header-cell-style="{ background: '#0064b9', color: '#c9f5fa' }"
              >
                <el-table-column
                  align="center"
                  label="序号"
                  type="index"
                  width="100px"
                  :index="indexMethod"
                  show-overflow-tooltip
                ></el-table-column>

                <el-table-column
                  prop="directLibrary"
                  label="设备名称"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="subTreasury"
                  label="设备编码"
                  align="center"
                >
                </el-table-column>

                <!-- <el-table-column align="center" label="操作" width="180">
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-edit"
                      @click="editBtn()"
                      >编辑</el-button
                    >
                  </template>
                </el-table-column> -->
              </el-table>
            </div>
          </div>
        </div>
      </div>
    </d2-container>

    <!-- //弹框 -->
    <el-dialog
      title="修改扫描规则"
      :visible.sync="dialogScanRule"
      id="dialogStyle"
    >
      <el-form ref="formData" :model="formData" :rules="rules">
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
          <el-input
            v-model="formData.sbName"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-radio-group
          v-model="formData.isOut"
          @change="radioGroupEvt"
          style="width: 100%"
        >
          <el-form-item>
            <el-radio :label="1">出入库规则设定</el-radio>
          </el-form-item>
          <el-form-item
            label="开始时间:"
            :label-width="formLabelWidth"
            prop="startHour"
          >
            <el-input
              v-model="formData.startHour"
              autocomplete="off"
              :disabled="formData.isOut == 0"
              placeholder="请输入开始时间"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox
              label="次日"
              name="type"
              v-model="checked"
              @change="CheckBox"
            ></el-checkbox>
          </el-form-item>
          <el-form-item
            label="结束时间:"
            :label-width="formLabelWidth"
            prop="endHour"
          >
            <el-input
              v-model="formData.endHour"
              autocomplete="off"
              :disabled="formData.isOut == 0"
              placeholder="请输入结束时间"
            ></el-input>
          </el-form-item>
          <el-form-item label="扫描间隔:" :label-width="formLabelWidth">
            <el-select
              style="width: 100%"
              v-model="formData.intervalHour"
              placeholder="请选择"
              :disabled="formData.isOut == 0"
            >
              <el-option
                v-for="item in intervalHourList"
                :key="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-radio :label="0">库存规则设定</el-radio>
          </el-form-item>
          <el-form-item label="扫描时间:" :label-width="formLabelWidth">
            <el-input
              v-model="formData.inventoryHour"
              autocomplete="off"
              :disabled="formData.isOut == 1"
              placeholder="请输入扫描时间"
              @blur="inventoryHourBlur"
            ></el-input>
          </el-form-item>
        </el-radio-group>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogScanRule = false">取 消</el-button>
        <el-button type="primary" @click="determine()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import api from "@/api";
export default {
  name: "scanParame",
  data() {
    //开始时间规则
    var checkStartHour = (rule, value, callback) => {
      if (this.formData.isOut == 0) {
        return callback();
      }

      if (!value) {
        return callback(new Error("开始时间不能为空"));
      }

      if (!/^\d+$/.test(value)) {
        return callback(new Error("请输入正整数"));
      }

      if (Number(value) < 0 || Number(value) > 23) {
        this.$message.info("请输入0-23范围内的数字");
        this.formData.startHour = "";
        return;
      }
      this.checkHour();
      // if (this.formData.endHour) {
      //   this.formData.endHour = "";
      // }
      callback();
    };
    //结束时间规则
    var checkEndHour = (rule, value, callback) => {
      if (this.formData.isOut == 0) {
        return callback();
      }
      console.log(value, "endHour");
      if (!value) {
        return callback(new Error("结束时间不能为空"));
      }
      if (!/^\d+$/.test(value)) {
        return callback(new Error("请输入正整数"));
      }
      if (Number(value) < 0 || Number(value) > 23) {
        this.$message.info("请输入0-23范围内的数字");
        this.formData.endHour = "";
        return;
      }

      if (this.formData.startHour) {
        if (!this.checkHour()) {
          return callback(new Error("结束时间必须大于开始时间两小时"));
        }
      }
      return callback();
    };
    //扫描间隔规则
    var checkIntervalHour = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("扫描间隔不能为空"));
      }
      if (!/^\d+$/.test(value)) {
        return callback(new Error("请输入正整数"));
      }
    };
    return {
      //验证规则
      rules: {
        startHour: [{ validator: checkStartHour, trigger: "blur" }],
        endHour: [{ validator: checkEndHour, trigger: "blur" }],
        intervalHour: [{ validator: checkIntervalHour, trigger: "blur" }],
      },
      //isOut=0(库存规则设定)    isOut=1(出入库规则设定)
      formData: {
        //默认出入库
        isOut: 1,
      },
      // label宽度
      formLabelWidth: "120px",
      // 弹框默认关闭
      dialogScanRule: false,
      filterText: "",
      //默认展开的节点
      defaultProps: {
        children: "children",
        label: "orgName",
      },
      //左侧树数据
      leftList: [
        {
          id: 0,
          label: "中储粮河南分公司",
          icon: "el-icon-tickets",
          children: [
            {
              id: 1,
              label: "商丘直属库",
              icon: "el-icon-paperclip",
              children: [
                {
                  id: 4,
                  label: "宁陵粮油储备公司",
                  children: [
                    {
                      id: 9,
                      label: "1号仓",
                    },
                    {
                      id: 10,
                      label: "2号仓",
                    },
                  ],
                },
              ],
            },
            {
              id: 2,
              label: "郑州直属库",
              icon: "el-icon-paperclip",
              children: [
                {
                  id: 5,
                  label: "中牟粮油储备公司",
                  children: [
                    {
                      id: 8,
                      label: "1号仓",
                    },
                    {
                      id: 9,
                      label: "1号仓",
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],

      formInline: {
        sbName: "",
      },
      //表格字段
      hardwareList: [
        // {
        //   directLibrary: "",
        //   subTreasury: "",
        //   cfName: "",
        //   scanStart: "",
        //   scanEnd: "",

        //   cfState: "",
        // },
      ],
      //扫描间隔列表
      intervalHourList: [2, 3, 4, 5],

      checked: false,
      disabled: false,
      loading: false,
      cfbh: null,
      //默认展开的节点
      defaultExpanded: [],
    };
  },
  watch: {
    filterTest(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    //获取左侧树
    getLeftTree() {
      this.loading = true;
      let params = {
        orgName: this.orgName,
      };

      api
        .leftTree(params)
        .then((res) => {
          if (res.code == 200) {
            console.log(res);
            this.leftList = [];
            if (res.data) {
              this.leftList = res.data;

              // if (res.data[0].id) {
              //   this.defaultExpanded.push(res.data[0].id);
              //   if (res.data[0].children) {
              //     this.defaultExpanded.push(
              //       res.data[0].children[0].children[0].id
              //     );

              //     this.cfbh =
              //       res.data[0].children[0].children[0].children[0].id;

              //     setTimeout(() => {
              //       this.$refs.tree.setCurrentKey(this.cfbh);
              //     }, 500);
              //     console.log(this.leftList);
              //     // this.getList();
              //   }
              // }
              var lastLevel = res.data;
              if (lastLevel.length > 0) {
                lastLevel = res.data[0];
                while (lastLevel.children && lastLevel.children.length > 0) {
                  lastLevel = lastLevel.children[0];
                }

                this.defaultExpanded.push(lastLevel.id);
                this.cfbh = lastLevel.id;
                setTimeout(() => {
                  this.$refs.tree.setCurrentKey(this.cfbh);
                }, 200);
                this.getList();
              }
            }
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
          this.loading = false;
        })
        .catch((error) => {
          this.loading = false;
          console.log(error);
        });
    },
    //点击左侧树节点
    handleNodeClick(data, node) {
      if(node.childNodes.length===0){
            this.cfbh = data.id;
        console.log(this.cfbh, ";;;");
        this.getList();
      }
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.orgName.indexOf(value) !== -1;
    },
    ////打开弹框时列表数据
    getLogList() {
      console.log(this.cfbh, "....");
      var condition = {
        houseId: this.cfbh,
      };
      condition = JSON.stringify(condition);
      var data = {
        condition: condition,
      };
      api
        .dataList(data)
        .then((res) => {
          if (res.code == 200) {
            console.log(res, "////");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    search() {},
    reset() {},
    //编辑
    editBtn() {
      this.dialogScanRule = true;
      this.getLogList();
    },
    //表格序号
    indexMethod(index) {
      // return (
      //   (this.pagination.curPage - 1) * this.pagination.pageSize + index + 1
      // );
    },
    //弹框确定按钮
    determine() {},

    //开始时间和结束时间的差的规则
    checkHour() {
      var startHour = this.formData.startHour;
      var endHour = this.formData.endHour;
      if (startHour === "" || endHour === "") {
        return;
      }
      //如果次日被勾选白则表示第二天需+24
      if (this.checked) {
        endHour = parseInt(endHour) + 24;
      }
      startHour = parseInt(startHour);
      //计算开始时间和结束时间的时间差
      var offset = endHour - startHour;
      // 根据开始时间和结束时间的时间差决定扫描间隔下拉选要展示的数据
      if (offset == 2) {
        this.intervalHourList = [2];
      } else if (offset <= 3) {
        this.intervalHourList = [2, 3];
      } else if (offset <= 4) {
        this.intervalHourList = [2, 3, 4];
      } else {
        this.intervalHourList = [2, 3, 4, 5];
      }
      var max = this.intervalHourList[this.intervalHourList.length - 1];
      //如果扫描时间大于扫描间隔时间或着小于2小时都重置为2小时(因为扫描时间不能大于6小时)
      if (this.formData.intervalHour > max || this.formData.intervalHour < 2) {
        this.formData.intervalHour = 2;
      }

      if (offset < 2) {
        return false;
      } else {
        return true;
      }
    },
    //扫描时间判断
    inventoryHourBlur() {
      var value = this.formData.inventoryHour;
      if (!value) {
        this.$message.info("扫描时间不能为空");
        return false;
      }
      console.log("---" + value + "---");
      if (!/^\d+$/.test(value)) {
        this.$message.info("请输入正整数");
        this.formData.inventoryHour = "";
        return false;
      } else {
        if (value > 23 || value < 0) {
          this.$message.info("扫描时间0-23");
          this.formData.inventoryHour = "";
          return false;
        } else {
          return true;
        }
      }
    },
    //次日的值改变时
    CheckBox(val) {
      this.$refs["formData"].validateField("endHour");
    },
    //radio选项改变时
    radioGroupEvt(val) {
      console.log(val);
      this.$refs["formData"].resetFields();
      if (val == "0") {
        console.log(9999);
        this.formData.startHour = "";
        this.formData.endHour = "";
        this.formData.intervalHour = "";
        this.checked = false;
      } else {
        this.formData.intervalHour = 2;
        this.formData.inventoryHour = "";
      }
    },
  },
  mounted() {
    this.getLeftTree();
  },
};
</script>
<style lang="scss" scoped>
.main {
  height: 100%;
  display: flex;
  
  .right {
    margin-left: 20px;
    width: calc(100% - 250px);
    .nav {
      padding: 10px 0 10px 10px;
      color: #fff;

      font-size: 14px;

      background: #004a93;
    }
  }
  .addBtn {
    margin-bottom: 20px;
  }
}
</style>