<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="车牌号" prop="carNumber">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="会员等级" prop="memberGrade">
        <el-input
          v-model="queryParams.memberGrade"
          placeholder="请输入会员等级"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="专车类型" prop="specialCarType">
        <el-select v-model="queryParams.specialCarType" placeholder="请选择专车类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="油品" prop="oilName">
        <el-select
          v-model="queryParams.oilName"
          placeholder="请选择油品"
          clearable
          size="small">
          <el-option
            v-for="dict in oilNameOptions"
            :key="dict.dictLabel"
            :label="dict.dictLabel"
            :value="dict.dictLabel"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="客户表id" align="center" prop="id" v-if="false" /> -->
      <el-table-column label="会员id" align="center" prop="memberId" />
      <el-table-column label="" align="center" prop="unionId" v-if="false"/>
      <el-table-column label="客户姓名" align="center" prop="customerName" />
      <!-- <el-table-column label="推荐人" align="center" prop="commendMan" /> -->
      <el-table-column label="会员等级" align="center" prop="grade" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="车牌号" align="center" prop="carNumber" v-if="false"/>
      <el-table-column label="油品" align="center" prop="oilName" />
      <el-table-column label="汽油余额" align="center" prop="qyAmt" />
      <el-table-column label="柴油余额" align="center" prop="cyAmt" />
      <el-table-column label="积分" align="center" prop="integral" />
      <el-table-column label="专车类型" align="center" prop="specialCarType" v-if="false"/>
      <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)"
          >编辑</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="lookinfo(scope.row)"
          >订单查询</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员号" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员id" />
        </el-form-item>
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="推荐人" prop="commendMan">
          <el-input v-model="form.commendMan" placeholder="请输入推荐人" />
        </el-form-item>
        <el-form-item label="会员等级" prop="memberGrade">
          <el-input v-model="form.memberGrade" placeholder="请输入会员等级" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="油品" prop="oilName">
          <el-select v-model="form.oilName" multiple placeholder="请选择">
            <el-option
              v-for="dict in oilNameOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入余额" />
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="专车类型" prop="specialCarType">
          <el-select v-model="form.specialCarType" placeholder="请选择专车类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="注册时间" prop="regtime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.regtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注册时间">
          </el-date-picker>
        </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="titleOrderInfo" :visible.sync="openOrderInfo" width="700px" append-to-body>
      <el-table v-loading="loading" :data="orderList" >
        <el-table-column v-if="false" label="订单id" align="center" prop="orderId" />
        <el-table-column label="订单号"  width="200" align="center" prop="orderNo" />
        <el-table-column label="油品名称" align="center" prop="oilName" />
        <el-table-column label="油品价格" align="center" prop="oilPirce" />
        <el-table-column label="加油升数" align="center" prop="orderLiters" />
        <el-table-column label="金额" align="center" prop="amt" />
        <el-table-column label="消费者" width="120" align="center" prop="consumer" />
        <el-table-column label="加油员" width="120" align="center" prop="oilPersonnel" />
        <el-table-column label="加油枪号" align="center" prop="oilGun" />
        <el-table-column label="支付类型" align="center" prop="payType"  v-if="false"  :formatter="payTypeFotmat"/>
        <el-table-column label="油站名称" width="120" align="center" v-if="false" prop="stationName" />
        <el-table-column v-if="false" label="用户id(消费者)" align="center" prop="consumerId" />
        <el-table-column v-if="false" label="油站id" align="center" prop="stationId" />
        <el-table-column label="状态" align="center" prop="status" :formatter="statusFotmat"/>
        <el-table-column label="支付方式" align="center" prop="payWay" :formatter="payWayFotmat"/>
        <el-table-column label="支付时间" align="center" prop="payDate" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createdDate" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="订单类型" align="center" prop="orderType" v-if="false" />
      </el-table>
      <pagination
        v-show="totalOrder>0"
        :total="totalOrder"
        :page.sync="queryOrder.pageNum"
        :limit.sync="queryOrder.pageSize"
        @pagination="listPage"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listManage, getManage, delManage, addManage, updateManage, exportManage,getOrderInfo } from "@/api/customer/manage";
import {stationinfo} from "@/api/station/gun";

export default {
  name: "Manage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalOrder: 0,
      // 客户管理表格数据
      manageList: [],
      //油站信息
      stationOptions:[],
      orderList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openOrderInfo: false,
      titleOrderInfo:"",
      oilNameOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        customerName: null,
        commendMan: null,
        memberGrade: null,
        phoneNumber: null,
        carNumber: null,
        oilName: null,
        balance: null,
        integral: null,
        specialCarType: null,
        regtime: null,
        stationId: null,
        stationName: null
      },
      queryOrder:{
        pageNum: 1,
        pageSize: 10,
        oilName: null,
        stationId: null,
        customerName: null
      },
      // 表单参数
      form: {},
      query:{
        deptId:null
      },
      row: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.query.deptId =this.$store.selectDeptId;
    if(this.query.deptId ==null || this.query.deptId ==""){
      this.query.deptId  =this.$store.state.user.deptId;
    }
    stationinfo(this.query).then(response => {
      this.stationOptions = response.rows;
    });
    this.getDicts("oil_name").then(response => {
      this.oilNameOptions = response.data;
    });
  },
  methods: {
    payTypeFotmat(row, column){
      if(row.payType === 'sxf'){
        return '随行付'
      }
    },
    payWayFotmat(row, column){
      if(row.payWay === '02'){
        return '公众号'
      }else if(row.payWay === '03'){
        return '小程序'
      }
    },
    statusFotmat(row, column){
      if(row.status === '0'){
        return '未支付'
      }else if(row.status === '1'){
        return '已支付'
      }
    },
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.stationId= this.$store.selectDeptId;
      if(this.queryParams.stationId==null || this.queryParams.stationId==""){
        this.queryParams.stationId =this.$store.state.user.deptId;
      }
      listManage(this.queryParams).then(response => {
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    onInstitutionChang(e){
      let obj = {};
      obj = this.stationOptions.find((item)=>{//这里的userList就是上面遍历的数据源
        return item.deptId === e;//筛选出匹配数据
      })
      this.form.stationName=obj.deptName;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        customerName: null,
        commendMan: null,
        memberGrade: null,
        phoneNumber: null,
        carNumber: null,
        oilName: null,
        balance: null,
        integral: null,
        specialCarType: null,
        regtime: null,
        stationId: null,
        stationName: 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
      getManage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    lookinfo(row){
      this.row =row;
      row.page= this.queryOrder.pageNum;
      row.limit= this.queryOrder.pageSize;
      this.listPage(row);
    },
    listPage(row){
      this.queryOrder.stationId=this.$store.selectDeptId;
      this.queryOrder.consumer=row.customerName;
      this.queryOrder.pageNum=row.page;
      this.queryOrder.pageSize =row.limit;
      getOrderInfo(this.queryOrder).then(response => {
        this.orderList = response.rows;
        this.openOrderInfo = true;
        this.totalOrder = response.total;
        this.titleOrderInfo = "查看客户订单信息";
        this.loading = false;

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManage(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManage(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delManage(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportManage(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },

  }
};
</script>
