<template>
  <el-dialog
    :visible.sync="visible"
    title="详情"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="75%"
  >

    <el-dialog
      :visible.sync="visible2"
      title="详情"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="10%"
      append-to-body
    >
      <el-button @click="click2">111</el-button>
    </el-dialog>
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'"
    >
      <el-row :gutter="30">
        <el-col :span="8">
          <el-form-item
            label="案件名称"
            prop="caseName"
          >
            <el-input
              v-model="dataForm.caseName"
              placeholder="案件名称"
              readonly
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="用户"
            prop="tempName"
          >
            <el-input
              v-model="dataForm.userName"
              placeholder="用户"
              readonly
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item
            label="角色"
            prop="userName"
          >
            <el-input
              v-model="dataForm.role"
              placeholder="角色"
              readonly
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <!--  <el-form-item
        label="问题"
        prop="questionName"
      >
        <el-input
          v-model="dataForm.questionName"
          placeholder="问题"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item
        label="答案"
        prop="answer"
        readonly
      >
        <el-input
          v-model="dataForm.answer"
          placeholder="答案"
        ></el-input>
      </el-form-item> -->
      <el-form-item
        label="问题及答案"
        prop="userName"
      >
        <el-table
          :data="currentTableData"
          style="width: 100%"
          :header-row-style="{height:'20px'}"
          :row-style="{height:'20px'}"
          size="mini"
        >
          <el-table-column
            type="index"
            label="序号"
            width="50"
          >
          </el-table-column>
          <el-table-column
            prop="questionName"
            label="问题"
          >
          </el-table-column>
          <el-table-column
            prop="answer"
            label="答案"
          >
          </el-table-column>

        </el-table>
        <el-pagination
          layout="prev, pager, next"
          :total="list.length"
          :page-size="10"
          :current-page.sync="currentPage"
          @current-change='handleCurrentChange'
        >
        </el-pagination>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <!--  <el-button @click="visible2 = true">111</el-button> -->
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <!--  <el-button
        type="primary"
        @click="dataFormSubmitHandle()"
      >{{ $t('confirm') }}</el-button> -->
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      currentPage: 1,
      visible2: false,
      currentTableData: [],

      dataForm: {
        id: "",
        createDate: "",
        userId: "",
        tempId: "",
        questionId: "",
        answerId: "",
        caseId: "",
        userId: "",
        role: "",
      },
      headerRowStyle: {
        color: "red",
        background: "red",
      },
      list: [],
    };
  },
  computed: {
    dataRule() {
      return {
        userId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        tempId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        questionId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        answerId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  created() {},
  methods: {
    click2() {
      this.$message({
        type: "success",
        message: "车接口接0",
        onClose: () => {
          this.visible2 = false;
        },
      });
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        // console.log("sdjkfjkdhghf", this.dataForm);
        if (this.dataForm.caseId) {
          this.getInfo();
        }
      });
    },
    handleCurrentChange() {
      console.log(this.currentPage);
      this.currentTableData = this.list.slice(
        (this.currentPage - 1) * 10,
        this.currentPage * 10
      );
    },

    // 获取信息
    getInfo() {
      this.$http
        .get(
          /* `/fhjw/fhjwcomment/${this.dataForm.caseId}` */ "/fhjw/fhjwcomment/getAnswer?caseId=" +
            this.dataForm.caseId +
            "&userId=" +
            this.dataForm.userId
        )
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.list = this.dataForm.questionList;
          this.currentTableData = this.list.slice(0, 10);
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/fhjw/fhjwcomment/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>

