<!-- 收银台订单-->
<template>
  <div style="padding-top: 15px;padding-bottom: 10px; padding-right: 40px; background: #f6f8f9;">
    <el-card class="box-card" shadow="never">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="120px">

        <el-form-item label="" prop="fleetName" style="width: 180px">
          <el-input
            v-model="queryParams.fleetName"
            placeholder="请输入车队名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="gradeId" style="width: 180px">
          <el-select
            v-model="queryParams.gradeId"
            clearable
            placeholder="请选择会员等级"
          >
            <el-option v-for="item in userGradeList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="status" style="width: 180px">
          <el-select
            v-model="queryParams.status"
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-date-picker
            v-model="beginTime"
            style="width: 160px"
            type="date"
            placeholder="开始日期">
          </el-date-picker>
          至
          <el-date-picker
            v-model="endTime"
            style="width: 160px"
            type="date"
            placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleAdd">新增车队</el-button>
        </el-form-item>

      </el-form>


      <!--    </el-card>-->
      <!--    <el-card class="box-card" style="min-height: 500px;" shadow="never">-->
      <div class="table-box">
        <el-table
          :data="orderList" border
          style="width: 100%">
          <el-table-column type="index" width="50" align="center" label="序号"/>
          <el-table-column prop="fleetName" align="center" label="车队名称"></el-table-column>
          <el-table-column prop="cardType" align="center" label="卡类型"></el-table-column>
          <el-table-column prop="fleetLeader" align="center" label="车队负责人"></el-table-column>
          <el-table-column prop="mobile" align="center" label="手机号" width="120"></el-table-column>
          <el-table-column prop="gradeName" align="center" label="会员等级"></el-table-column>
          <el-table-column prop="oilHead" align="center" label="油站负责人"></el-table-column>
          <el-table-column align="center" prop="totalBalance" label="总余额"></el-table-column>
          <el-table-column align="center" prop="rechargeAmount" label="充值本金">
            <template slot-scope="scope">
              <span>{{ scope.row.rechargeAmount || "--" }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="giveAmount" label="赠送金额">
            <template slot-scope="scope">
              <span>{{ scope.row.giveAmount || "--" }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="corporateName" label="公司名称">
            <template slot-scope="scope">
              <span>{{ scope.row.corporateName || "--" }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="status" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="qy"
                inactive-value="jy"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="remark" label="备注">
            <template slot-scope="scope">
              <span>{{ scope.row.remark || "--" }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="accountName" label="创建人"></el-table-column>
          <el-table-column align="center" prop="createTime" label="创建时间" width="150"></el-table-column>
          <el-table-column align="center" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleRecharge(scope.row)"
              >车队充值</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="车队名称" prop="fleetName">
          <el-input v-model="form.fleetName" placeholder="请输入车队名称" maxlength="30" />
        </el-form-item>

        <el-form-item label="卡类型" prop="cardType">
          <el-checkbox-group v-model="form.cardType">
            <el-checkbox label="储值卡" name="type"></el-checkbox>
            <el-checkbox label="囤油卡" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="车队负责人" prop="fleetLeader">
          <el-input v-model="form.fleetLeader" placeholder="请输入车队负责人姓名" maxlength="30" />
        </el-form-item>

        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="会员等级" prop="gradeId">
          <el-select v-model="form.gradeId" placeholder="请选择会员等级" style="width: 100%">
            <el-option
              v-for="item in userGradeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="油站负责人" prop="staffId">
          <el-select v-model="form.staffId" placeholder="请选择油站负责人" multiple style="width: 100%">
            <el-option
              v-for="item in staffList"
              :key="item.id+''"
              :label="item.realName"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="corporateName">
          <el-input v-model="form.corporateName" placeholder="请输入公司名称" />
        </el-form-item>


        <el-form-item label="用户状态" prop="status">
          <el-switch
            v-model="form.status"
            active-value="qy"
            inactive-value="jy"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>

        <el-form-item label="备注" >
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
        </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="title" :visible.sync="open1" width="30%" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules" label-width="120px">

        <el-form-item label="车队名称" prop="fleetName">
          <el-input v-model="form1.fleetName" placeholder="请输入车队名称" maxlength="50"/>
        </el-form-item>

        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form1.rechargeAmount1" placeholder="请输入充值金额" >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>

        <el-form-item label="赠送金额" prop="giveAmount">
          <el-input v-model="form1.giveAmount1" placeholder="请输入赠送金额，若不赠送请输入0" >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentType">
          <el-select v-model="form1.paymentType" placeholder="请选择支付方式" style="width: 100%">
            <el-option label="现金" value="CASH"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" >
          <el-input v-model="form1.remark" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
        <el-form-item label="操作密码" prop="password">
          <el-input v-model="form1.password" placeholder="请输入操作密码" type="password" />
        </el-form-item>

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

<script>

import {delStaff, queryStaffs} from "@/api/staff/staff";
import {addFleetInfo, deleteFleetInfo, editFleetInfo, getFleetInfo, getFleetInfoList} from "@/api/fleet/fleetinfo";
import {listUserGrade} from "@/api/staff/user/usergrade";

export default {
  dicts: ['zhzt'],
  data() {
    return {
      loading: false,
      open:false,
      open1:false,
      title:'',
      // 日期范围
      dateRange: [],
      beginTime: "",
      endTime: "",
      // 收银台订单列表
      orderList: [],
      deptList: [],
      userGradeList:[],
      staffList:[],
      form:{
        cardType:[],
        status:'qy',
      },
      form1:{},
      options: [{
        value: '选项1',
        label: '黄金糕'
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      // 表单校验
      rules: {
        fleetName: [
          {required: true, message: "车队名称不能为空", trigger: "blur"},
        ],
        cardType: [
          {required: true, message: "卡类型不能为空", trigger: "blur"},
        ],
        fleetLeader: [
          {required: true, message: "车队负责人不能为空", trigger: "blur"}
        ],
        mobile: [
          {required: true, message: "手机号不能为空", trigger: "blur"}
        ],
        gradeId: [
          {required: true, message: "会员等级不能为空", trigger: "blur"}
        ],
        staffId: [
          {required: true, message: "油站负责人不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        rechargeAmount: [
          {required: true, message: "充值金额不能为空", trigger: "blur"}
        ],
        giveAmount: [
          {required: true, message: "赠送金额不能为空", trigger: "blur"}
        ],
        paymentType: [
          {required: true, message: "支付方式不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.getList();
    this.getUserGradeList()
    this.getStaffList()
  },
  methods: {
    // 查询会员等级列表
    getUserGradeList(){
      listUserGrade({page:1,pageSize:1000}).then(res => {
        this.userGradeList = res.data.records
      })
    },
    getStaffList(){
      queryStaffs().then(res => {
        this.staffList = res.data
      })
    },
    getDeptList() {
      // getRunningWaterByTissueApi(this.addDateRange(this.queryParams)).then(response => {
      //   this.deptList = response.data.records;
      // })
    },
    // 获取列表信息
    getList() {
      let dateRange = []
      if (this.beginTime && this.endTime) {
        dateRange.push(this.beginTime.toLocaleDateString())
        dateRange.push(this.endTime.toLocaleDateString())
      }
      getFleetInfoList(this.addDateRange(this.queryParams, dateRange)).then(res => {
        this.orderList = res.data.records
        this.total = res.data.total
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 提交按钮
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.cardType = this.form.cardType.toString()
          this.form.staffId = this.form.staffId.toString()
          if (!this.form.id) {
            addFleetInfo(this.form).then(res => {
              if (res.data==1) {
                this.$message.success("添加成功")
                this.open = false
                this.queryParams.page = 1
                this.getList()
              }else {
                this.$message.error("添加失败，车队名称不可重复")
              }
            })
          } else {
            editFleetInfo(this.form).then(res => {
              if (res.data==1) {
                this.$message.success("修改成功")
                this.open = false
                this.queryParams.page = 1
                this.getList()
              }else {
                this.$message.error("添加失败，车队名称不可重复")
              }
            })
          }
        }
      });
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset();
    },
    // 提交按钮
    submitForm1: function () {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          // if (!this.form1.id) {
            editFleetInfo(this.form1).then(res => {
              this.$message.success("修改成功")
              this.open1 = false
              this.queryParams.page = 1
              this.getList()
            })
          // }
        }
      });
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增车队信息";
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        page: 1,
        pageSize: 10,
      }
      this.beginTime = ''
      this.endTime = ''
      this.handleQuery();
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.reset();
      getFleetInfo(row.id).then(res => {
        this.form = res.data
        this.form.cardType = res.data.cardType.split(",")
        this.form.staffId = res.data.staffId.split(",")
        this.open = true;
        this.title = "修改车队信息";
      })
    },
    // 修改按钮操作
    handleRecharge(row) {
      getFleetInfo(row.id).then(res => {
        this.form1 = res.data
        this.open1 = true;
        this.title = "车队充值";
      })
    },
    // 表单重置
    reset() {
      this.form = {
        cardType:[],
        status:'qy',
      }
      this.resetForm("form");
    },
    // 删除按钮操作
    handleDelete(row) {
      const name = row.fleetName || this.id;
      this.$modal.confirm('是否确认删除"' + name + '"的车队信息？').then(function() {
        return deleteFleetInfo(row.id);
      }).then(() => {
        this.queryParams.page = 1
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
}
</script>

<style scoped>
.box-card {
  width: 100%;
  margin: 0px 20px 20px 20px;
  border: none;
  box-shadow: none;
}

.el-form--inline .el-form-item {
  margin-right: 20px;
}

.table-box {
  width: 100%;
  height: 67.5vh;
  overflow: auto;
}

.wrap-boxs {
  width: 100%;
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.k_ {
  width: 250px;
  height: 75px;
  margin-right: 20px;
  box-sizing: border-box;
  padding: 15px 10px;
  padding-top: 5px;
  color: #333333;
}

.size_ {
  font-weight: 400;
  font-size: 14px;
  color: #333333;
  margin-bottom: 20px;
}

.title_ {
  font-weight: 600;
  font-size: 24px;
  color: #333333;
}

.d-s {
  display: flex;
  align-items: center;
}

.anniu-h {
  width: 80px;
  height: 26px;
  background: #FAFAFA;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #DDDDDD;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #777777;
  margin-right: 20px;
}

.anniu-act {
  background: #FF9655 !important;
  border: 1px solid #FF9655 !important;
  color: #fff;

}

</style>
