<template>
  <div class="app-container">
    <el-button
      size="small"
      @click="dialogFormVisible = true"
    >
      添加模板
    </el-button>
    <el-tabs v-model="editableTabsValue" :tab-position="tabPosition" type="card" @tab-click="handleTabClick">
      <el-tab-pane
        v-for="(item, index) in editableTabs "
        :key="item.modelCode"
        :label="item.modelCode"
        :name="item.modelCode"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="报文类别" prop="messageTypeCode">
            <el-select v-model="form.messageType" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="车辆报文类型编码" prop="messageTypeCode">
            <el-select v-model="form.messageTypeCode" clearable placeholder="请选择车辆报文类型编码">
              <el-option
                v-for="item in messageTypeList"
                :key="item.messageCode"
                :label="item.messageName"
                :value="item.messageCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始位下标" prop="messageStartIndex">
            <el-input v-model="form.messageStartIndex" placeholder="请输入开始位下标" />
          </el-form-item>
          <el-form-item label="结束位下标" prop="messageEndIndex">
            <el-input v-model="form.messageEndIndex" placeholder="请输入结束位下标" />
          </el-form-item>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </el-form>

        <el-tabs @tab-click="handleTypeTabClick" >
          <el-tab-pane
            v-for="(item, index) in options"
            :key="item.value"
            :label="item.label"
            :name="item.value"
          >

            <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="车辆报文类型编码" align="center" prop="messageTypeCode" />



              <el-table-column label="开始位下标" align="center" prop="carMessageStartIndex" />
              <el-table-column label="结束位下标" align="center" prop="carMessageEndIndex" />

              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['message:message:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="添加模板" :visible.sync="dialogFormVisible">
      <el-form :model="modelCodes">
        <el-form-item label="活动名称">
          <el-input v-model="modelCodes" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTab">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { insertCarMessage, selectCarMessageList } from '@/api/car/message'

export default {
  name: "Message",
  data() {
    return {
      options: [
        {
          value: 'identification',
          label: '车辆基础'
        },
        {
          value: 'information',
          label: '车辆数据'
        },
        {
          value: 'status',
          label: '设备状态'
        }
      ],
      dialogFormVisible: false,
      tabPosition:'left',
      editableTabsValue: '',
      editableTabs: [],
      tabIndex: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆报文记录表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      messageTypeList:[],
      queryTypeParams: {
        messageName: null,
        messageType: null
      },
      modelCodes:'',
    };
  },
  created() {
    this.getList();
    this.getTypeList();
    this.getListCopy();
  },
  methods: {
    addTab() {
      let newTabName = ++this.tabIndex + '';
      this.editableTabs.push({
        modelCode: this.modelCodes,
        content: 'New Tab content'
      });
      this.editableTabsValue = newTabName;
      this.dialogFormVisible = false
    },
    handleTabClick(tab){
      this.queryParams.modelCode = tab.name
      this.form.modelCode = tab.name
      this.getList()
    },
    handleTypeTabClick(tab){
      console.log(tab.name)
      this.queryParams.messageType = tab.name
      this.getList()
    },
    /** 查询车辆报文类型列表 */
    getTypeList() {
      this.loading = true;
      listMessageType(this.queryTypeParams).then(response => {
        this.messageTypeList = response.data;
        this.loading = false;
      });
    },
    getListCopy() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.data;
        const uniqueSysLeixingpick = new Map();
        response.data.forEach(item => {
          uniqueSysLeixingpick.set(item.modelCode, item);
        });
        // 将 Map 的值（即去重后的对象）转换回数组
        this.editableTabs = Array.from(uniqueSysLeixingpick.values()) ;
        this.loading = false;
      });
    },
    /** 查询车辆报文记录列表 */
    getList() {
      this.loading = true;
      selectCarMessageList(this.queryParams).then(response => {
        this.messageList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modelCode: null,
        messageTypeCode: null,
        messageStartIndex: null,
        messageEndIndex: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      insertCarMessage(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆报文记录编号为"' + ids + '"的数据项？').then(function() {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('message/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
