<!--参数设置-->
<template>
  <div class="app-container parameterSetting">
    <div class="page-container parameterSettingPage">
      <div class="page-form-title-box">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="" prop="configName">
            <el-input
              v-model="queryParams.configName"
              placeholder="参数名称"
              clearable
              size="small"
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item label="">
            <el-date-picker
              :clearable="false"
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="创建时间"
              end-placeholder="创建时间"
            ></el-date-picker>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="handleAdd"
             v-hasPermiRouter="['system:conf:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="参数名称" align="left" prop="configName" :show-overflow-tooltip="true" />
          <el-table-column label="参数键名" align="left" prop="configKey" :show-overflow-tooltip="true" />
          <el-table-column label="参数键值" align="left" prop="configValue" :show-overflow-tooltip="true"/>
          <el-table-column label="备注" align="left" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="left" prop="createTime" width="200">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="handleUpdate(scope.row)"
                   v-hasPermiRouter="['system:conf:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="handleDelete(scope.row)"
                   v-hasPermiRouter="['system:conf:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    layout="total, prev, pager, next, sizes, jumper"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="参数名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数键名" prop="configKey">
          <el-input v-model="form.configKey" :disabled="!!form.id" placeholder="请输入参数键名" />
        </el-form-item>
        <el-form-item label="参数键值" prop="configValue">
          <el-input v-model="form.configValue" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" maxLength="50"
                    resize="none" placeholder="请输入备注内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="cancel">取消</p>
        <p class="dialog-footer-button-primary" @click="submitForm">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getConfigList } from "@/api/commonality/permission";
  import { addOrUpdate,systemConfigInfoDelete } from "@/api/systemManagement/index";
  export default {
    name: 'parameterSetting',
    data(){
      return{
        tableButtonType:this.hasPermiDom(['system:conf:edit','system:conf:detail','system:conf:del']),
        typeOptions:[{}],
        loading:false,
        // 查询参数
        dateRange:[],
        queryParams: {
          page: 1,
          pageSize:20,
          category:1,
          configType: 0,
          configName:'',
        },
        dataList:[],
        total:0,
        //dialog参数
        open:false,
        title:'',
        form:{},
        // 表单校验
        rules: {
          configName: [
            { required: true, message: "请输入参数名称", trigger: "blur" },
            { required: true, message: "请输入参数名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          configKey: [
            { required: true, message: "请输入参数键名", trigger: "blur" },
            { required: true, message: "请输入参数键名", validator: this.spaceJudgment, trigger: "blur" }
          ],
          configValue: [
            { required: true, message: "请输入参数键值", trigger: "blur" },
            { required: true, message: "请输入参数键值", validator: this.spaceJudgment, trigger: "blur" }
          ]
        }
      }
    },
    created(){

    },
    mounted(){
      this.getList();
    },
    methods:{
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.$set(this,'form',{
          configName:'',
          configKey:'',
          configValue:'',
          remark:'',
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.$set(this,'queryParams',{
          page: 1,
          pageSize:20,
          category:1,
          configType: 0,
          configName:'',
        });
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加参数";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.$set(this,'form',{
          id:row.id,
          configName:row.configName,
          configKey:row.configKey,
          configValue:row.configValue,
          remark:row.remark,
        });
        this.open = true;
        this.title = "修改参数";
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let obj = JSON.parse(JSON.stringify(this.form))
            if (obj.id) {
              obj.category = 1;
              addOrUpdate(obj).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              obj.category = 1;
              addOrUpdate(obj).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除参数?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return systemConfigInfoDelete({id:row.id});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      //获取数据列表
      getList(){
        this.loading = true;
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.startTime = this.dateRange[0]+'T00:00:00'
          obj.endTime = this.dateRange[1]+'T23:59:59'
        }else{
          obj.startTime = "";
          obj.endTime = "";
        }
        getConfigList(obj).then(response => {
          this.dataList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
    },
  }
</script>

<style scoped lang="scss">
  .parameterSetting{
    .parameterSettingPage{

    }
  }
</style>
