<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item :label="$t('peisongfreight.运费')" prop="freight">
        <el-input
          v-model="queryParams.freight"
          :placeholder="$t('peisongfreight.请输入运费')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('peisongfreight.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('peisongfreight.重置')}}</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:freight:add']"
        >{{$t('peisongfreight.新增')}}</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:freight:edit']"
        >{{$t('peisongfreight.修改')}}</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:freight:remove']"
        >{{$t('peisongfreight.删除')}}</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:freight:export']"
        >{{$t('peisongfreight.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="freightList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column :label="$t('peisongfreight.开始时间')" align="center" prop="startTime" />
      <el-table-column :label="$t('peisongfreight.结束时间')" align="center" prop="endTime" />
      <el-table-column :label="$t('peisongfreight.收费')" align="center" prop="freight" />
      <el-table-column :label="$t('peisongfreight.每公里')" align="center" prop="distance" />
	  <el-table-column :label="$t('peisongfreight.起送里程')" align="center" prop="startingDistance" />
	  <el-table-column :label="$t('peisongfreight.起送价格')" align="center" prop="startingFare" />
      <el-table-column :label="$t('peisongfreight.操作')" 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:freight:edit']"
          >{{$t('peisongfreight.修改')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:freight:remove']"
          >{{$t('peisongfreight.删除')}}</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"
    />

    <!-- 添加或修改freight对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'170px':'170px'">
        <el-form-item :label="$t('peisongfreight.开始时间')" prop="startTime">
          <el-time-select
              :placeholder="$t('peisongfreight.起始时间')"
              v-model="form.startTime"
              @change="goxiaoll"
              :picker-options="{
                start: '00:00',
                step: '01:00',
                end: '23:00'
              }"></el-time-select>
        </el-form-item>
        <el-form-item :label="$t('peisongfreight.结束时间')" prop="endTime">
           <el-time-select
              :placeholder="$t('peisongfreight.结束时间')"
              v-model="form.endTime"
              :picker-options="{
                start: '00:59:59',
                step: '01:00',
                end: '23:59:59',
                minTime: form.startTime
              }">
            </el-time-select>
        </el-form-item>
        <el-form-item :label="$t('peisongfreight.距离')" prop="distance">
          <el-input v-model="form.distance" :placeholder="$t('peisongfreight.输入公里数')" />
        </el-form-item>
        <el-form-item :label="$t('peisongfreight.运费')" prop="freight">
          <el-input v-model="form.freight" :placeholder="$t('peisongfreight.输入公里数的运价')" />
        </el-form-item>
		<el-form-item :label="$t('peisongfreight.起送里程')" prop="startingDistance">
		  <el-input v-model="form.startingDistance" :placeholder="$t('peisongfreight.输入起送公里数')" />
		</el-form-item>
		<el-form-item :label="$t('peisongfreight.起送价格')" prop="startingFare">
		  <el-input v-model="form.startingFare" :placeholder="$t('peisongfreight.输入起送运价')" />
		</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('peisongfreight.确定')}}</el-button>
        <el-button @click="cancel">{{$t('peisongfreight.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFreight, getFreight, delFreight, addFreight, updateFreight } from "@/api/system/freight";

export default {
  name: "Freight",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // freight表格数据
      freightList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        freight: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goxiaoll(e){
      console.log('==============',e)
    },
    /** 查询freight列表 */
    getList() {
      this.loading = true;
      listFreight(this.queryParams).then(response => {
        this.freightList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        startTime: null,
        endTime: null,
        freight: null,
        distance: null,
		startingFare: null,
		startingDistance: 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 = this.$t('peisongfreight.添加时间段');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFreight(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('peisongfreight.修改时间段');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFreight(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('peisongfreight.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addFreight(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('peisongfreight.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('peisongfreight.是否确认删除编号为') + ids + this.$t('peisongfreight.的数据项')).then(function() {
        return delFreight(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('peisongfreight.删除成功'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/freight/export', {
        ...this.queryParams
      }, `freight_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
