<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('table.customerName1')">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('placeholder.customerName')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item :label="$t('table.beginDate')" >
        <el-date-picker clearable
          v-model="queryParams['params[beginDate]']"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('placeholder.beginDate')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('table.endDate')">
        <el-date-picker clearable
          v-model="queryParams['params[endDate]']"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('placeholder.endDate')">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="奖励积分数量" prop="distributionIntegral">
        <el-input
          v-model="queryParams.distributionIntegral"
          placeholder="请输入奖励积分数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="bus公司id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入bus公司id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('btn.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('btn.Reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
	  <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="openJiangLi"
        >{{$t('btn.add2')}}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customerDistributionRecord:customerDistributionRecord: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="['customerDistributionRecord:customerDistributionRecord: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="['customerDistributionRecord:customerDistributionRecord: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="['customerDistributionRecord:customerDistributionRecord:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerDistributionRecordList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
	  <el-table-column :label="$t('table.SerialNumber')" width="50px" align="center">
		<template slot-scope="scope">
			{{ scope.$index + (queryParams.pageNum - 1) * queryParams.pageSize + 1 }}
		</template>
	  </el-table-column>
	  <el-table-column :label="$t('table.customerName1')" align="center" prop="name" />
	  <el-table-column :label="$t('table.username1')" align="center" prop="username" />
	  <el-table-column :label="$t('table.totalNum')" align="center" prop="totalNum" />
      <!-- <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="客户id" align="center" prop="customerId" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="奖励积分数量" align="center" prop="distributionIntegral" />
      <el-table-column label="bus公司id" align="center" prop="deptId" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column :label="$t('table.operation')" 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="['customerDistributionRecord:customerDistributionRecord:edit']"
          >{{$t('table.detail')}}</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customerDistributionRecord:customerDistributionRecord: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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item :label="$t('table.customerName1')" style="width:400px">
          <el-input disabled v-model="form.name"/>
        </el-form-item>
		<el-form-item :label="$t('table.username1')" style="width:400px">
          <el-input disabled v-model="form.username"/>
        </el-form-item>
		<el-form-item :label="$t('dialog.distributionQRcode')">
          	<vue-qr
				:text="'https://baidu.com'"
				:size="150">
			</vue-qr>
        </el-form-item>
		<el-divider content-position="center">{{$t('dialog.customerDistributionRecordTitel3')}}</el-divider>
		<el-table v-loading="loading" :data="form.distributionRecordList">
			<el-table-column :label="$t('table.SerialNumber')" width="50px" align="center">
				<template slot-scope="scope">
					{{ scope.$index +1}}
				</template>
			</el-table-column>
			<!-- <el-table-column :label="$t('table.orderyNo')" align="center" prop="orderNo" />
			<el-table-column :label="$t('table.commodityType')" align="center" prop="typeName" />
			<el-table-column :label="$t('table.commodityName')" align="center" prop="commodityName" /> -->
			<el-table-column :label="$t('table.customerName1')" align="center" prop="customerName" />
	  		<el-table-column :label="$t('table.phone')" align="center" prop="phone" />
			<el-table-column :label="$t('table.orderMoney')" align="center" prop="orderMoney" />
			<el-table-column :label="$t('dialog.updateTime')" align="center" prop="createTime" />
			<el-table-column :label="$t('dialog.distributionIntegral')" align="center" prop="distributionIntegral" />
		</el-table>	
        <!-- <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="奖励积分数量" prop="distributionIntegral">
          <el-input v-model="form.distributionIntegral" placeholder="请输入奖励积分数量" />
        </el-form-item>
        <el-form-item label="bus公司id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入bus公司id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">{{$t('btn.submit')}}</el-button> -->
        <el-button @click="cancel">{{$t('btn.cancel')}}</el-button>
      </div>
    </el-dialog>
	<el-dialog :title="$t('dialog.customerDistributionRecordTitel2')" :visible.sync="open2" width="1000px" append-to-body>
		<el-table v-loading="loading" :data="customerDistributionSetList">
			<el-table-column :label="$t('table.SerialNumber')" width="50px" align="center">
				<template slot-scope="scope">
					{{ scope.$index+1}}
				</template>
			</el-table-column>
			<el-table-column :label="$t('dialog.level')" align="center" prop="orderNo" >
				<template slot-scope="scope">
					<el-input v-model="scope.row.level" :placeholder="$t('placeholder.level')"></el-input>
				</template>
			</el-table-column>
			<el-table-column :label="$t('dialog.proportion')" align="center" prop="typeName" >
				<template slot-scope="scope">
					<el-input v-model="scope.row.proportion" :placeholder="$t('placeholder.proportion')" type="number" min="0"></el-input>
				</template>
			</el-table-column>
			<el-table-column :label="$t('table.operation')" align="center" class-name="small-padding fixed-width">
			  	<template slot-scope="scope">	
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="jiangLiUpdate(scope.row)"
						v-hasPermi="['customerDistributionRecord:customerDistributionRecord:edit']"
					>{{$t('table.edit')}}</el-button>
					<!-- <el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['customerDistributionRecord:customerDistributionRecord:remove']"
					>删除</el-button> -->
					</template>
			</el-table-column>
		</el-table>	
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="open2=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomerDistributionRecord, getCustomerDistributionRecord, delCustomerDistributionRecord, addCustomerDistributionRecord, updateCustomerDistributionRecord,getCustomerDistributionSetList,updateCustomerDistributionSet } from "@/api/customerDistributionRecord/customerDistributionRecord";
import VueQr from 'vue-qr'

export default {
  name: "CustomerDistributionRecord",
  components:{
	VueQr
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户分销信息表格数据
      customerDistributionRecordList: [],
	  // 分销奖励设定列表
	  customerDistributionSetList:[
		{
			proportion:'',
			level:''
		}
	  ],   
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
	  open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
		'params[beginDate]':null,
		'params[endDate]':null,
        // orderId: null,
        // distributionIntegral: null,
        // deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户分销信息列表 */
    getList() {
      this.loading = true;
      listCustomerDistributionRecord(this.queryParams).then(response => {
        this.customerDistributionRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        orderId: null,
        distributionIntegral: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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 = "添加客户分销信息";
    },
	openJiangLi(){
		this.open2 = true
		getCustomerDistributionSetList().then(res=>{
			if(res.code==200){
				if(res.data.length>0){
					this.customerDistributionSetList = res.data
				}
			}
		})
	},
	jiangLiUpdate(row){
		let data = this.customerDistributionSetList[0]
		updateCustomerDistributionSet(data).then(res=>{
			if(res.code==200){
				this.$modal.msgSuccess(this.$t('message.success1'));
				setTimeout(() => {
					this.open2 = false
				}, 800);
			}
		})
	},
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerDistributionRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('dialog.customerDistributionRecordTitel1') ;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomerDistributionRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomerDistributionRecord(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 delCustomerDistributionRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customerDistributionRecord/customerDistributionRecord/export', {
        ...this.queryParams
      }, `customerDistributionRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
