<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">    
      <el-form-item label="电话" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>    
      <el-form-item label="角色标示" prop="roleCode">
        <el-input
            v-model="queryParams.roleCode"
            placeholder="请输入角色标示"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>    
      <el-form-item label="所属等级" prop="levelOrder">
        <el-input
            v-model="queryParams.levelOrder"
            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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jx_horizon/jxHorizonPartner/add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jx_horizon/jxHorizonPartner/edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jx_horizon/jxHorizonPartner/delete']"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="jxHorizonPartnerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />      
      <el-table-column label="系统id" align="center" prop="id" />      
      <el-table-column label="系统用户ID" align="center" prop="userId" />      
      <el-table-column label="电话" align="center" prop="phone" />      
      <el-table-column label="角色标示" align="center" prop="roleCode" :formatter="roleCodeFormat" />      
      <el-table-column label="所属等级" align="center" prop="levelOrder" />      
      <el-table-column label="分成比例" align="center" prop="commissionRate" />      
      <el-table-column label="生效时间" align="center" prop="effectiveAt" width="180">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.effectiveAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>      
      <el-table-column label="公司ID" align="center" prop="companyId" />      
      <el-table-column label="合伙人" align="center" prop="parentId" />      
      <el-table-column label="代理人" align="center" prop="agentId" />      
      <el-table-column label="备注" align="center" prop="remark" />      
      <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="['jx_horizon/jxHorizonPartner/edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jx_horizon/jxHorizonPartner/delete']"
          >删除</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="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">      
      <el-form-item label="系统用户ID" prop="userId">
           <el-input v-model="form.userId" placeholder="请输入系统用户ID" />
      </el-form-item>      
      <el-form-item label="电话" prop="phone">
           <el-input v-model="form.phone" placeholder="请输入电话" />
      </el-form-item>      
      <el-form-item label="角色标示" prop="roleCode">
           <el-input v-model="form.roleCode" placeholder="请输入角色标示" />
      </el-form-item>      
      <el-form-item label="所属等级" prop="levelOrder">
           <el-input v-model="form.levelOrder" placeholder="请输入所属等级" />
      </el-form-item>      
      <el-form-item label="分成比例" prop="commissionRate">
           <el-input v-model="form.commissionRate" placeholder="请输入分成比例" />
      </el-form-item>      
       <el-form-item label="生效时间" prop="effectiveAt">
           <el-date-picker clearable size="small" style="width: 200px"
               v-model="form.effectiveAt"
               type="date"
               value-format="yyyy-MM-dd"
               placeholder="选择生效时间">
           </el-date-picker>
       </el-form-item>      
      <el-form-item label="公司ID" prop="companyId">
           <el-input v-model="form.companyId" placeholder="请输入公司ID" />
      </el-form-item>      
      <el-form-item label="合伙人" prop="parentId">
           <el-input v-model="form.parentId" placeholder="请输入合伙人" />
      </el-form-item>      
      <el-form-item label="代理人" prop="agentId">
           <el-input v-model="form.agentId" placeholder="请输入代理人" />
      </el-form-item>      
      <el-form-item label="备注" prop="remark">
           <el-input v-model="form.remark" placeholder="请输入备注" />
      </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 {
    listJxHorizonPartner,
    getJxHorizonPartner,
    delJxHorizonPartner,
    addJxHorizonPartner,
    updateJxHorizonPartner,    
    getUserList,    
} from "@/api/jx_horizon/jxHorizonPartner";
export default {
  components:{    
  },
  name: "JxHorizonPartner",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 合伙人表格数据
      jxHorizonPartnerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,      
      // roleCodeOptions字典数据
      roleCodeOptions: [],      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createdAt: undefined,
        phone: undefined,
        roleCode: undefined,
        levelOrder: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: { 
        createdAt : [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        userId : [
          { required: true, message: "系统用户ID不能为空", trigger: "blur" }
        ],
        phone : [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        roleCode : [
          { required: true, message: "角色标示不能为空", trigger: "blur" }
        ],
        levelOrder : [
          { required: true, message: "所属等级不能为空", trigger: "blur" }
        ],
        commissionRate : [
          { required: true, message: "分成比例不能为空", trigger: "blur" }
        ],
        companyId : [
          { required: true, message: "公司ID不能为空", trigger: "blur" }
        ],
        parentId : [
          { required: true, message: "合伙人不能为空", trigger: "blur" }
        ],
        agentId : [
          { required: true, message: "代理人不能为空", trigger: "blur" }
        ],
        remark : [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {    
    this.getDicts("sys_horizon_partner").then(response => {
      this.roleCodeOptions = response.data.values||[];
    });
    this.getList();
  },
  methods: {    
    /** 查询合伙人列表 */
    getList() {
      this.loading = true;
      listJxHorizonPartner(this.queryParams).then(response => {
        let list = response.data.list || [];        
        this.jxHorizonPartnerList = list;        
        this.total = response.data.total;
        this.loading = false;
      });
    },    
    // 角色标示字典翻译
    roleCodeFormat(row, column) {
      return this.selectDictLabel(this.roleCodeOptions, row.roleCode);
    },    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {        
        createdAt: undefined,        
        updatedAt: undefined,        
        id: undefined,        
        userId: undefined,        
        phone: undefined,        
        roleCode: undefined,        
        levelOrder: undefined,        
        commissionRate: undefined,        
        effectiveAt: undefined,        
        companyId: undefined,        
        parentId: undefined,        
        agentId: undefined,        
        createdBy: undefined,        
        remark: undefined,        
      };      
      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
      getJxHorizonPartner(id).then(response => {
        let data = response.data;        
        this.form = data;
        this.open = true;
        this.title = "修改合伙人";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateJxHorizonPartner(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addJxHorizonPartner(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除合伙人编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delJxHorizonPartner(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>
<style>
</style>