<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营商" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入运营商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

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

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车型" align="center" prop="model" />
      <el-table-column label="运营商" align="center" prop="operatorName" />
      <el-table-column label="满电电压(V)" align="center" prop="fullVoltage" />
      <el-table-column label="亏电电压(V)" align="center" prop="lowVoltage" />
      <el-table-column label="满电续航(km)" align="center" prop="fullEndurance" />
      <el-table-column label="骑行低电量提醒(%)" align="center" prop="lowBatteryReminder" />
      <el-table-column label="已投放车辆" align="center" prop="deviceNum" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:model:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:model:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆型号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车型" prop="model">
          <el-input v-model="form.model" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item v-if="userName == 'admin'" label="运营商" prop="operator">
          <treeselect v-model="form.operator" :options="deptOptions" :show-count="true" placeholder="选择上级运营商" />
        </el-form-item>
        <el-form-item label="满电电压(V)" label-width="90" prop="fullVoltage">
          <el-input style="width: 85%" v-model="form.fullVoltage" placeholder="请输入满电电压" />
        </el-form-item>
        <el-form-item label="亏电电压(V)" label-width="90" prop="lowVoltage">
          <el-input style="width: 85%" v-model="form.lowVoltage" placeholder="请输入亏电电压" />
        </el-form-item>
        <el-form-item label="满电续航(KM)" label-width="90" prop="fullEndurance">
          <el-input style="width: 80%" v-model="form.fullEndurance" placeholder="请输入满电续航" />
        </el-form-item>
        <el-form-item label="骑行低电量提醒(%)" label-width="120" prop="lowBatteryReminder">
          <el-input style="width: 78%" v-model="form.lowBatteryReminder" placeholder="请输入骑行低电量提醒" />
        </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 { listModel, getModel, delModel, addModel, updateModel } from "@/api/system/model";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deptTreeSelect } from '@/api/system/user'

export default {
  name: "Model",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 运营商树选项
      deptOptions: [],
      userName: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆型号表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        model: null,
        brand: null,
        operator: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        model: [
          { required: true, message: "车型不能为空", trigger: "blur" }
        ],
        operator: [
          { required: true, message: "运营商不能为空", trigger: "blur" }
        ],
        fullVoltage: [
          { required: true, message: "满电电压不能为空", trigger: "blur" },
          { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入最多两位小数的浮点数', trigger: 'blur' }

        ],
        lowVoltage: [
          { required: true, message: "亏电电压不能为空", trigger: "blur" },
          { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入最多两位小数的浮点数', trigger: 'blur' }

        ],
        fullEndurance: [
          { required: true, message: "满电续航不能为空", trigger: "blur" },
          { pattern: /^\d+$/, message: '满电续航必须为正整数', trigger: 'blur' }
        ],
        lowBatteryReminder: [
          { required: true, message: "骑行低电量提醒不能为空", trigger: "blur" },
          { pattern: /^\d+$/, message: '骑行低电量提醒必须为正整数', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    console.log("当前用户信息：",this.$store.state.user.name)
    this.userName = this.$store.state.user.name;
  },
  methods: {
    /** 查询车辆型号列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(response => {
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        modelId: null,
        model: null,
        brand: null,
        operator: null,
        fullVoltage: null,
        parentId: undefined,
        lowVoltage: null,
        fullEndurance: null,
        lowBatteryReminder: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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.modelId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询运营商下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const modelId = row.modelId || this.ids
      getModel(modelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆型号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.modelId != null) {
            updateModel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const modelIds = row.modelId || this.ids;
      this.$modal.confirm('是否确认删除车辆型号编号为"' + modelIds + '"的数据项？').then(function() {
        return delModel(modelIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/model/export', {
        ...this.queryParams
      }, `model_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
