
var permDelete = $("#perm-delete").val();//删除权限
var permEdit = $("#perm-edit").val();//编辑权限
/**
 * 用户信息
 */
$(function () {
  var service = $("#contextPath").val();
  var page = null;
  var proSetId = 0; //名额ID
  var id = ''; //服务ID
  var extraServiceId = ''; //洗车服务id
  var extraName = '洗车'; //洗车服务名称
  var orgId = $("#orgId").val();
  // 数据获取
  var api = {
    set_data_original: function () {
      $('#content').val('');
      $('#amount').val('');
      $('#serviceName').val('');
      $('.alert').css('display', 'none');
      id = '';
    },
    del_service_data: function (id) { //删除服务
      $.ajax({
        type: "post",
        url: service + "/admin/washcar/deleteExtraService",
        data: {
          id: id
        },
        success: function (data) {
          var data = data;
          // console.log(data, 222222222);
          if (data.code == '200') {
            layer.msg(data.msg);
            api.get_info_list();
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_add_edit_project: function (params) { //保存、修改服务
      $.ajax({
        type: "post",
        url: service + "/admin/washcar/saveOrUpdateExtraService",
        data: params,
        success: function (data) {
          var data = data;
          // console.log(data, 222222222);
          if (data.code == '200') {
            layer.msg(data.msg);
            api.set_data_original();
            api.get_info_list();
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_info_list: function () { //设置初始数据获取
      api.get_user_info_list();
      $('input[name="number"]').val('');
      $('input[name="extraPrice"]').val('');
      $.ajax({
        type: "post",
        url: service + "/admin/washcar/getExtraServiceList",
        data: {
          projectId: $("#orgId").val()
        },
        success: function (data) {
          var data = data;
          // console.log(data, 222222222);
          if (data.code == '200') {
            proSetId = data.data.id;
            extraServiceId = data.data.extraServiceId;
            extraName = data.data.extraName;
            $('input[name="number"]').val(data.data.quota);
            $('input[name="extraPrice"]').val(data.data.extraPrice);
            api.get_user_info_list(data.data.exList);
          } else {
            layer.msg(data.msg);

          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_user_info_list: function (data) { //初始页面渲染
      layui.use('table', function () {
        var table = layui.table;
        table.render({
          elem: '#table_user_info',
          skin: 'nob', //行边框风格
          even: true, //开启隔行背景
          size: 'lg', //小尺寸的表格
          cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          cols: [
            [{
              field: 'paixu',
              width: 96,
              title: '序号',
              templet: '#paixu',
            }, {
              field: 'serviceName',
              width: 164,
              title: '服务名称'
            }, {
              field: 'content',
              width: 174,
              title: '服务内容',
            }, {
              field: 'amount',
              width: 174,
              title: '价格配置'
            }, {
              field: 'manage',
              width: 181,
              fixed: 'right',
              title: '操作管理',
              templet: function (data) {
                var str = "<div class='operate'>";

                if (permEdit == 1){
                  str += "<span class='op_edit_btn' lay-event='edit'><i class='edit'></i><i class='op_hover'>编辑</i></span>";
                }
                if (permDelete == 1){
                  str += "<span class='op_del_btn' lay-event='del'><i class='delete'></i><i class='op_hover'>删除</i></span>";
                }
                str += "</div>";
                return str;
              }
            }]
          ],
          data: data,
          id: '',
          response: {
            statusName: 'code', //数据状态的字段名称，默认：code             
            statusCode: 200, //成功的状 态码，默认：0        
            countName: "count", //数据总数的字段名称，默认：count            
            dataName: "data" //数据列表的字段名称，默认：data
          },
          done: function (res, curr, count) {
            $(".edit,.delete").mouseover(function () {
              $(this).next().show();
            })
            $(".edit,.delete").mouseout(function () {
              $(this).next().hide();
            })
          }
        });
        table.on('tool(user)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
          console.log(obj.event); //点击事件
          if (obj.event === 'edit') {
            $('.alert').css('display', 'block');
            $('.alert .alert_con>h2').text('编辑服务项');
            id = obj.data.id;
            proSetId = parseInt(obj.data.proSetId);
            $('#content').val(obj.data.content),
              $('#amount').val(obj.data.amount),
              $('#serviceName').val(obj.data.serviceName)
          } else if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
              api.del_service_data(obj.data.id);
              obj.del();
              layer.close(index);
            });
          }
          console.log(obj.data); //所在行的所有相关数据  
        });
      });
    },
    save_service_data: function (orgId, quota, id) { //保存、修改名额
      $.ajax({
        type: "post",
        url: service + "/admin/washcar/saveOrUpdateQuota",
        data: {
          id: id,
          quota: quota,
          orgId: orgId
        },
        success: function (data) {
          // console.log(data, 222222222);
          if (data.code == '200') {
            layer.msg(data.msg)
            proSetId = data.data.id;
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    }
  }
  api.get_info_list();

  // 监听表单选值
  $("#orgId").change(function(){

    api.get_info_list();
  })

  // 事件处理
  $('.btn1').click(function () {
    $('.alert').css('display', 'block');
    $('.alert .alert_con>h2').text('新增服务项');
  })
  $('.saveBtn').click(function () {
    let params = {
      id: id,
      proSetId: proSetId,
      content: $('#content').val(),
      amount: $('#amount').val(),
      serviceName: $('#serviceName').val(),
      isOther: "1"
    };
    api.get_add_edit_project(params);
  })
  $('.cancelBtn').click(function () {
    api.set_data_original();
    id = '';
  })
  $('#addService').click(function () {
    let quota = $('input[name="number"]').val();
    let orgId = $('#orgId').val();
    api.save_service_data(orgId, quota, proSetId);
  })
  $('#priceCertain').click(function () {
    let params = {
      id: extraServiceId,
      proSetId: proSetId,
      content: '洗车服务',
      amount: $('input[name="extraPrice"]').val(),
      serviceName: extraName,
      isOther: "2"
    };
    api.get_add_edit_project(params);
  })
})