layui.define(['table','form'],function(exports){
    var table = layui.table;
    var form = layui.form;
    renderSelectOptions = function (data, settings) {
        settings = settings || {};
        var valueField = settings.valueField || 'value';
        var textField = settings.textField || 'text';
        var selectedValue = settings.selectedValue || "";
        var html = [];

        for (var i = 0; i < data.length; i++) {
            var item = data[i];

            html.push('<option value="');
            html.push(item[valueField]);
            html.push('"');
            if(selectedValue && item[valueField] == selectedValue ){
                html.push(' selected="selected"');
            }
            html.push('>');
            html.push(item[textField]);
            html.push('</option>');
        }

        return html.join('');
    };

    getSelectedRow = function (id) {
        var checkStatus = table.checkStatus(id);
        var data = checkStatus.data;

        if (data.length == 0) {
            layer.msg('请选择一条记录');
            return false;
        }
        if (data.length > 1) {
            layer.msg('只能选择一条记录');
            return false;
        }
        return data[0];
    };
    isHasPermissions = function (perm) {
        var permissions = layui.data(layui.setter.tableName)['permissions'];
        if (!perm) {
            return true;
        }

        var perms = perm.split('|');
        var is_has = false;

        for (var i in perms) {
            if (permissions.indexOf(perms[i]) != -1) {
                is_has = true;
                break;
            }
        }

        return is_has;
    };

    form.verify({
        intGeZ: function (value, item) {
            value = value.trim();
            if (value === '') {
                return;
            }
            if (!(/^[0-9]\d*$/i.test(value))){
                return '请输入整数';
            }
            value = parseInt(value);
            if (isNaN(value)){
                return '请输入整数';
            }
            if (value <= 0){
                return '该整数必须大于0';
            }
        },
        intGtz: function (value, item) {
            value = value.trim();
            if (value === '') {
                return;
            }
            if (!(/^[0-9]\d*$/i.test(value))){
                return '请输入整数';
            }
            value = parseInt(value);
            if (isNaN(value)){
                return '请输入整数';
            }
            if (value < 0){
                return '该整数必须大于等于0';
            }
        },
        numberGtZ: function (value, item) {
            value = value.trim();
            if (value === '') {
                return;
            }
            if (!(/^([0-9]+(\.\d+)?|0\.\d+)$/i.test(value) || /^[1-9]\d*$/i.test(value))){
                return '请输入数字';
            }
            value = parseFloat(value);
            if (isNaN(value)){
                return '请输入数字';
            }
            if (value <= 0){
                return '该数字必须大于0';
            }
        },
        numberGeZ: function (value, item) {
            value = value.trim();
            if (value === '') {
                return;
            }
            if (!(/^([0-9]+(\.\d+)?|0\.\d+)$/i.test(value) || /^[1-9]\d*$/i.test(value))){
                return '请输入数字';
            }
            value = parseFloat(value);
            if (isNaN(value)){
                return '请输入数字';
            }
            if (value < 0){
                return '该数字必须大于等于0';
            }
        },
        tel: function (value, item) {
            value = value.trim();
            if (value === '') {
                return;
            }
            if(!(/^0?(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9]|166)[0-9]{8}$/i.test(value) || /^([0-9]{3,4}-)?[0-9]{7,8}$/i.test(value))){
                return '请输入正确的手机或者固话号码'
            }
        }
    });

  exports(
      'utils', {
        renderSelectOptions:renderSelectOptions,
        getSelectedRow:getSelectedRow,
        isHasPermissions:isHasPermissions
      }
  );
});