<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加配置项目
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column label="配置key" width="180" prop="configKey" />
      <el-table-column label="配置名称" width="180" prop="configName" />
      <el-table-column label="参数值1" width="170" :show-overflow-tooltip="true" prop="configValue1" />
      <el-table-column label="参数值2" width="170" :show-overflow-tooltip="true" prop="configValue2" />
      <el-table-column label="参数值3" width="170" :show-overflow-tooltip="true" prop="configValue3" />
      <el-table-column label="配置类型" width="170" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          <span v-if="row.configType == 0">
            系统内置模块
          </span>
          <span v-else-if="row.configType ==1">
            业务内置模块
          </span>
          <span v-else-if="row.configType == 2">
            业务可修改模块
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="所属模块" prop="configModule" />
      <el-table-column label="操作" width='150' fixed="left" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button v-if="row.configType !=0" @click="deleteConfig(row)" type="danger" size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getConfigList" />

    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogVisible" width="80%">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="配置key" prop="dictName">
          <el-input v-model="form.configKey" placeholder="请输入配置key" />
        </el-form-item>
        <el-form-item label="配置名称" prop="dictType">
          <el-input v-model="form.configName" placeholder="请输入配置名称" />
        </el-form-item>
        <el-form-item label="参数值1" prop="dictType">
          <el-input v-model="form.configValue1" placeholder="请输入参数值1" />
        </el-form-item>
        <el-form-item label="参数值2" prop="dictType">
          <el-input v-model="form.configValue2" placeholder="请输入参数值2" />
        </el-form-item>
        <el-form-item label="参数值3" prop="dictType">
          <el-input v-model="form.configValue3" placeholder="请输入参数值3" />
        </el-form-item>
        <el-form-item label="配置类型" prop="dictType">
          <el-select v-model="form.configType" placeholder="请选择">
            <el-option
              v-for="item in configOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属模块" prop="dictType">
          <el-input v-model="form.configModule" placeholder="请输入所属模块" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { listConfig, addConfig, getConfigInfo, updateConfig } from '@/api/system/config.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'

const form = {
  configKey: undefined,
  configName: undefined,
  configValue1: undefined,
  configValue2: undefined,
  configValue3: undefined,
  configType: 0,
  configModule: undefined,
  remark: undefined
}

export default {
  components: {
    Cancledialog, Pagination
  },
  data() {
    return {
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,

      innerVisible: false,

      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),

      confirmdialog: false,
      listLoading: false,
      tableData: [],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },

      configOptions: [
        {
          value: 0,
          label: '系统内置模块'
        },
        {
          value: 1,
          label: '业务内置模块'
        },
        {
          value: 2,
          label: '业务可修改模块'
        }
      ]

    }
  },
  mounted() {
    this.getConfigList()
  },
  methods: {
    // 获取列表
    getConfigList() {
      const _that = this
      _that.listLoading = true
      const params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      const data = {
        'params': JSON.stringify(params)
        // "page": JSON.stringify(page),
      }
      // console.log(data);
      listConfig(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        // _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    // 增加字典管理
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)
      _that.dialogTitle = '增加字典管理'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 增加档案
    addBtn() {
      const _that = this
      const info = JSON.stringify(_that.form)
      const data = {
        'config': info
      }
      // console.log(data);
      // return

      addConfig(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this

      _that.dialogTitle = '修改字典管理'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      const idPrint = row.configKey
      const data = {
        configKey: idPrint
      }
      // console.log(data);
      getConfigInfo(data).then(res => {
        console.log(res)
        const data = res.data
        _that.form = {
          configKey: data.configKey,
          configName: data.configName,
          configValue1: data.configValue1,
          configValue2: data.configValue2,
          configValue3: data.configValue3,
          configType: data.configType,
          configModule: data.configModule,
          remark: data.remark
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改档案
    editBtn() {
      const _that = this
      const info = JSON.stringify(_that.form)
      const data = {
        'config': info
      }
      // console.log(data);
      // return;

      updateConfig(data).then(res => {
        console.log(res)
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getConfigList()
    },

    // 启用禁用
    deleteConfig(row) {
      const _that = this
      const printerTypeInfo = {
        configKey: row.configKey
      }
      const params = {
        'config': JSON.stringify(printerTypeInfo)
      }

      const url = '/system/removeConfigValue'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getConfigList()
    }

  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
  .tableItemInput{
    width: 30%;
  }
</style>
