<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:repo:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="companyList"
      row-key="companyId"
    >
      <el-table-column type="index" label="ID" align="center" :show-overflow-tooltip="true" width="80"
      ></el-table-column>
      <el-table-column prop="companyName" label="名称" align="center" :show-overflow-tooltip="true" width="160"
      ></el-table-column>
      <el-table-column prop="updateBy" label="更新人" align="center" :show-overflow-tooltip="true" width="160"
      ></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" align="center" :show-overflow-tooltip="true" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true" width="500"
      ></el-table-column>
      <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="handleConfig(scope.row)">配置</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDel(scope.row)">删除</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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="企业名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入企业名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="可输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-divider>配置模板</el-divider>
          </el-col>
        </el-row>
        <el-row v-for="(category, index) in Object.keys(mapData)" :key="category">
          <el-col :span="24">
            <el-form-item :label="category">
              <el-select v-model="form.templateIds[index]">
                <el-option
                  v-for="item in mapData[category]"
                  :key="item.repoId"
                  :label="item.repoName"
                  :value="item.repoId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

  </div>
</template>


<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import FileUpload from '@/components/FileUpload'
import { addCompany, delCompany, getInfo, getTemplate, listCompany, updateCompany } from '@/api/system/company'

export default {
  name: 'Repo',
  dicts: [],
  components: { FileUpload },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      companyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 判断按钮
      type: 0,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined
      },
      // 表单参数
      form: {},
      // 企业配置
      mapData: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      listCompany(this.queryParams).then(response => {
        this.companyList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        companyId: undefined,
        companyName: undefined,
        remark: undefined,
        templateIds: []

      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加菜单'
      this.type = 0
    },
    /** 删除按钮操作 */
    handleDel(row) {
      this.$modal.confirm('是否确认删除：' + row.repoName).then(function() {
        return delCompany(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 提交按钮 */
    submit: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type === 0) {
            addCompany(this.form).then(response => {
              this.$message('新增成功')
              this.open = false
              this.getList()
            })
          } else if (this.type === 1) {
            updateCompany(this.form).then(response => {
              this.$message('修改成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 企业配置 */
    handleConfig(row) {
      this.reset()
      getTemplate().then(response => {
        this.mapData = response.data
      })
      getInfo(row.id).then(response => {
        this.form = response.data
        const categoriesLength = Object.keys(this.mapData).length;
        if (this.form.templateIds === null || this.form.templateIds.length === 0) {
          this.form.templateIds = Array(categoriesLength).fill(0);
        } else if (this.form.templateIds.length < categoriesLength) {
          const diff = categoriesLength - this.form.templateIds.length;
          this.form.templateIds = this.form.templateIds.concat(Array(diff).fill(0));
        }
        this.form.templateIds.forEach((item, index) => {
          if (item === 0) {
            this.form.templateIds[index] = undefined
          }
        })
      })
      this.open = true
      this.title = '企业配置'
      this.type = 1
    },

  }
}
</script>








