<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-input
          v-model="queryParams.customerType"
          placeholder="请选择客户类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户所在地区" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入客户所在地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计周期" prop="statisticPeriod">
        <el-date-picker clearable
          v-model="queryParams.statisticPeriod"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择统计周期">
        </el-date-picker>
      </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="['md:kehutongji: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="['md:kehutongji: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="['md:kehutongji: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="['md:kehutongji:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kehutongjiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="统计周期" align="center" prop="id" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="客户类型" align="center" prop="customerType" />
      <el-table-column label="联系人姓名" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="客户所在地区" align="center" prop="region" />
      <el-table-column label="总销售额" align="center" prop="totalSales" />
      <el-table-column label="总销售量" align="center" prop="totalQuantity" />
      <el-table-column label="平均单价" align="center" prop="averageUnitPrice" />
      <el-table-column label="最大订单金额" align="center" prop="largestOrder" />
      <el-table-column label="平均订单金额" align="center" prop="averageOrderValue" />
      <el-table-column label="直销销售额" align="center" prop="directSales" />
      <el-table-column label="代理商销售额" align="center" prop="agentSales" />
      <el-table-column label="电商平台销售额" align="center" prop="onlineSales" />
      <el-table-column label="气体产品销售额" align="center" prop="gasSales" />
      <el-table-column label="设备销售额" align="center" prop="equipmentSales" />
      <el-table-column label="服务销售额" align="center" prop="serviceSales" />
      <el-table-column label="首次下单日期" align="center" prop="firstOrderDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.firstOrderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近下单日期" align="center" prop="lastOrderDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastOrderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="累计订单数" align="center" prop="totalOrders" />
      <el-table-column label="复购率" align="center" prop="repeatPurchaseRate" />
      <el-table-column label="累计支付金额" align="center" prop="totalPayments" />
      <el-table-column label="累计退款金额" align="center" prop="totalRefunds" />
      <el-table-column label="未结清金额" align="center" prop="outstandingBalance" />
      <el-table-column label="统计周期" align="center" prop="statisticPeriod" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statisticPeriod, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="['md:kehutongji:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:kehutongji: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户类型" prop="customerType">
          <el-input v-model="form.customerType" placeholder="请选择客户类型" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="客户所在地区" prop="region">
          <el-input v-model="form.region" placeholder="请输入客户所在地区" />
        </el-form-item>
        <el-form-item label="总销售额" prop="totalSales">
          <el-input v-model="form.totalSales" placeholder="请输入总销售额" />
        </el-form-item>
        <el-form-item label="总销售量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入总销售量" />
        </el-form-item>
        <el-form-item label="平均单价" prop="averageUnitPrice">
          <el-input v-model="form.averageUnitPrice" placeholder="请输入平均单价" />
        </el-form-item>
        <el-form-item label="最大订单金额" prop="largestOrder">
          <el-input v-model="form.largestOrder" placeholder="请输入最大订单金额" />
        </el-form-item>
        <el-form-item label="平均订单金额" prop="averageOrderValue">
          <el-input v-model="form.averageOrderValue" placeholder="请输入平均订单金额" />
        </el-form-item>
        <el-form-item label="直销销售额" prop="directSales">
          <el-input v-model="form.directSales" placeholder="请输入直销销售额" />
        </el-form-item>
        <el-form-item label="代理商销售额" prop="agentSales">
          <el-input v-model="form.agentSales" placeholder="请输入代理商销售额" />
        </el-form-item>
        <el-form-item label="电商平台销售额" prop="onlineSales">
          <el-input v-model="form.onlineSales" placeholder="请输入电商平台销售额" />
        </el-form-item>
        <el-form-item label="气体产品销售额" prop="gasSales">
          <el-input v-model="form.gasSales" placeholder="请输入气体产品销售额" />
        </el-form-item>
        <el-form-item label="设备销售额" prop="equipmentSales">
          <el-input v-model="form.equipmentSales" placeholder="请输入设备销售额" />
        </el-form-item>
        <el-form-item label="服务销售额" prop="serviceSales">
          <el-input v-model="form.serviceSales" placeholder="请输入服务销售额" />
        </el-form-item>
        <el-form-item label="首次下单日期" prop="firstOrderDate">
          <el-date-picker clearable
            v-model="form.firstOrderDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择首次下单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最近下单日期" prop="lastOrderDate">
          <el-date-picker clearable
            v-model="form.lastOrderDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最近下单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="累计订单数" prop="totalOrders">
          <el-input v-model="form.totalOrders" placeholder="请输入累计订单数" />
        </el-form-item>
        <el-form-item label="复购率" prop="repeatPurchaseRate">
          <el-input v-model="form.repeatPurchaseRate" placeholder="请输入复购率" />
        </el-form-item>
        <el-form-item label="累计支付金额" prop="totalPayments">
          <el-input v-model="form.totalPayments" placeholder="请输入累计支付金额" />
        </el-form-item>
        <el-form-item label="累计退款金额" prop="totalRefunds">
          <el-input v-model="form.totalRefunds" placeholder="请输入累计退款金额" />
        </el-form-item>
        <el-form-item label="未结清金额" prop="outstandingBalance">
          <el-input v-model="form.outstandingBalance" placeholder="请输入未结清金额" />
        </el-form-item>
        <el-form-item label="统计周期" prop="statisticPeriod">
          <el-date-picker clearable
            v-model="form.statisticPeriod"
            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>
  </div>
</template>

<script>
import { listKehutongji, getKehutongji, delKehutongji, addKehutongji, updateKehutongji } from "@/api/md/kehutongji";

export default {
  name: "Kehutongji",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户统计报表格数据
      kehutongjiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,        customerName: null,        customerType: null,        contactName: null,        contactPhone: null,        region: null,        totalSales: null,        totalQuantity: null,        averageUnitPrice: null,        largestOrder: null,        averageOrderValue: null,        directSales: null,        agentSales: null,        onlineSales: null,        gasSales: null,        equipmentSales: null,        serviceSales: null,        firstOrderDate: null,        lastOrderDate: null,        totalOrders: null,        repeatPurchaseRate: null,        totalPayments: null,        totalRefunds: null,        outstandingBalance: null,        statisticPeriod: null      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "客户ID不能为空", trigger: "blur" }
        ],        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],        customerType: [
          { required: true, message: "客户类型不能为空", trigger: "change" }
        ],        totalSales: [
          { required: true, message: "总销售额不能为空", trigger: "blur" }
        ],        totalQuantity: [
          { required: true, message: "总销售量不能为空", trigger: "blur" }
        ],        statisticPeriod: [
          { required: true, message: "统计周期不能为空", trigger: "blur" }
        ]      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户统计报列表 */
    getList() {
      this.loading = true;
      listKehutongji(this.queryParams).then(response => {
        this.kehutongjiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        customerId: null,        customerName: null,        customerType: null,        contactName: null,        contactPhone: null,        region: null,        totalSales: null,        totalQuantity: null,        averageUnitPrice: null,        largestOrder: null,        averageOrderValue: null,        directSales: null,        agentSales: null,        onlineSales: null,        gasSales: null,        equipmentSales: null,        serviceSales: null,        firstOrderDate: null,        lastOrderDate: null,        totalOrders: null,        repeatPurchaseRate: null,        totalPayments: null,        totalRefunds: null,        outstandingBalance: null,        statisticPeriod: 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
      getKehutongji(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) {
            updateKehutongji(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKehutongji(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户统计报编号为"' + ids + '"的数据项？').then(function() {
        return delKehutongji(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/kehutongji/export', {
        ...this.queryParams
      }, `kehutongji_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
