<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="所属单位" prop="appEncode">
        <el-input
          v-model="queryParams.appEncode"
          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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zcustom:platPublicCar:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zcustom:platPublicCar:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-upload"
          size="mini"
          @click="handleUpload"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="platPublicCarList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          {{(scope.$index + 1) + ((queryParams.pageNum - 1) * queryParams.pageSize)}}
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="appEncode" />
      <el-table-column label="公务车实有总数" align="center" prop="totalCarNum" />
      <el-table-column label="商务车数量" align="center" prop="carNumOne" />
      <el-table-column label="越野车数量" align="center" prop="carNumTwo" />
      <el-table-column label="小轿车数量" align="center" prop="carNumThree" />
      <el-table-column label="小型客车数量" align="center" prop="carNumFour" />
      <el-table-column label="大型客车数量" align="center" prop="carNumFive" />
      <el-table-column label="企业用车数量" align="center" prop="enterpriseCarNum" />
      <el-table-column label="企业负责人用车数量" align="center" prop="headCarNum" />
      <el-table-column label="燃油车数量" align="center" prop="oilCarNum" />
      <el-table-column label="新能源车数量" align="center" prop="newPowerCarNum" />
      <el-table-column label="公务车编制数" align="center" prop="establishmentsCarNum" />
      <el-table-column label="公务车年度增加数" align="center" prop="yearAddNum" />
      <el-table-column label="公务车年度减少数" align="center" prop="yearReduceNum" />
      <el-table-column label="公务车终端数" align="center" prop="terminalTotal" />
      <el-table-column label="公务车终端安装数" align="center" prop="terminalInstall" />
      <el-table-column label="公务车终端在线数" align="center" prop="terminalOnline" />
      <el-table-column label="处置未归档数量" align="center" prop="notArchivedNum" />
      <el-table-column label="未带工单数" align="center" prop="noPidNum" />
      <el-table-column label="超速预警数" align="center" prop="overSpeedNum" />
      <el-table-column label="节假日预警数" align="center" prop="holidayNum" />
      <el-table-column label="派车单总数" align="center" prop="pcdNum" />
      <el-table-column label="均车派车单数" align="center" prop="avgPcdNum" />
      <el-table-column label="公务车行驶总里程" align="center" prop="mileCount" />
      <el-table-column label="均车里程" align="center" prop="avgMile" />
      <el-table-column label="操作" align="center" fixed="right" 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="['zcustom:platPublicCar:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zcustom:platPublicCar: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" :close-on-click-modal="false" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="所属单位" prop="appEncode">
          <el-input v-model="form.appEncode" placeholder="请输入所属单位" />
        </el-form-item>
        <el-form-item label="公务车实有总数" prop="totalCarNum">
          <el-input v-model="form.totalCarNum" placeholder="请输入公务车实有总数" />
        </el-form-item>
        <el-form-item label="商务车数量" prop="carNumOne">
          <el-input v-model="form.carNumOne" placeholder="请输入商务车数量" />
        </el-form-item>
        <el-form-item label="越野车数量" prop="carNumTwo">
          <el-input v-model="form.carNumTwo" placeholder="请输入越野车数量" />
        </el-form-item>
        <el-form-item label="小轿车数量" prop="carNumThree">
          <el-input v-model="form.carNumThree" placeholder="请输入小轿车数量" />
        </el-form-item>
        <el-form-item label="小型客车数量" prop="carNumFour">
          <el-input v-model="form.carNumFour" placeholder="请输入小型客车数量" />
        </el-form-item>
        <el-form-item label="大型客车数量" prop="carNumFive">
          <el-input v-model="form.carNumFive" placeholder="请输入大型客车数量" />
        </el-form-item>
        <el-form-item label="企业用车数量" prop="enterpriseCarNum">
          <el-input v-model="form.enterpriseCarNum" placeholder="请输入企业用车数量" />
        </el-form-item>
        <el-form-item label="企业负责人用车数量" prop="headCarNum">
          <el-input v-model="form.headCarNum" placeholder="请输入企业负责人用车数量" />
        </el-form-item>
        <el-form-item label="燃油车数量" prop="oilCarNum">
          <el-input v-model="form.oilCarNum" placeholder="请输入燃油车数量" />
        </el-form-item>
        <el-form-item label="新能源车数量" prop="newPowerCarNum">
          <el-input v-model="form.newPowerCarNum" placeholder="请输入新能源车数量" />
        </el-form-item>
        <el-form-item label="公务车编制数" prop="establishmentsCarNum">
          <el-input v-model="form.establishmentsCarNum" placeholder="请输入公务车编制数" />
        </el-form-item>
        <el-form-item label="公务车年度增加数" prop="yearAddNum">
          <el-input v-model="form.yearAddNum" placeholder="请输入公务车年度增加数" />
        </el-form-item>
        <el-form-item label="公务车年度减少数" prop="yearReduceNum">
          <el-input v-model="form.yearReduceNum" placeholder="请输入公务车年度减少数" />
        </el-form-item>
        <el-form-item label="公务车终端数" prop="terminalTotal">
          <el-input v-model="form.terminalTotal" placeholder="请输入公务车终端数" />
        </el-form-item>
        <el-form-item label="公务车终端安装数" prop="terminalInstall">
          <el-input v-model="form.terminalInstall" placeholder="请输入公务车终端安装数" />
        </el-form-item>
        <el-form-item label="公务车终端在线数" prop="terminalOnline">
          <el-input v-model="form.terminalOnline" placeholder="请输入公务车终端在线数" />
        </el-form-item>
        <el-form-item label="处置未归档数量" prop="notArchivedNum">
          <el-input v-model="form.notArchivedNum" placeholder="请输入处置未归档数量" />
        </el-form-item>
        <el-form-item label="未带工单数" prop="noPidNum">
          <el-input v-model="form.noPidNum" placeholder="请输入未带工单数" />
        </el-form-item>
        <el-form-item label="超速预警数" prop="overSpeedNum">
          <el-input v-model="form.overSpeedNum" placeholder="请输入超速预警数" />
        </el-form-item>
        <el-form-item label="节假日预警数" prop="holidayNum">
          <el-input v-model="form.holidayNum" placeholder="请输入节假日预警数" />
        </el-form-item>
        <el-form-item label="派车单总数" prop="pcdNum">
          <el-input v-model="form.pcdNum" placeholder="请输入派车单总数" />
        </el-form-item>
        <el-form-item label="均车派车单数" prop="avgPcdNum">
          <el-input v-model="form.avgPcdNum" placeholder="请输入均车派车单数" />
        </el-form-item>
        <el-form-item label="公务车行驶总里程" prop="mileCount">
          <el-input v-model="form.mileCount" placeholder="请输入公务车行驶总里程" />
        </el-form-item>
        <el-form-item label="均车里程" prop="avgMile">
          <el-input v-model="form.avgMile" 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>

    <el-dialog title="导入json" :visible.sync="op" :close-on-click-modal="false" width="600px" append-to-body>
      <el-form ref="form" :rules="rules" label-width="80px">
        <el-form-item label="json来源">
          <el-select v-model="value" 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="json数据">
          <el-input v-model="json" type="textarea" placeholder="请输入json数据" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sure">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listPlatPublicCar,
  getPlatPublicCar,
  delPlatPublicCar,
  addPlatPublicCar,
  updatePlatPublicCar,
  uploadJson
} from '@/api/zcustom/public'

