<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin: 0px">
      <div>
        <div class="filter">
          <label>{{ this.$t("1132") }}：</label>
          <Cascader
            :data="datalevel"
            :value="leveldata"
            trigger="hover"
            change-on-select
            class="searchSelect"
            @on-change="getlevel"
          ></Cascader>
        </div>
        <div class="filter">
          <label>{{ this.$t("100000") }}：</label>
          <Select
            v-model="department"
            label-in-value
            @on-change="departmentchange"
            clearable
            class="searchSelect"
          >
            <Option
              v-for="item in departmentlist"
              :value="item.id"
              :key="item.id"
              >{{ item.title }}</Option
            >
          </Select>
        </div>
        <div class="filter">
          <label>{{ this.$t("200179") }}：</label>
          <Select v-model="group" label-in-value clearable class="searchSelect">
            <Option v-for="item in grouplist" :value="item.id" :key="item.id">{{
              item.name
            }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>{{ this.$t("1036") }}：</label>
          <DatePicker
            class="searchInput middleWidth"
            type="datetimerange"
            :value="daterange"
            confirm
            placeholder
            format="yyyy-MM-dd"
            @on-change="selectdate"
          ></DatePicker>
        </div>
        <!-- 次级搜索条件--隐藏 -->
        <div v-show="!morecondition">
          <div>
            <div class="filter">
              <label>{{ this.$t("100045") }}：</label>
              <Select
                v-model="shift"
                label-in-value
                clearable
                class="searchSelect"
              >
                <Option
                  v-for="item in shiftlist"
                  :value="item.id"
                  :key="item.id"
                  >{{ item.title }}</Option
                >
              </Select>
            </div>
            <div class="filter">
              <label>{{ this.$t("100046") }}：</label>
              <Select
                v-model="useritem"
                label-in-value
                clearable
                class="searchSelect"
              >
                <Option
                  v-for="item in userlist"
                  :value="item.id"
                  :key="item.id"
                  >{{ item.name }}</Option
                >
              </Select>
            </div>
          </div>
        </div>
      </div>
      <div class="searchBtn">
        <Button
          type="primary"
          class="twoWord"
          icon="ios-search"
          @click="tosearch()"
          >{{ $t("1001") }}</Button
        >
        <Button
          type="warning"
          class="twoWord"
          icon="ios-create-outline"
          @click="edit()"
          >{{ $t("1003") }}</Button
        >
        <Button
          type="error"
          class="twoWord intervalRight"
          icon="md-close-circle"
          @click="dele()"
          >{{ $t("1004") }}</Button
        >
        <Button
          type="success"
          class="sixWord"
          icon="md-add-circle"
          @click="addshift()"
          >{{ $t("950275") }}</Button
        >
        <Button
          type="success"
          class="sixWord intervalRight"
          icon="ios-build"
          @click="addshiftuser()"
          >{{ $t("950276") }}</Button
        >
        <Upload
          style="display: inline-block"
          ref="upload"
          :action="action"
          name="excel-file"
          :headers="headers"
          :show-upload-list="false"
          :on-format-error="handleFormatError"
          :on-success="handleSuccess"
          :on-error="handleError"
          :format="['xlsx']"
        >
          <Button type="primary" class="fourWord" icon="md-cloud-upload">{{
            $t("1006")
          }}</Button>
        </Upload>
      </div>
      <!-- 更多条件 按钮 -->
      <MoreConbtn
        @getCon="morecondition = !morecondition"
        :morecondition="morecondition"
      ></MoreConbtn>
    </div>
    <div class="divborder newTableDiv">
      <Table
        class="tableClass"
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData1"
        :columns="tableColumns1"
      ></Table>
      <div class="pageDiv">
        <div class="pageDirection">
          <Page
            :total="total"
            :current="page"
            @on-change="changePage"
            show-elevator
            show-sizer
            show-total
            @on-page-size-change="handlePageSize"
            :page-size-opts="[10, 20, 30, 40, 100]"
          ></Page>
        </div>
      </div>
    </div>
    <Modal class="newModalClass" v-model="addmodal" :width="600">
      <p slot="header">
        <span>{{ $t(950275) }}</span>
      </p>
      <div class="modal_body">
        <div class="filter">
          <label>
            <i>*</i>
            {{ $t(950277) }}：
          </label>
          <Input class="searchInput" v-model="addname" />
        </div>
        <div class="filter">
          <label>
            <i>*</i>
            {{ $t(950281) }}：
          </label>
          <Select
            v-model="departmentadd"
            @on-change="departmentaddchange"
            clearable
            class="searchSelect MaxWidth"
          >
            <Option v-for="v in departmentlistadd" :value="v.id" :key="v.id">{{
              v.title
            }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>
            <i>*</i>
            {{ $t(950282) }}：
          </label>
          <Select
            v-model="useradd"
            label-in-value
            :max-tag-count="4"
            clearable
            multiple
            class="searchSelect MaxWidth nowropStyle"
            filterable
            filter-by-label
          >
            <Option v-for="v in userlistadd" :value="v.id" :key="v.id" :label="v.name+'#'+v.work_code">{{
              v.name+'#'+v.work_code
            }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>
            <i>*</i>
            {{ $t(950283) }}：
          </label>
          <InputNumber
            class="searchInput"
            :min="1"
            :max="31"
            placeholder="小于31的整数"
            v-model="addinterval"
            onkeypress="return /[\d]/.test(String.fromCharCode(event.keyCode))"
            @on-change="intervalchange"
          ></InputNumber>
        </div>
        <br />
        <div class="filter" v-show="accordinterval" style="position: relative">
          <label style="position: absolute; top: 5px; left: 0">
            <i>*</i>
            {{ $t(950285) }}：
          </label>
          <div style="display: inline-block; width: 420px; margin-left: 105px">
            <div
              v-for="index in intervals"
              :key="index"
              style="display: inline-block; width: 120px"
            >
              <Select
                v-model="shifts[index]"
                clearable
                class="searchSelect MaxWidth nowropStyle"
                style="width: 120px"
              >
                <Option v-for="v in shiftlistadd" :value="v.id" :key="v.id">{{
                  v.title
                }}</Option>
              </Select>
            </div>
          </div>
        </div>
        <div class="filter" style="margin-top: 12px">
          <label>
            <i>*</i>
            {{ this.$t("950286") }}：
          </label>
          <DatePicker
            type="datetime"
            v-model="startdate"
            confirm
            format="yyyy-MM-dd"
          ></DatePicker>
        </div>
        <br />
      </div>
      <div slot="footer">
        <Button size="large" @click="closeadd">{{ $t(1033) }}</Button>
        <Button size="large" type="primary" @click="adddata">{{
          $t(1011)
        }}</Button>
      </div>
    </Modal>
    <Modal class="newModalClass" v-model="editmodal" :width="600">
      <p slot="header">
        <span>{{ $t(1003) }}</span>
      </p>
      <div class="modal_body">
        <div class="filter">
          <label>
            <i>*</i>
            {{ this.$t("950277") }}：
          </label>
          <Input class="searchInput MaxWidth" type="text" v-model="editname" />
        </div>
        <div class="filter">
          <label>
            <i>*</i>
            {{ this.$t("950281") }}：
          </label>
          <Select
            v-model="departmentedit"
            label-in-value
            class="searchSelect"
            :disabled="true"
          >
            <Option
              v-for="item in departmentlistedit"
              :value="item.id"
              :key="item.id"
              >{{ item.title }}</Option
            >
          </Select>
        </div>
        <div class="filter">
          <label>
            <i>*</i>
            {{ this.$t("950282") }}：
          </label>
          <Select
            v-model="useredit"
            label-in-value
            :max-tag-count="4"
            clearable
            multiple
            class="searchSelect MaxWidth nowropStyle"
          >
            <Option
              v-for="item in userlistedit"
              :value="item.id"
              :key="item.id"
              >{{ item.name }}</Option
            >
          </Select>
        </div>
        <div class="filter">
          <label>
            <i>*</i>
            {{ this.$t("950286") }}：
          </label>
          <DatePicker
            type="datetime"
            v-model="startdateedit"
            confirm
            format="yyyy-MM-dd"
            class="searchInput"
          ></DatePicker>
        </div>
        <br />
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="closeedit()">{{
          $t("1033")
        }}</Button>
        <Button type="primary" size="large" @click="editdata()">{{
          $t("1011")
        }}</Button>
      </div>
    </Modal>
    <Modal class="newModalClass" v-model="editequipment" :width="740">
      <p slot="header">
        <span>{{ $t(950276) }}</span>
      </p>
      <div class="modal_body">
        <table>
          <tr>
            <td style="width: 400px">
              <div class="filter">
                <label>
                  <i>*</i>
                  {{ this.$t("950281") }}：
                </label>
                <Select
                  v-model="sectionset"
                  label-in-value
                  @on-change="sectionchange"
                  class="searchSelect"
                >
                  <Option
                    v-for="item in departmentlistadd"
                    :value="item.id"
                    :key="item.id"
                    >{{ item.title }}</Option
                  >
                </Select>
              </div>
            </td>
            <td rowspan="3" style="height: 360px">
              <div
                style="
                  width: 260px;
                  height: 100%;
                  overflow-y: scroll;
                  border: 1px solid #ddd;
                  border-radius: 5px;
                  margin-left: 0px;
                "
              >
                <template>
                  <Tree
                    :data="treedata1"
                    show-checkbox
                    multiple
                    ref="tree"
                  ></Tree>
                </template>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="filter">
                <label>
                  <i>*</i>
                  {{ this.$t("950277") }}：
                </label>
                <Select
                  v-model="workgroup"
                  label-in-value
                  @on-change="workgroupchange"
                  class="searchSelect"
                >
                  <Option
                    v-for="item in workgrouplist"
                    :value="item.id"
                    :key="item.id"
                    >{{ item.name }}</Option
                  >
                </Select>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div
                style="
                  height: 300px;
                  overflow-y: scroll;
                  border: 1px solid #ddd;
                  border-radius: 5px;
                "
              >
                <div
                  style="
                    border-bottom: 1px solid #e9e9e9;
                    padding-bottom: 6px;
                    margin-bottom: 6px;
                  "
                >
                  <Checkbox
                    :indeterminate="indeterminate"
                    :value="checkAll"
                    @click.prevent.native="handleCheckAll"
                    >{{ this.$t("700018") }}</Checkbox
                  >
                </div>
                <CheckboxGroup
                  v-model="checkAllGroup"
                  style="padding-left: 20px"
                >
                  <Checkbox
                    v-for="item in groupusers"
                    :value="item.id"
                    :key="item.id"
                    :label="item.id"
                    style="text-align: left; width: 110px"
                    >{{ item.name }}</Checkbox
                  >
                </CheckboxGroup>
              </div>
            </td>
          </tr>
        </table>
      </div>
      <div slot="footer">
        <Button size="large" @click="closeeditequipment()">{{
          $t("1033")
        }}</Button>
        <Button type="primary" size="large" @click="editequipmentdata()">{{
          $t("1011")
        }}</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import axios from "@/libs/api.request";
export default {
  name: "sectionuser", // 组件不刷新
  data() {
    return {
      headers: {
        token: localStorage.getItem("token"),
        timestamp: localStorage.getItem("timestamp"),
        nonce: localStorage.getItem("nonce"),
        sign: localStorage.getItem("sign"),
      },
      action:
        axios.publicPath + "config/Upload/Submit?templateName=WorkingGroup",
      editname: "",
      departmentedit: "",
      useredit: "",
      startdateedit: "",
      morecondition: false,
      leveldata: [],
      datalevel: [],
      levelid: 0,
      tableData1: [],
      total: 0,
      page: 1,
      pagesize: 10,
      addmodal: false,
      detailobj: {},
      editmodal: false,
      editobj: {},
      department: "0",
      departmentlist: [],
      departmentadd: "0",
      departmentlistadd: [],
      departmentlistedit: [],
      group: "0",
      grouplist: [],
      shift: "0",
      shiftlist: [],
      shiftlistadd: [],
      accordinterval: false, // 班次规则框事先隐藏，当循环天数大于0时——》显示
      useritem: "0",
      userlist: [],
      useradd: "0",
      userlistadd: [],
      userlistedit: [],
      daterange: "",
      addname: "",
      addinterval: null,
      addshiftids: "",
      startdate: "",
      editequipment: false,
      workgroup: "",
      workgrouplist: [],
      sectionset: "0",
      treedata1: [],
      groupusers: [],
      allusers: "",
      checkAllGroup: [],
      checkAllSection: [],
      indeterminate: false,
      checkAll: false,
      intervals: [],
      shifts: [],
      selectIdsArr: [],
      tableColumns1: [
        {
          type: "index2",
          title: this.$t("1008"),
          key: "xuhao",
          render: (h, params) => {
            return h(
              "span",
              params.index + (this.page - 1) * this.pagesize + 1
            );
          },
          width: 90,
        },
        {
          title: this.$t("100033"),
          key: "sectionName",
          // width: 120
        },
        {
          title: this.$t("950277"),
          key: "name",
          // width: 120
        },
        {
          title: this.$t("950278"),
          key: "userCount",
        },
        {
          title: this.$t("950279"),
          key: "start_date",
        },
        {
          title: this.$t("1020"),
          key: "create_time",
        },
        {
          title: this.$t("100040"),
          key: "creator_name",
        },
      ],
    };
  },
  created() {
    this.search();
    this.initlevel();
    this.initdepartment();
    this.initgroup();
    this.initshift();
    this.initusers();
    // 格式化表格样式
    this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
  },
  methods: {
    handleFormatError(file) {
      // 导入文件类型检查
      this.$Message.error(this.$t("950396") + file.name + this.$t("950397"));
    },
    handleSuccess(res, file) {
      // 导入成功函数
      if (res.ret === 1) {
        this.$Message.success(this.$t("950398"));
        this.search(1, this.pagesize);
      } else {
        if ((res.msg + "").indexOf(",") != -1) {
          inittip(res.msg);
        } else {
          this.$Message.error(this.$t(res.msg));
        }
      }
    },
    handleError() {
      // 导入失败函数
      this.$Message.error(this.$t("950399"));
    },
    initlevel() {
      axios
        .request({
          url: "config/ProjectLevelSection/GetSectionTree",
          method: "get",
        })
        .then((res) => {
          const arr = [];
          this.datalevel = res.data.data;
          if (res.data.data && res.data.data.length > 0) {
            arr.push(res.data.data[0].value);
          }
          this.leveldata = arr;
        });
    },
    getlevel: function (data) {
      this.levelid = data[data.length - 1];
    },
    initdepartment() {
      axios
        .request({
          url: "/config/ProjectUser/getUserDepartment",
          method: "get",
        })
        .then((res) => {
          this.departmentlist = res.data.data;
          this.departmentlistadd = res.data.data;
        });
    },
    initgroup() {
      var param = { departid: this.department };
      axios
        .request({
          url: "/config/WorkingGroup/GetAllWorkGroup",
          params: param,
          method: "get",
        })
        .then((res) => {
          this.grouplist = res.data.data;
        });
    },
    initshift() {
      var url = "/config/ProjectShift/getAllShiftLists";
      if (this.department > 0) {
        url = "/config/ProjectShift/getShiftListsBySectionid";
      }
      var param = { id: this.department };
      axios
        .request({
          url: url,
          params: param,
          method: "get",
        })
        .then((res) => {
          this.shiftlist = res.data.data;
        });
    },
    initusers() {
      var param = { sectionid: this.department };
      axios
        .request({
          url: "/config/ProjectUser/getUserListsBySectionId",
          params: param,
          method: "get",
        })
        .then((res) => {
          this.userlist = res.data.data;
        });
    },
    selectdate(e) {
      this.daterange = e[0] + " - " + e[1];
    },
    departmentchange() {
      this.initgroup();
      this.initshift();
      this.initusers();
    },
    changecondition() {
      this.morecondition = !this.morecondition;
    },
    tosearch() {
      this.page = 1;
      // this.pagesize = 10
      this.search();
    },
    search() {
      var data = {
        levelId: this.levelid,
        sectionId: this.department,
        shiftId: this.shift,
        userId: this.useritem,
        workgroup_id: this.group,
        page: this.page,
        pagesize: this.pagesize,
        timerange: this.daterange,
      };
      axios
        .request({
          url: "/config/WorkingGroup/GetWorkingGroupList",
          data,
          method: "post",
        })
        .then((res) => {
          this.total = res.data.total;
          this.page = res.data.page;
          this.mockTableData1(res.data.data);
        });
    },
    mockTableData1(tablelist) {
      let data = [];
      tablelist.forEach((item) => {
        if (item) {
          data.push({
            sectionId: item.sectionId,
            sectionName: item.sectionName,
            name: item.name,
            user_mids: item.user_mids,
            userCount: item.userCount,
            start_date: item.start_date,
            create_time: item.create_time,
            creator_name: item.creator_name,
            id: item.id,
          });
        }
      });
      this.tableData1 = data;
    },
    handleRowChange(currentRow, oldCurrentRow) {
      this.detailobj = currentRow;
    },
    handlePageSize(value) {
      this.pagesize = value;
      this.tableData1 = this.search();
    },
    changePage(value) {
      this.page = value;
      this.tableData1 = this.search();
    },
    addshift() {
      this.addmodal = true;
      this.departmentadd = "";
      this.addname = "";
      this.useradd = "0";
      this.addinterval = 1;
      this.startdate = "";
      this.shifts = [];
      this.shiftlistadd = [];
      this.intervalchange();
    },
    departmentaddchange() {
      var param = { sectionid: this.departmentadd };
      axios
        .request({
          url: "/config/ProjectUser/getUserListsBySectionId",
          params: param,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret == 1) {
            this.useradd = "0";
            this.userlistadd = res.data.data;
          }
        });

      var url = "/config/ProjectShift/getAllShiftLists";
      if (this.departmentadd > 0) {
        url = "/config/ProjectShift/getShiftListsBySectionid";
      }
      var param = { id: this.departmentadd };
      axios
        .request({
          url: url,
          params: param,
          method: "get",
        })
        .then((res) => {
          this.shiftlistadd = res.data.data;
        });
    },
    intervalchange() {
      this.shifts = [];
      if (this.addinterval < 1 || this.addinterval > 31) {
        this.addinterval = 1;
      }
      if (this.addinterval) {
        this.accordinterval = true;
      } else {
        this.accordinterval = false;
      }
      this.intervals = [];
      for (var i = 1; i <= this.addinterval; i++) {
        this.intervals.push(i);
      }
    },
    closeadd() {
      this.addmodal = false;
    },
    adddata() {
      var arry = [];
      for (var item in this.useradd) {
        arry.push(this.useradd[item]);
      }
      var usernames = arry.join(",");

      // for (var i = 1; i < this.shifts.length; i++) {
      //   console.log(this.shifts[i])
      //   if (Array.isArray(this.shifts[i])) {
      //     this.$Message.error(this.$t('700078'));
      //     return false;
      //   }
      // }

      this.selectIdsArr = [];
      for (var i = 1; i < this.shifts.length; i++) {
        this.selectIdsArr.push(this.shifts[i]);
      }

      var shiftIds = this.selectIdsArr.join(",");
      if (
        this.addname === "" ||
        this.departmentadd === "0" ||
        usernames.length === 0 ||
        shiftIds.length === 0 ||
        !this.addinterval ||
        this.startdate === ""
      ) {
        this.$Message.error(this.$t("700078"));
        return false;
      }
      if (this.addinterval != this.selectIdsArr.length) {
        this.$Message.error(this.$t("101112"));
        return false;
      }
      var data = {
        name: this.addname,
        section_id: this.departmentadd,
        user_mids: usernames,
        interval: this.addinterval,
        shift_ids: shiftIds,
        start_date: this.startdate,
      };
      axios
        .request({
          url: "/config/WorkingGroup/AddWorkingGroup",
          data,
          method: "post",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            this.$Message.success(this.$t("1064"));
            this.search();
            this.addmodal = false;
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        });
    },
    dele() {
      if (this.detailobj === undefined || this.detailobj.id === undefined) {
        this.$Message.error(this.$t("1017"));
        return false;
      } else {
        this.$Modal.confirm({
          title: this.$t("1018"),
          content: "",
          closable: true,
          okText: this.$t("1004"),
          onOk: () => {
            this.$Spin.show();
            axios
              .request({
                url:
                  "/config/WorkingGroup/DeleteWorkingGroup?id=" +
                  this.detailobj.id,
                method: "post",
              })
              .then((res) => {
                this.$Spin.hide();
                if (res.data.ret === 1) {
                  this.$Message.success(this.$t("1058"));
                  this.search();
                  this.detailobj = {};
                } else {
                  this.$Message.error(this.$t(res.data.msg));
                }
              });
          },
        });
      }
    },
    initeditdepartment() {
      axios
        .request({
          url: "/config/ProjectUser/getUserDepartment",
          method: "get",
        })
        .then((res) => {
          this.departmentlistedit = res.data.data;
        });
    },
    initeditusers() {
      var param = { sectionid: this.departmentedit };
      axios
        .request({
          url: "/config/ProjectUser/getUserListsBySectionId",
          params: param,
          method: "get",
        })
        .then((res) => {
          this.userlistedit = res.data.data;
          var arr = this.detailobj.user_mids.split(",");
          var arr1 = [];
          arr.forEach((ele) => {
            arr1.push(parseInt(ele));
          });
          this.useredit = arr1;
        });
    },
    edit() {
      if (
        JSON.stringify(this.detailobj) === "{}" ||
        this.detailobj === undefined ||
        this.detailobj === null
      ) {
        this.$Message.error(this.$t("1043"));
        return false;
      }
      this.editname = this.detailobj.name;
      this.startdateedit = this.detailobj.start_date;
      this.initeditdepartment();
      this.departmentedit = this.detailobj.sectionId;
      this.initeditusers();

      this.editmodal = true;
    },
    editdata() {
      if (this.editname === undefined || this.editname === "") {
        this.$Message.error(this.$t("950280"));
        return false;
      }
      if (this.useredit === undefined || this.useredit === "") {
        this.$Message.error(this.$t("200161"));
        return false;
      }
      if (this.startdateedit === undefined || this.startdateedit === "") {
        this.$Message.error(this.$t("9000414"));
        return false;
      }
      var ids = "";
      this.useredit.forEach((ele) => {
        ids += ele + ",";
      });
      if (ids === "") {
        this.$Message.error(this.$t("960579"));
        return false;
      }
      var data = {
        id: this.detailobj.id,
        sectionid: this.detailobj.sectionid,
        name: this.editname,
        start_date: this.startdateedit,
        user_mids: ids,
      };
      axios
        .request({
          url: "/config/WorkingGroup/UpdateWorkingGroup",
          data,
          method: "post",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            this.$Message.success(this.$t("1064"));
            this.search();
            this.editmodal = false;
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        });
    },
    closeedit() {
      this.editmodal = false;
    },
    getchildren(item) {
      let arry = [];
      if (
        item.children !== undefined &&
        item.children !== null &&
        item.children !== [] &&
        item.children.length > 0
      ) {
        item.children.forEach((child) => {
          let data = {};
          data.id = child.id;
          data.title = child.title;
          data.expand = true;
          data.checked = this.checkitem(child.id, child.checked);
          data.children = this.getchildren(child);
          arry.push(data);
        });
      }
      return arry;
    },
    inittree() {
      axios
        .request({
          url: "/config/ProjectLevelSection/GetSectionTreeForUserSetting",
          method: "get",
        })
        .then((res) => {
          let data = res.data.data.children;
          data.forEach((item) => {
            let data = {};
            data.id = item.id;
            data.title = item.title;
            data.expand = true;
            data.checked = this.checkitem(item.id, item.checked);
            data.children = this.getchildren(item);
            this.treedata1.push(data);
          });
        });
    },
    checkitem(id, checked) {
      if (this.checkAllSection === null || this.checkAllSection === undefined) {
        return false;
      }
      if (this.checkAllSection.indexOf(id) > -1) {
        return true;
      } else {
        return checked;
      }
    },
    addshiftuser() {
      this.editequipment = true;
      this.sectionset = "0";
      this.treedata1 = [];
      this.workgroup = "0";
      this.checkAllGroup = [];
      this.checkAllSection = [];
      this.indeterminate = false;
      this.checkAll = false;
    },
    closeeditequipment() {
      this.editequipment = false;
    },
    sectionchange() {
      this.groupusers = [];
      this.checkAllGroup = [];
      var param = { departid: this.sectionset };
      axios
        .request({
          url: "/config/WorkingGroup/GetAllWorkGroup",
          params: param,
          method: "get",
        })
        .then((res) => {
          this.workgrouplist = res.data.data;
          this.workgroup = "0";
        });
    },
    workgroupchange() {
      if (this.sectionset === undefined || this.workgroup === undefined) {
        return;
      }
      var param = { id: this.workgroup };
      axios
        .request({
          url: "/config/WorkingGroup/GetAllUsersByWorkGroup",
          params: param,
          method: "get",
        })
        .then((res) => {
          this.groupusers = res.data.data;
        });

      this.checkAllSection = [];
      this.checkAllGroup = [];
      var param2 = {
        sectionId: this.sectionset,
        workinggroupId: this.workgroup,
      };
      axios
        .request({
          url: "/config/WorkingGroup/GetWorkingMachine",
          params: param2,
          method: "get",
        })
        .then((res) => {
          var result = res.data.data;
          if (result.machine_ids !== "") {
            var machineIds = result.machine_ids.split(",");
            if (machineIds.length > 0) {
              var arr1 = [];
              machineIds.forEach(function (u, ele) {
                arr1.push(parseInt(u));
              });
              this.checkAllSection = arr1;
            }
          }

          if (result.user_mids !== "") {
            var userIds = result.user_mids.split(",");
            if (userIds.length > 0) {
              var arr = [];
              userIds.forEach(function (u, ele) {
                arr.push(parseInt(u));
              });
              this.checkAllGroup = arr;
            }
          }
          this.treedata1 = [];
          this.inittree();
        });
    },
    handleCheckAll() {
      if (this.indeterminate) {
        this.checkAll = false;
      } else {
        this.checkAll = !this.checkAll;
      }
      this.indeterminate = false;
      if (this.checkAll) {
        if (this.groupusers.length > 0) {
          var arr = [];
          this.groupusers.forEach(function (u, ele) {
            arr.push(u.id);
          });
          this.checkAllGroup = arr;
        }
      } else {
        this.checkAllGroup = [];
      }
    },
    editequipmentdata() {
      let nodes = this.$refs.tree.getCheckedNodes();
      let section_Ids = [];
      nodes.forEach((item) => {
        if (!item.children.length) {
          section_Ids.push(item.id);
        }
      });
      // if (
      //   this.sectionset === "0" ||
      //   this.workgroup === "0" ||
      //   this.checkAllGroup.length === 0 ||
      //   section_Ids.length === 0
      // ) {
      //   this.$Message.error(this.$t("700078"));
      //   return false;
      // }
      if (
        this.sectionset === "0" ||
        this.sectionset === null ||
        this.sectionset === undefined
      ) {
        this.$Message.error(this.$t("5500270"));
        return false;
      }
      if (
        this.workgroup === "0" ||
        this.workgroup === null ||
        this.workgroup === undefined
      ) {
        this.$Message.error(this.$t("9000417"));
        return false;
      }
      if (this.checkAllGroup.length === 0) {
        this.$Message.error(this.$t("200161"));
        return false;
      }
      if (section_Ids.length === 0) {
        this.$Message.error(this.$t("700071"));
        return false;
      }

      var data = {
        section_id: this.sectionset,
        workgroup_id: this.workgroup,
        user_mids: this.checkAllGroup.join(","),
        machine_ids: section_Ids.join(","),
      };
      this.handleSpinCustom();
      axios
        .request({
          url: "/config/WorkingGroup/SetWorkingMachine",
          data,
          method: "post",
        })
        .then((res) => {
          this.$Spin.hide();
          if (res.data.ret === 1) {
            this.editequipment = false;
            this.$Message.success(this.$t("1064"));
          } else {
            if (typeof res.data.msg == "string") {
              this.custom_msg(res.data.msg);
            } else {
              this.$Message.error(this.$t(res.data.msg));
            }
          }
        })
        .catch((e) => {
          this.$Spin.hide();
          this.$Message.error(this.$t("9999999"));
        });
    },
    custom_msg(msg) {
      if (typeof msg != "string") {
        return;
      }
      let message = msg.toString();
      let str = "";
      if (message.indexOf(";") != -1) {
        let errs = message.split(";");
        errs.forEach((v) => {
          str += v + "<br/>";
        });
      } else {
        str += message;
      }
      window_warning(str);
    },
    handleSpinCustom() {
      this.$Spin.show({
        render: (h) => {
          return h("div", [
            h("Icon", {
              class: "demo-spin-icon-load",
              props: {
                type: "ios-loading",
                size: 18,
              },
            }),
            h("div", "Loading"),
          ]);
        },
      });
      // setTimeout(() => {
      //     this.$Spin.hide();
      // }, 3000);
    },
  },
};
</script>
<style lang="less" scoped>
.demo-spin-icon-load {
  animation: ani-demo-spin 1s linear infinite;
}
</style>
