<template>
  <div>
    <el-card>
      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="字典名称" prop="parmName"></el-table-column>
        <el-table-column label="字段" prop="parmKey"></el-table-column>
        <el-table-column label="供应商选入必填" prop="supSelect">

          <template slot-scope="scope">
            <el-select v-model="scope.row.supSelect">
              <el-option label="是" :value=true></el-option>
              <el-option label="否" :value=false></el-option>
            </el-select>
          </template>

        </el-table-column>
        <el-table-column label="供应商新增必填" prop="supAdd">
          <template slot-scope="scope">
            <el-select v-model="scope.row.supAdd">
              <el-option label="是" :value=true></el-option>
              <el-option label="否" :value=false></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="供应商编辑必填" prop="localAdd">
          <template slot-scope="scope">
            <el-select v-model="scope.row.localEdit">
              <el-option label="是" :value=true></el-option>
              <el-option label="否" :value=false></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="说明" prop="remark"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleSave(scope.$index, scope.row)"
            >保存
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="query.limit"
        :page.sync="query.page"
        @pagination="handleCurrentChange"
      ></pagination>
    </el-card>

    <el-card>

      <el-table v-loading="systemLoading" :data="systemList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="参数名" prop="paramName" width="320"></el-table-column>
        <!--        <el-table-column label="参数键" prop="paramKey" width="200"></el-table-column>-->
        <el-table-column label="标题名称" prop="paramValue" width="100"></el-table-column>
        <el-table-column label="描述" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ scope.row.paramExplain }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native="handleForm(scope.$index, scope.row)"
            >设置
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--表单-->
      <el-dialog
        :title="formMap[formName]"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :before-close="hideForm"
        width="45%"
        top="5vh"
      >
        <el-form :model="formData" :rules="formRules" ref="dataForm">
          <el-row>
            <el-form-item label="标题:" prop="paramName" label-width="100px">
              <el-input
                v-model="formData.paramName"
                auto-complete="off"
                :disabled="true"
                style="width: 90%"
              ></el-input>
            </el-form-item>

          </el-row>

          <el-row>
            <el-form-item label="参数键:" prop="paramKey" label-width="100px">
              <el-input
                v-model="formData.paramKey"
                auto-complete="off" style="width: 90%"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="参数名称" prop="paramValue" label-width="100px">
              <el-input type="" v-model="formData.paramValue" auto-complete="off" style="width: 90%"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="描述:" label-width="100px">
              <el-input
                type="textarea"
                v-model="formData.paramExplain" style="width: 90%"
              ></el-input>
            </el-form-item>
          </el-row>

          <!--<el-form-item label="参数状态" prop="paramStatus">-->
          <!--<el-radio-group v-model="formData.paramStatus">-->
          <!--<el-radio :label="0">禁用</el-radio>-->
          <!--<el-radio :label="1">正常</el-radio>-->
          <!--</el-radio-group>-->
          <!--</el-form-item>-->

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="hideForm">取消</el-button>
          <el-button type="primary" @click.native="formSubmit()" :loading="formLoading"
          >提交
          </el-button>
        </div>
      </el-dialog>
    </el-card>

  </div>
</template>

<script>
import {
  filterList,
  modifySetup
} from "@/api/basic/basicProductConfig";
import {systemParamConfigList, systemParamConfigSave} from '@/api/system/param/systemParamConfig'

const formJson = {
  id: "",
  parentId: "",
  paramName: "",
  paramKey: "",
  paramValue: "",
  paramStatus: 1,
  paramType: 1,
  paramExplain: "",
};

export default {
  data() {
    return {
      query: {
        page: 1,
        limit: 20
      },
      systemQuery: {
        page: 1,
        limit: 20,
        paramType: 2,
        paramStatus: 1,
      },
      formRules: {
        paramValue: [{required: true, message: "请输入参数值", trigger: "blur"}],
        // ,
        // paramStatus: [
        //     {required: true, message: "请选择参数状态", trigger: "change"}
        // ]
      },
      list: [],
      formLoading: false,
      systemList: [],
      total: 0,
      formData: formJson,
      formName: null,
      systemTotal: 0,
      loading: true,
      systemLoading: true,
      formVisible: false,
      enableMap: {
        true: "是",
        false: "否",
      },
      formMap: {
        add: "新增运行参数",
        update: "运行参数设置",
      },
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: ""
      });
      this.query = {
        page: 1,
        limit: 20
      };
      this.getList();
    },
    onSubmit() {
      this.getList();
    },
    handleCurrentChange(val) {
      this.query.page = val.page;
      this.getList();
    },
    getList() {
      this.loading = true;
      filterList(this.query)
        .then(response => {
          this.loading = false;
          this.list = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    // 显示表单
    handleForm(index, row) {
      this.formVisible = true;
      this.formData = JSON.parse(JSON.stringify(formJson));
      if (row !== null) {
        this.formData = Object.assign({}, row);
      }
      this.formName = "add";
      if (index !== null) {
        this.index = index;
        this.formName = "update";
      }
    },
    hideForm() {
      // 更改值
      this.formVisible = !this.formVisible;
      return true;
    },
    getSystemList() {
      this.systemLoading = true;
      systemParamConfigList(this.systemQuery)
        .then((response) => {
          this.systemLoading = false;
          this.systemList = response.data.list || [];
          this.systemTotal = response.data.total || 0;
        })
        .catch(() => {
          this.systemLoading = false;
          this.systemList = [];
          this.systemTotal = 0;
        });
    },
    formSubmit() {
      // if (!Number.isInteger(parseInt(this.formData.paramValue))) {
      //     this.$message.warning("参数值必须为数字");
      //     return;
      // }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.formLoading = true;
          let data = Object.assign({}, this.formData);
          data.paramValue = data.paramValue.trim();
          systemParamConfigSave(data, this.formName)
            .then((response) => {
              this.formLoading = false;
              if (response.code != 20000) {
                this.$message.error(response.message);
                return false;
              }
              this.$message.success("操作成功");
              this.formVisible = false;
              if (this.formName === "add") {
                // 向头部添加数据
                if (response.data && response.data.id) {
                  data.id = response.data.id;
                  this.list.unshift(data);
                }
              } else {
                this.list.splice(this.index, 1, data);
              }
              // 刷新表单
              this.resetForm();
              this.getList();
              this.getSystemList();
            })
            .catch(() => {
              this.formLoading = false;
            });
        }
      });
    },
    handleSave(index, row) {
      this.currentCheckIndex = -1;
      modifySetup(row).then((res) => {
        if (res.code == 20000) {
          this.getList();
          this.$message.success("保存成功！");
        } else {
          this.$message.error(res.message);
        }
      })
    },
  },
  filters: {},
  mounted() {
  },
  created() {
    // 加载表格数据
    this.getList();
    this.getSystemList();
  }
};
</script>

<style type="text/scss" lang="scss">
</style>