export default {
  name: "PlatPublicCar",
  data() {
    return {
      value: null,
      options:[
        {
          value: '1',
          label: '资源台账-按照月度统计'
        }, {
          value: '2',
          label: '资源台账-按照年度统计'
        }, {
          value: '3',
          label: '资源台账-公务车实有数展开-1'
        }, {
          value: '4',
          label: '资源台账-公务车实有数展开-2'
        }, {
          value: '5',
          label: '资源台账-公务车实有数展开-3'
        }, {
          value: '6',
          label: '运行管理-公务车派车单总数'
        }, {
          value: '7',
          label: '监控管理-公务车终端安装率、在线率'
        }, {
          value: '8',
          label: '监控管理-公务车处置未归档数量'
        }, {
          value: '9',
          label: '监控管理-未带工单预警、超速预警数、节假日预警数'
        }
      ],
      json: undefined,
      op:false,
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公务用车表格数据
      platPublicCarList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appEncode: null,
        totalCarNum: null,
        carNumOne: null,
        carNumTwo: null,
        carNumThree: null,
        carNumFour: null,
        carNumFive: null,
        enterpriseCarNum: null,
        headCarNum: null,
        oilCarNum: null,
        newPowerCarNum: null,
        establishmentsCarNum: null,
        yearAddNum: null,
        yearReduceNum: null,
        terminalInstall: null,
        terminalTotal: null,
        terminalOnline: null,
        notArchivedNum: null,
        noPidNum: null,
        overSpeedNum: null,
        holidayNum: null,
        pcdNum: null,
        avgPcdNum: null,
        mileCount: null,
        avgMile: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        value: [
          { required: true, message: "来源不能为空", trigger: "blur" }
        ],
        json: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ],
        appEncode: [
          { required: true, message: "所属单位不能为空", trigger: "blur" }
        ],
        totalCarNum: [
          { required: true, message: "公务车实有总数不能为空", trigger: "blur" }
        ],
        carNumOne: [
          { required: true, message: "商务车数量不能为空", trigger: "blur" }
        ],
        carNumTwo: [
          { required: true, message: "越野车数量不能为空", trigger: "blur" }
        ],
        carNumThree: [
          { required: true, message: "小轿车数量不能为空", trigger: "blur" }
        ],
        carNumFour: [
          { required: true, message: "小型客车数量不能为空", trigger: "blur" }
        ],
        carNumFive: [
          { required: true, message: "大型客车数量不能为空", trigger: "blur" }
        ],
        enterpriseCarNum: [
          { required: true, message: "企业用车数量不能为空", trigger: "blur" }
        ],
        headCarNum: [
          { required: true, message: "企业负责人用车数量不能为空", trigger: "blur" }
        ],
        oilCarNum: [
          { required: true, message: "燃油车数量不能为空", trigger: "blur" }
        ],
        newPowerCarNum: [
          { required: true, message: "新能源车数量不能为空", trigger: "blur" }
        ],
        establishmentsCarNum: [
          { required: true, message: "公务车编制数不能为空", trigger: "blur" }
        ],
        yearAddNum: [
          { required: true, message: "公务车年度增加数不能为空", trigger: "blur" }
        ],
        yearReduceNum: [
          { required: true, message: "公务车年度减少数不能为空", trigger: "blur" }
        ],
        terminalTotal: [
          { required: true, message: "公务车终端数不能为空", trigger: "blur" }
        ],
        terminalInstall: [
          { required: true, message: "公务车终端安装数不能为空", trigger: "blur" }
        ],
        terminalOnline: [
          { required: true, message: "公务车终端在线数不能为空", trigger: "blur" }
        ],
        notArchivedNum: [
          { required: true, message: "处置未归档数量不能为空", trigger: "blur" }
        ],
        noPidNum: [
          { required: true, message: "未带工单数不能为空", trigger: "blur" }
        ],
        overSpeedNum: [
          { required: true, message: "超速预警数不能为空", trigger: "blur" }
        ],
        holidayNum: [
          { required: true, message: "节假日预警数不能为空", trigger: "blur" }
        ],
        pcdNum: [
          { required: true, message: "派车单总数不能为空", trigger: "blur" }
        ],
        avgPcdNum: [
          { required: true, message: "均车派车单数不能为空", trigger: "blur" }
        ],
        mileCount: [
          { required: true, message: "公务车行驶总里程不能为空", trigger: "blur" }
        ],
        avgMile: [
          { required: true, message: "均车里程不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    sure(){
      const jsonString = JSON.parse(this.json)
      console.log(jsonString)
      uploadJson(this.value, jsonString).then(res => {

      })
    },

    /** 查询公务用车列表 */
    getList() {
      this.loading = true;
      listPlatPublicCar(this.queryParams).then(response => {
        this.platPublicCarList =  response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.op = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        appEncode: null,
        totalCarNum: null,
        carNumOne: null,
        carNumTwo: null,
        carNumThree: null,
        carNumFour: null,
        carNumFive: null,
        enterpriseCarNum: null,
        headCarNum: null,
        oilCarNum: null,
        newPowerCarNum: null,
        establishmentsCarNum: null,
        yearAddNum: null,
        yearReduceNum: null,
        terminalTotal: null,
        terminalInstall: null,
        terminalOnline: null,
        notArchivedNum: null,
        noPidNum: null,
        overSpeedNum: null,
        holidayNum: null,
        pcdNum: null,
        avgPcdNum: null,
        mileCount: null,
        avgMile: 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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公务用车";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlatPublicCar(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公务用车";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlatPublicCar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlatPublicCar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delPlatPublicCar(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入操作 */
    handleUpload(){
      this.op = true
    }
  }
};
</script>

