<template>

  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="报文模版表" prop="templateId"/>
      <el-table-column align="center" label="报文模版名称" prop="templateName"/>
      <el-table-column align="center" label="报文模版描述" prop="templateDescribe"/>
      <el-table-column align="center" label="备注" prop="remark"/>
<!--      <el-table-column align="messageId" label="报文表" prop="carId"/>-->
<!--      <el-table-column align="messageType" label="车辆报文类别" prop="carId"/>-->
<!--      <el-table-column align="messageStartIndex" label="开始位下标" prop="carId"/>-->
<!--      <el-table-column align="messageEndIndex" label="结束位下标" prop="carFrame"/>-->
<!--      <el-table-column align="templateId" label="模版id" prop="carCoed"/>-->
<!--      <el-table-column align="messageLabel" label="报文标签" prop="carColor">-->

      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:post:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="getShow(scope.row)"
          >查看
          </el-button>
          <el-button
            v-hasPermi="['system:post:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['system:post:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改模版对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      {{this.form}}
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="报文模版表" prop="carPlate">
          <el-input v-model="form.templateId" controls-position="right" disabled />
        </el-form-item>

        <el-form-item label="报文模版名称" prop="carPlate">
          <el-input v-model="form.templateName" controls-position="right"/>
        </el-form-item>

        <el-form-item label="报文模版描述" prop="carPlate">
          <el-input v-model="form.templateDescribe" controls-position="right" />
        </el-form-item>

        <el-form-item label="备注" prop="carPlate">
          <el-input v-model="form.remark" controls-position="right"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { CarListShow, CarListShowByCarId, deleteCar, insertCar, selectCompany, updataCar } from '@/api/car/manage'
import {
  deleteTemplate,
  insertTemplate,
  selectMessageShow,
  selectTemplateByTemplateId,
  updataTemplate
} from '@/api/car/template'

export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 报文数组
      messageList : [],
      //企业
      company:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carCoed: [
          {required: true, message: "车牌号不能为空", trigger: "blur"}
        ],
        typeId: [
          {required: true, message: "车辆配置不能为空", trigger: "blur"}
        ],
        companyId: [
          {required: true, message: "所属企业不能为空", trigger: "blur"}
        ],
        carType: [
          {required: true, message: "车辆类型不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询详情
    getShow(row){
      selectMessageShow(row.templateId).then(response => {
        console.log(response)
        debugger
        this.messageList = response;
        this.open = true;
        this.title = "查询详情";
      });
    },


    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      selectMessageShow(this.queryParams).then(response => {
        debugger
        console.log(response)
        this.postList = response
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const templateId = row.templateId || this.ids
      selectTemplateByTemplateId(templateId).then(response => {
        console.log(response)
        debugger
        this.form = response;
        this.open = true;
        this.title = "修改岗位";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.templateId != undefined) {
            updataTemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.templateId || this.ids;
      this.$modal.confirm('是否确认删除为"' + carIds + '"的车辆？').then(function () {
        return deleteTemplate(templateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
