<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
<!--      <el-form-item label="业务员ID" prop="salesmanId">-->
<!--        <el-input-->
<!--          v-model="queryParams.salesmanId"-->
<!--          placeholder="请输入业务员ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="业务员名" prop="salesmanName">
        <el-input
          v-model="queryParams.salesmanName"
          placeholder="请输入业务员名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="金额" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入金额"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="提成" prop="money">-->
<!--        <el-input-->
<!--          v-model="queryParams.money"-->
<!--          placeholder="请输入提成"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="冻结中" value="0" />
          <el-option label="已获得" value="1" />
          <el-option label="已失效" value="2" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="关联订单ID" prop="linkOrderId">-->
<!--        <el-input-->
<!--          v-model="queryParams.linkOrderId"-->
<!--          placeholder="请输入关联订单ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="关联订单号" prop="linkOrderNo">
        <el-input
          v-model="queryParams.linkOrderNo"
          placeholder="请输入关联订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="订单状态：0=待支付,1=待发货,2=待收货,3=待评论,4=已完成,5=退货中,6=退款成功,7=换货中,8=已取消,10-待拼团" prop="orderStatus">-->
<!--        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态：0=待支付,1=待发货,2=待收货,3=待评论,4=已完成,5=退货中,6=退款成功,7=换货中,8=已取消,10-待拼团" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="配送方式：0-快递，1-自提" prop="deliveryType">-->
<!--        <el-select v-model="queryParams.deliveryType" placeholder="请选择配送方式：0-快递，1-自提" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="关联会员ID" prop="linkUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.linkUserId"-->
<!--          placeholder="请输入关联会员ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="关联会员名" prop="linkUserName">
        <el-input
          v-model="queryParams.linkUserName"
          placeholder="请输入关联会员名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="achievementTimeRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="更新时间" prop="updateAt">-->
<!--        <el-input-->
<!--          v-model="queryParams.updateAt"-->
<!--          placeholder="请输入更新时间"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @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"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['mall:mallSalesmanAchievement: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="['mall:mallSalesmanAchievement: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="['mall:mallSalesmanAchievement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['mall:mallSalesmanAchievement:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleBatchUpdate"
          v-hasPermi="['mall:mallSalesmanAchievement:remove']"
        >已发放清零</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallSalesmanAchievementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="业务员业绩主键ID" align="center" prop="achievementId" />-->
<!--      <el-table-column label="业务员ID" align="center" prop="salesmanId" />-->
      <el-table-column label="业务员名" align="center" prop="salesmanName" />
      <el-table-column label="金额" align="center" prop="price" />
      <el-table-column label="提成" align="center" prop="money" >
        <template slot-scope="scope">
          <span v-if="scope.row.isGive!==1">{{scope.row.money}}</span>
          <span v-if="scope.row.isGive===1">{{0}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">冻结中</div>
          <div v-if="scope.row.status == 1">已获得</div>
          <div v-if="scope.row.status == 2">已失效</div>
        </template>
      </el-table-column>

<!--      <el-table-column label="关联订单ID" align="center" prop="linkOrderId" />-->
      <el-table-column label="关联订单号" align="center" prop="linkOrderNo" />
<!--      <el-table-column label="订单状态：0=待支付,1=待发货,2=待收货,3=待评论,4=已完成,5=退货中,6=退款成功,7=换货中,8=已取消,10-待拼团" align="center" prop="orderStatus" />-->
<!--      <el-table-column label="配送方式：0-快递，1-自提" align="center" prop="deliveryType" />-->
<!--      <el-table-column label="关联会员ID" align="center" prop="linkUserId" />-->
      <el-table-column label="关联会员名" align="center" prop="linkUserName" />
      <el-table-column label="创建时间" align="center" prop="createAt" width="160" />
<!--      <el-table-column label="更新时间" align="center" prop="updateAt" />-->
      <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="['mall:mallSalesmanAchievement:edit']"
            v-if="scope.row.isGive!==1 && scope.row.status ===1"
          >已发放清零</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallSalesmanAchievement: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  :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row >
          <el-col style="text-align: center">
            <span style="align-content: center;font-size: 18px">是否清零</span>
          </el-col>
        </el-row>
      </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 { listMallSalesmanAchievement, getMallSalesmanAchievement, delMallSalesmanAchievement, addMallSalesmanAchievement, updateMallSalesmanAchievement, exportMallSalesmanAchievement } from "@/api/mall/mallSalesmanAchievement";

export default {
  name: "MallSalesmanAchievement",
  data() {
    return {
      // 业绩时间范围
      achievementTimeRange:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务员业绩表格数据
      mallSalesmanAchievementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        salesmanId: null,
        salesmanName: null,
        price: null,
        money: null,
        status: null,
        linkOrderId: null,
        linkOrderNo: null,
        orderStatus: null,
        deliveryType: null,
        linkUserId: null,
        linkUserName: null,
        createAt: null,
        updateAt: null,
        params:{
          beginAchievementTime:'',
          endAchievementTime:''
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      updateType: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询业务员业绩列表 */
    getList() {
      this.loading = true;
      if (null != this.achievementTimeRange && '' != this.achievementTimeRange) {
        this.queryParams.params["beginAchievementTime"] = this.achievementTimeRange[0];
        this.queryParams.params["endAchievementTime"] = this.achievementTimeRange[1];
      }
      listMallSalesmanAchievement(this.queryParams).then(response => {
        this.mallSalesmanAchievementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        achievementId: null,
        salesmanId: null,
        salesmanName: null,
        price: null,
        money: null,
        status: 0,
        linkOrderId: null,
        linkOrderNo: null,
        orderStatus: "0",
        deliveryType: null,
        linkUserId: null,
        linkUserName: null,
        createAt: null,
        updateAt: null,
        delFlag: null,
        isGive: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.achievementTimeRange = []
      this.queryParams.params["beginAchievementTime"] = ''
      this.queryParams.params["endAchievementTime"] = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.achievementId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业务员业绩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.updateType = 0;
      this.form = {
        achievementId: null,
        isGive: 1
      };
      const achievementId = row.achievementId
      this.form.achievementId = achievementId
      this.open = true;
      this.title = "是否清零";

    },
    handleBatchUpdate(){
      this.updateType = 1;
      this.form = {
        achievementIdList: this.ids,
        isGive: 1
      }
      this.open = true;
      this.title = "是否清零";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.updateType === 1){
            if (!this.form.achievementIdList){
              this.msgWarning("请选择清已发放零的数据");
              return;
            }
            updateMallSalesmanAchievement(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }else {
            if (this.form.achievementId != null) {
              this.form.isGive = 1;
              updateMallSalesmanAchievement(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMallSalesmanAchievement(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const achievementIds = row.achievementId || this.ids;
      this.$confirm('是否确认删除业务员业绩编号为"' + achievementIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMallSalesmanAchievement(achievementIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有业务员业绩数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMallSalesmanAchievement(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
