<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="租户名称" prop="tenantId">
        <ChainSelect v-model="queryParams.tenantId" v-bind="tenantProps"></ChainSelect>
      </el-form-item>
      <el-form-item label="系统名称" prop="sysName">
        <el-input v-model="queryParams.sysName" placeholder="请输入系统名称" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--
      <el-form-item label="是否限制用户数" prop="isUserAstrict">
        <el-select v-model="queryParams.isUserAstrict" @change="handleQuery" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否限制在线数" prop="isOnLine">
        <el-select v-model="queryParams.isOnLine" @change="handleQuery" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否定义系统名称" prop="isDefinitionName">
        <el-select v-model="queryParams.isDefinitionName" @change="handleQuery" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否定义系统logo" prop="isDefinitionLogo">
        <el-select v-model="queryParams.isDefinitionLogo" @change="handleQuery" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否定义系统色调" prop="isDefinitionHue">
        <el-select v-model="queryParams.isDefinitionHue" @change="handleQuery" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['tenant:config: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="['tenant:config: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="['tenant:config: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="['tenant:config:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户ID" align="center" prop="tenantId" />
      <el-table-column label="是否限制用户数" align="center" prop="isUserAstrict">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isUserAstrict" />
        </template>
      </el-table-column>
      <el-table-column label="是否限制在线数" align="center" prop="isOnLine">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isOnLine" />
        </template>
      </el-table-column>
      <el-table-column label="是否定义系统名称" align="center" prop="isDefinitionName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDefinitionName" />
        </template>
      </el-table-column>
      <el-table-column label="是否定义系统logo" align="center" prop="isDefinitionLogo">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDefinitionLogo" />
        </template>
      </el-table-column>
      <el-table-column label="是否定义系统色调" align="center" prop="isDefinitionHue">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDefinitionHue" />
        </template>
      </el-table-column>
      <el-table-column label="系统名称" align="center" prop="sysName" />
      <el-table-column label="系统色调" align="center" prop="sysHue" />
      <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="['tenant:config:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['tenant:config:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination hide-on-single-page :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改租户配置对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="选择租户" prop="tenantId">
          <ChainSelect v-model="form.tenantId" v-bind="tenantProps"></ChainSelect>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startData">
              <el-date-picker clearable v-model="form.startData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="stopData">
              <el-date-picker clearable v-model="form.stopData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否限制用户数" prop="isUserAstrict">
              <el-select v-model="form.isUserAstrict" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户数" prop="userQuantity">
              <el-input v-model.number="form.userQuantity" min="0" placeholder="请输入用户数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否限制在线数" prop="isOnLine">
              <el-select v-model="form.isOnLine" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="在线数" prop="onLineUserQuantity">
              <el-input v-model.number="form.onLineUserQuantity" min="0" placeholder="请输入在线数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否定义系统色调" prop="isDefinitionHue">
              <el-select v-model="form.isDefinitionHue" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统色调" prop="sysHue">
              <el-color-picker v-model="form.sysHue"></el-color-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否定义系统名称" prop="isDefinitionName">
              <el-select v-model="form.isDefinitionName" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统名称" prop="sysName">
              <el-input v-model="form.sysName" placeholder="请输入系统名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否定义系统logo" prop="isDefinitionLogo">
              <el-select v-model="form.isDefinitionLogo" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统log" prop="sysLog">
              <userAvatar :is-logo="true" :logo="form.sysLog" @uploadLogoEnd="uploadLogoEnd" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首页类型" prop="homeType">
              <el-input v-model="form.homeType" placeholder="请输入首页类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

      </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 { pageListConfig, getConfig, delConfig, addConfig, updateConfig, uploadLogo } from '@/api/system/tenant/config'
import {listTenant, tenantAllList, tenantList} from '@/api/system/tenant';
import userAvatar from '../../user/profile/userAvatar';
import ChainSelect from '@/components/Chain/SelectTable/ChainSelect';

export default {
  name: 'Config',
  components: {
    userAvatar,
    ChainSelect
  },
  dicts: ['sys_yes_no'],
  data() {
    const checkNumber = (rule, value, callback) => {
      if (!value && value !== 0) {
        return callback(new Error('请输入'));
      }
      if (!Number.isInteger(value)) {
        callback(new Error('请输入数字值'));
      } else {
        if (value < 0) {
          callback(new Error('必须大于等于0'));
        } else {
          callback();
        }
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户配置表格数据
      configList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      tenantProps: {
        data: [],
        tableColumn: [
          {
            field: 'name',
            title: '租户名',
            width: 100,
            align: 'center'
          },
        ],
        valueField: 'id',
        labelField: 'name',
        trigger: true,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startData: null,
        stopData: null,
        isUserAstrict: null,
        userQuantity: null,
        isOnLine: null,
        onLineUserQuantity: null,
        isDefinitionName: null,
        sysName: null,
        isDefinitionLogo: null,
        sysLog: null,
        isDefinitionHue: null,
        customizationFields: null,
        remark: null
      },
      // 表单参数
      form: {
        // 默认颜色
        sysHue: '#409EFF',
      },
      // 表单校验
      rules: {
        startData: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        stopData: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        isUserAstrict: [
          { required: true, message: '是否限制用户数不能为空', trigger: 'blur' }
        ],
        userQuantity: [
          { validator: checkNumber, trigger: 'blur' }
        ],
        isOnLine: [
          { required: true, message: '是否限制在线数不能为空', trigger: 'blur' }
        ],
        onLineUserQuantity: [
          { validator: checkNumber, trigger: 'blur' }
        ],
        isDefinitionName: [
          { required: true, message: '是否定义系统名称不能为空', trigger: 'blur' }
        ],
        sysName: [
          { required: true, message: '系统名称不能为空', trigger: 'blur' }
        ],
        isDefinitionLogo: [
          { required: true, message: '是否定义系统logo不能为空', trigger: 'blur' }
        ],
        // sysLog: [
        //   { required: true, message: '系统log不能为空', trigger: 'blur' }
        // ],
        isDefinitionHue: [
          { required: true, message: '是否定义系统色调不能为空', trigger: 'blur' }
        ],
        sysHue: [
          { required: true, message: '系统色调不能为空', trigger: 'blur' }
        ],
        customizationFields: [
          { required: true, message: '定制化数据', trigger: 'blur' }
        ],
        // remark: [
        //   { required: true, message: '备注不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  created() {
    this.getList();

    listTenant().then(res => {
      if (res.data) {
        this.tenantProps.data = res.data;
      }

    })
  },
  methods: {
    uploadLogoEnd(url) {
      this.form.sysLog = url
    },
    /** 查询租户配置列表 */
    getList() {
      this.loading = true
      pageListConfig(this.queryParams).then(response => {
        this.configList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        startData: null,
        stopData: null,
        isUserAstrict: null,
        userQuantity: null,
        isOnLine: null,
        onLineUserQuantity: null,
        isDefinitionName: null,
        sysName: null,
        isDefinitionLogo: null,
        sysLog: null,
        isDefinitionHue: null,
        sysHue: '#409EFF',
        customizationFields: 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.names = 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
      getConfig(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改租户配置'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      const names = row.id || this.names
      this.$modal.confirm('是否确认删除租户配置为"' + names + '"的数据项？').then(function () {
        return delConfig(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tenant/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
