<template>
  <div class="app-container">
    <el-card style="margin-bottom: 20px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="活动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" clearable placeholder="请选择活动状态">
          <el-option label="启用" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否在线" prop="isonline">
        <el-select v-model="queryParams.isonline" clearable placeholder="请选择是否在线">
          <el-option label="在线" value="0"></el-option>
          <el-option label="下线" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>

      </el-form-item>
    </el-form>
    </el-card>

    <el-card style="margin-bottom: 20px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
      </el-row>

    <el-table v-loading="loading" :data="activeFullminusList"  @selection-change="handleSelectionChange">
      <el-table-column
        type="index"
        align="center"
        label="排序"
      >
      </el-table-column>
      <el-table-column label="活动编号" align="center" prop="id" />
      <el-table-column label="活动名称" align="center" prop="name" />
      <el-table-column label="会员等级" align="center" prop="dieselUserLevel" >
        <template slot-scope="scope">
          <span v-for="(item,index) in scope.row.dieselUserLevel "> {{item}} </span>
        </template>
      </el-table-column>


      <el-table-column label="活动状态 " align="center" prop="status" >
        <template slot-scope="scope">
          <span style="cursor: pointer;" @click="qiyong(scope.row)" v-if="scope.row.status == 0"> <el-tag>启用</el-tag > </span>
          <span style="cursor: pointer;" @click="jinyong(scope.row)" v-if="scope.row.status == 1"> <el-tag type="danger">禁用</el-tag>  </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)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeletexia(scope.row)"
            v-if="scope.row.isonline == 0"
          >下线</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(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-card>

    <!-- 添加或修改满减营销对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-position="labelPosition" label-width="110px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="满减规则" prop="activeDiscountChildList">
          <div v-for="(item,index) in form.activeDiscountChildList" :key="index" style="width: 100%; display: flex;align-items: center; margin-bottom: 10px  ">
            <div style="width: 25%;display: flex; align-items: center;margin-right: 20px " >
              <span>满</span> <el-input-number v-model="item.amount"  :min="0" :max="9999" label="0"></el-input-number>   <span>元</span>
            </div>
            <div  style="width: 25%;display: flex; align-items: center;">
              <span>减</span> <el-input-number v-model="item.deductionAmount"  :min="0" :max="9999" label="0"></el-input-number> <span>元</span>

            </div>
            <div @click="deleteactiveDiscountChildList(index)">
              <el-button type="danger" icon="el-icon-delete" circle style="margin-left: 8px"  ></el-button>
            </div>

          </div>
          <div>
            <el-button type="primary" icon="el-icon-plus"  @click="addactiveDiscountChildList">新增</el-button>
          </div>

        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="活动开始时间" prop="activeStartTime">
              <el-date-picker clearable
                              v-model="form.activeStartTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择活动开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动结束时间" prop="activeEndTime">
              <el-date-picker clearable
                              v-model="form.activeEndTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择活动结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>

        </el-row>




        <el-form-item label="可用油品" prop="adaptOil" >
          <el-checkbox-group v-model="form.adaptOil">
            <el-checkbox v-for="(item,index) in oillist" :label="item.id" :key="index">{{item.oilType}}{{item.oilName}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="会员等级" prop="dieselUserLevel" >
          <el-select v-model="form.dieselUserLevel"  multiple clearable placeholder="会员等级">
            <el-option :label="item.name" :value="item.id.toString()" v-for="(item,index) in userGradeList" :key="index" > </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与条件" prop="participationCondition">
          <el-select v-model="form.participationCondition" clearable placeholder="请选择天参与条件">
            <el-option label="不限制" value="0"></el-option>
            <el-option label="优惠订单不参与" value="1"></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="参与次数类别" prop="participationAcount">
          <el-select v-model="form.participationAcount" clearable placeholder="请选择参与次数类别">
            <el-option label="不限制" value="0"></el-option>
            <el-option label="限制" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="限制次数" prop="limitAcount" v-if="form.participationAcount ==1">
          <el-input-number v-model="form.limitAcount" placeholder="请输入限制次数" />
        </el-form-item>
<!--        <el-form-item label="是否上线" prop="isonline">-->
<!--          <el-select v-model="form.isonline" clearable placeholder="请选择参与次数类别">-->
<!--            <el-option label="上线" value="0"></el-option>-->
<!--            <el-option label="下线" value="1"></el-option>-->
<!--          </el-select>-->
<!--        </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 { listActiveFullminus, getActiveFullminus, delActiveFullminus, addActiveFullminus, updateActiveFullminus } from "@/api/EventMarketing/activeFullminus";
import {getActiveDiscount, updateActiveDiscount} from "@/api/EventMarketing/activeDiscount";
import {getoilName} from "@/api/EventMarketing/activeConsumption";
import {listUserGrade} from "@/api/staff/user/usergrade";

export default {
  name: "ActiveFullminus",
  data() {
    return {
      labelPosition:'left',
      oillist:['92#','95#','98#','0#','-10#','LNG','CNG','京92#','京95#','京0#'],
      userGradeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 满减营销表格数据
      activeFullminusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chainStorId: null,
        storeId: null,
        name: null,
        activeStartTime: null,
        activeEndTime: null,
        adaptOil: [],
        adaptUserType: null,
        memberType: null,
        dieselUserLevel: null,
        gasolineUserLevel: null,
        naturalUserLevel: null,
        paymentType: null,
        participationCondition: null,
        participationAcount: null,
        limitAcount: null,
        status: null,
        isonline: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        activeStartTime:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        activeEndTime:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        adaptOil:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        dieselUserLevel:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        gasolineUserLevel:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        naturalUserLevel:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        participationCondition:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        participationAcount:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        isonline:[
          { required: true, message: '不能为空', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getoilName()
    this.listUserGrade()
  },
  methods: {
    /** 查询满减营销列表 */
    getList() {
      this.loading = true;
      listActiveFullminus(this.queryParams).then(response => {
        this.activeFullminusList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getoilName(){
      getoilName().then(res=>{
        console.log(res)
        this.oillist = res.data

      })
    },
    listUserGrade(){
      listUserGrade().then(res=>{
        if(res.code == 200){
          this.userGradeList = res.data.records
        }

      })
    },
    // 新增
    addactiveDiscountChildList(){
      let data = {
        amount :this.form.activeDiscountChildList.discount,
        deductionAmount:this.form.activeDiscountChildList.amount,

      }
      this.form.activeDiscountChildList.push(data)
    },
    deleteactiveDiscountChildList(index){
      this.form.activeDiscountChildList.splice(index, 1)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chainStorId: null,
        storeId: null,
        name: null,
        activeStartTime: null,
        activeEndTime: null,
        adaptOil: [],
        adaptUserType: null,
        memberType: null,
        dieselUserLevel: [],
        gasolineUserLevel: [],
        naturalUserLevel: [],
        paymentType: null,
        participationCondition: null,
        participationAcount: null,
        limitAcount: null,
        status: 0,
        isonline: 0,
        activeDiscountChildList: [],
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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
      getActiveFullminus(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改满减营销";
      });
    },
    handleDeletexia(row){
      getActiveFullminus(row.id).then(response => {
        // this.form = response.data;
        let data = response.data
        data.isonline = 1
        updateActiveFullminus(data).then(response => {
          this.$modal.msgSuccess("下线成功");
          this.open = false;
          this.getList();
        });

      });
    },
    qiyong(row){
      if(row.isonline == 1){
        this.$message.error("下线不可进行操作")
        return
      }
      getActiveFullminus(row.id).then(response => {
        // this.form = response.data;
        let data = response.data
        data.status = 1
        updateActiveFullminus(data).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        });

      });
    },
    jinyong(row){
      if(row.isonline == 1){
        this.$message.error("下线不可进行操作")
        return
      }
      getActiveFullminus(row.id).then(response => {
        // this.form = response.data;
        let data = response.data
        data.status = 0
        updateActiveFullminus(data).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        });

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActiveFullminus(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActiveFullminus(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 delActiveFullminus(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/marketingActivity/activeFullminus/export', {
        ...this.queryParams
      }, `activeFullminus_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
