<template>
  <el-form
    :model="serviceReturnForm"
    ref="serviceReturnForm"
    label-width="100px"
  >
    <table id="serviceReturnTable">
      <colgroup>
        <col width="6%" align="center" valign="middle"/>
        <col width="20%" align="center" valign="middle"/>
        <col width="20%" align="center" valign="middle"/>
        <col width="20%" align="center" valign="middle"/>
        <col width="20%" align="center" valign="middle"/>
        <col width="14%" align="center" valign="middle"/>
      </colgroup>
      <tr>
        <th>序号</th>
        <th>参数字段</th>
        <th>参数名称</th>
        <th>参数类型</th>
        <th>参数备注</th>
        <th>操作</th>
      </tr>
      <tr v-for="(param, index) in serviceReturnForm.params" :key="index">
        <td>
          {{ index + 1 }}
        </td>
        <td>
          <el-form-item
            :prop="'params.' + index + '.paramCode'"
            :rules="{
              required: true,
              message: '参数字段不能为空',
              trigger: 'blur',
            }"
          >
            <el-input v-model="param.paramCode"></el-input>
          </el-form-item>
        </td>
        <td>
          <el-form-item
            :prop="'params.' + index + '.paramName'"
            :rules="{
              required: true,
              message: '参数名称不能为空',
              trigger: 'blur',
            }"
          >
            <el-input v-model="param.paramName"></el-input>
          </el-form-item>
        </td>
        <td>
          <el-form-item
            :prop="'params.' + index + '.paramType'"
            :rules="{
              required: true,
              message: '参数类型不能为空',
              trigger: 'blur',
            }"
          >
            <el-select v-model="param.paramType" placeholder="请选择">
              <el-option
                v-for="item in paramTypes"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </td>
        <td>
          <el-form-item>
            <el-input v-model="param.paramNote"></el-input>
          </el-form-item>
        </td>
        <td>
          <el-button @click.prevent="removeReturn(param)"> 删除</el-button>
        </td>
      </tr>
    </table>
    <el-row type="flex" justify="center">
      <!-- <el-button
        icon="ArrowLeft"
        @click="forwardStep"
        circle
      ></el-button> -->
      <el-button @click="addReturn">新增参数</el-button>
      <!-- <el-button type="primary" @click="submitForm('serviceReturnForm')">
        保存
      </el-button>
      <el-button @click="exit">退出</el-button>
      <el-button
        icon="ArrowRight"
        @click="nextStep"
        circle
      ></el-button> -->
    </el-row>
  </el-form>
</template>

<script>
import {
  getServiceAllReturn,
  addServiceReturn,
  delServiceReturn,
} from "@/api/service/return";

export default {
  props: ["srvId"],
  data() {
    return {
      serviceReturnForm: {
        params: [
          {
            srvId: this.srvId,
            paramCode: "",
            paramName: "",
            paramType: "string",
            paramNote: "",
          },
        ],
      },
      paramTypes: ["string", "int", "double", "date"],
    };
  },
  methods: {
    // 前进一步
    forwardStep() {
      this.$emit("forwardStep");
    },
    // 后退一步
    nextStep() {
      this.$emit("nextStep");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addServiceReturn(this.serviceReturnForm.params).then((r) => {
            this.$message({
              message: "添加参数成功",
              type: "success",
            });
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeReturn(item) {
      if (item.paramCode) {
        this.$confirm("此操作将永久删除该参数, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delServiceReturn({
            srvId: item.srvId,
            paramCode: item.paramCode,
          }).then(() => {
            var index = this.serviceReturnForm.params.indexOf(item);
            if (index !== -1) {
              this.serviceReturnForm.params.splice(index, 1);
            }
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        });
      } else {
        var index = this.serviceReturnForm.params.indexOf(item);
        if (index !== -1) {
          this.serviceReturnForm.params.splice(index, 1);
        }
      }
    },
    addReturn() {
      this.serviceReturnForm.params.push({
        srvId: this.srvId,
        paramCode: "",
        paramName: "",
        paramType: "string",
        paramNote: "",
      });
    },
    async getReturnList(id) {
      if (id) {
        await getServiceAllReturn(id).then((r) => {
          if (r.data && r.data.length > 0) {
            this.serviceReturnForm.params = r.data;
          } else {
            this.serviceReturnForm.params = [
              {
                srvId: this.srvId,
                paramCode: "",
                paramName: "",
                paramType: "string",
                paramNote: "",
              },
            ];
          }
        });
      }
    },
    // 退出
    exit() {
      this.$nextTick(() => {
        this.$router.replace({
          query: {isClose: "true"},
        });
      });
    },
  },
  watch: {
    srvId: {
      handler(val) {
        this.getReturnList(val);
      },
      immediate: true,
    },
  },
};
</script>
<style scoped>
#serviceReturnTable {
  width: 100%;
  margin: 0 auto;
  margin-bottom: 20px;
  border-radius: 5px;
  border: 1px solid #dcdfe6;
  border-collapse: collapse;
}

#serviceReturnTable th {
  text-align: center;
  vertical-align: middle;
  border-collapse: collapse;
  padding: 5px;
  background-color: #f6f6f6;
}

#serviceReturnTable td {
  text-align: center;
  vertical-align: middle;
  border-top: 1px solid #dcdfe6;
  border-bottom: 1px solid #dcdfe6;
  border-collapse: collapse;
  padding: 5px;
}

#serviceReturnTable td ::v-deep .el-form-item {
  margin: 0;
}
</style>
