<template>
  <div class="app-container">
    <el-radio-group v-model="tabPosition" style="margin-bottom: 30px">
      <el-radio-button label="deviceInfo">设备信息</el-radio-button>
      <el-radio-button label="Loto">锁定站</el-radio-button>

    </el-radio-group>

    <!-- 设备列表 -->
    <div class="device" v-if="this.tabPosition == 'deviceInfo'">
      <Tinymce  />
    </div>
    <!-- LOTO站 -->
    <div class="loto">
      <MapData v-if="tabPosition=='Loto'" :machineryId="this.$route.query.machineryId"></MapData>
    </div>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="450px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="上级" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :options="machineryOptions"
            :normalizer="normalizer"
            placeholder="选择上级"
          />
        </el-form-item>

        <el-form-item label="设备/工艺名称" prop="machineryName">
          <el-input
            v-model="form.machineryName"
            placeholder="请输入设备/工艺名称"
          />
        </el-form-item>
        <el-row>
          <el-col :span="18">
            <el-form-item label="设备/工艺编号" prop="machineryCode">
              <el-input
                v-model="form.machineryCode"
                placeholder="请输入设备/工艺编号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="30">
              <el-switch
                v-model="autoGenFlag"
                active-color="#13ce66"
                active-text="自动生成"
                @change="handleAutoGenChange(autoGenFlag)"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
<!--        mars岗位-->
        <el-form-item label="岗位" prop="workstationId">
          <treeselect
            v-model="form.workstationId"
            :options="marsOptions"
            :normalizer="Marsnormalizer"
            placeholder="请选择岗位"
          />
        </el-form-item>
        <el-form-item label="所属电柜" prop="lotoId">
          <el-select
            style="width: 300px"
            v-model="form.lotoId"
            placeholder="请选择所属电柜"
          >
            <el-option
              v-for="dict in LotoOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备/工艺类型" prop="machineryType">
          <el-input
            v-model="form.machineryType"
            placeholder="请输入设备/工艺类型"
          />
        </el-form-item>

        <el-form-item label="工艺图" prop="machineryImg">
          <ImageUploadSingle
            :limit="1"
            :value="form.machineryImg"
            :fileSize="5"
            @onUploaded="handleIconUplaoded"
            @onRemoved="handleIconRemoved"
          ></ImageUploadSingle>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm"
        >确 定</el-button
        >
        <el-button v-no-more-click @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTechnology,
  addTechnology,
  updateTechnology,
  delTechnology,
  getTechnologyInfo,
} from "@/api/system/machinery";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { genCode } from "@/api/system/autocode/rule";
import { listMarsDept } from "@/api/system/marsdept";
import { listLoto } from "@/api/mes/lotoStation/lotoStation";
import MapData from '@/views/mes/dv/technology/technologyDetail/MapData.vue'
import Tinymce from '@/components/tinymce/example/Index.vue'
export default {
  name: "Dept",
  dicts: ["sys_normal_disable"],
  components: { MapData, Treeselect,Tinymce },
  data() {
    return {
      // 遮罩层
      loading: true,
      //自动生成编码
      autoGenFlag: false,
      tabPosition: "deviceInfo",
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // mars树选项
      marsOptions: [],
      //工艺树
      machineryOptions: [],
      // 电柜
      lotoOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pages: 1,
        size: -1,
        machineryName: undefined,
        machineryCode: undefined,
      },
      LotoOptions: [], //电柜绑定
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        machineryCode: [
          { required: true, message: "设备/工艺编码不能为空", trigger: "blur" },
        ],
        machineryName: [
          { required: true, message: "设备/工艺名称不能为空", trigger: "blur" },
        ],
        workstationId: [
          { required: true, message: "岗位不能为空", trigger: "blur" },
        ],
        lotoId: [{ required: true, message: "电柜不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    "form.workstationId": function (newVal, oldVal) {
      if (newVal) {
        const data = {
          pasge: 1,
          size: -1,
          workstationId: this.form.workstationId,
        };
        listLoto(data).then((response) => {
          this.LotoOptions = response.data.records.map((item) => {
            return {
              value: item.lotoId,
              label: item.lotoName,
            };
          });
        });
      }
    },
    // 监听 tabPosition 的变化
    tabPosition(newVal) {
      if (newVal === "craftInformation") {
        // 当 tabPosition 为 "craftInformation" 时跳转
        this.$router.push("/technology/technologyList");
      }
    },
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      listTechnology(this.queryParams).then((response) => {
        debugger;
        this.deptList = this.handleTree(
          response.data.records,
          "machineryId",
          "parentId"
        );
        this.machineryOptions = this.handleTree(
          response.data.records,
          "machineryId",
          "parentId"
        );
        console.log(this.deptList, "设备/工艺列表");
        this.loading = false;
      });
      const data = {
        pasge: 1,
        size: -1,
      };
      listMarsDept(data).then((response) => {
        this.marsOptions = this.handleTree(
          response.data.records,
          "workstationId",
          "parentId"
        );
      });
      listLoto(data).then((response) => {
        this.LotoOptions = response.data.records.map((item) => {
          return {
            value: item.lotoId,
            label: item.lotoName,
          };
        });
      });
    },
    /** 转换部门数据结构 */
    Marsnormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.workstationId,
        label: node.workstationName,
        children: node.children,
      };
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.machineryId,
        label: node.machineryName,
        children: node.children,
      };
    },
    // 地图预览
    look(row) {
      console.log(row, "row预览");
      const data = row.lotoId;
      this.$router.push(
        `/mes/dv/technology/technologyList/index/MapData?lotoId=${data}&machineryId=${row.machineryId}`
      );
      // this.dialogVisibleMap = true; // 显示地图预览弹框
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        machineryId: undefined,
        parentId: undefined,
        machineryName: undefined,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("TECHNOLOGY_CODE").then((response) => {
          console.log(response, "设备工艺编码");
          this.form.machineryCode = response;
        });
      } else {
        this.form.machineryCode = null;
      }
    },
    //图标上传成功
    handleIconUplaoded(imgUrl) {
      this.form.machineryImg = imgUrl[0].url;
    },
    // 图标移除
    handleIconRemoved(imgUrl) {
      this.form.machineryImg = null;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != null && row.machineryId) {
        this.form.parentId = row.machineryId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "新增设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getTechnologyInfo(row.machineryId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.machineryId != undefined) {
            updateTechnology(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechnology(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.codes = selection.map((item) => item.machineryCode);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function () {
          return delTechnology(row.machineryId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
.app-container {
  width: 100%;
  height: 100%;
}
.left {
  width: 15%;
  height: 100%;
  margin-right: 10px;
  /* background: green; */
}
.deptTree {
  width: 100%;
  height: 90%;
  /* background: pink; */
}
.right {
  width: 83%;
  height: 100%;
  /* background: yellow; */
}
</style>
