<!--
 * @Author: zhp
 * @Date: 2023-07-14 13:44:46
 * @LastEditTime: 2023-07-18 16:53:52
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <div>
    <el-drawer close-on-press-escape:title="$t('add')" :append-to-body="true" :before-close="handleClose"
      :visible.sync="innerDrawer">
      <el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()"
        label-width="120px">
        <el-form-item prop="requirementListId" :label="$t('customerquality.requirementListName')">
          <el-select clearable v-model="dataForm.requirementListId"
            :placeholder="$t('customerquality.requirementListName')">
            <el-option v-for="item in requirementList" :key="item.id" :label="item.requirementListName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="knowledgeBaseId" :label="$t('customerquality.knowledgeBaseName')">
          <el-select clearable v-model="dataForm.knowledgeBaseId"
            :placeholder="$t('customerquality.knowledgeBaseName')">
            <el-option v-for="item in knowledgeBaseList" :key="item.id" :label="item.knowledgeBaseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="innerDrawer = false">{{ $t('cancel') }} </el-button>
          <el-button type="primary" @click="dataFormSubmit"> {{ $t('confirm') }}</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import i18n from "@/i18n"
import basicAdd from "@/mixins/basic-add"
import debounce from "lodash/debounce"
export default {
  mixins: [basicAdd],
    data() {
      return {
        urlOptions: {
          submitURL: "/customerquality/qmsKnowledgeBaseRequirementList",
          getRequirementListURL: "/customerquality/qmsCustomerQualityRequirementList/page",
          getKnowledgeBaseURL: "/customerquality/qmsKnowledgeBase/page"
        },
        dataForm: {
          knowledgeBaseId: null,
          requirementListId:null
        },
        innerDrawer: false,
        listQuery: {
          limit: 10,
          page:1,
        },
        requirementList: [],
        knowledgeBaseList:[],
      }
  },
  computed: {
    dataRule() {
      return {
        knowledgeBaseId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        requirementListId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ]
      };
    },
  },
  mounted () {
    this.getDict()
  },
    methods: {
      init(obj) {
        this.dataForm.knowledgeBaseId = obj.knowledgeBaseId || ''
        this.dataForm.requirementListId = obj.requirementListId || ''
        this.dataForm.id = obj.id || ''
        this.innerDrawer = true
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields()
          if (this.dataForm.id) {
            this.getInfo()
          } else {
            // this.getCode()
          }
        })
      },
      handleClose() {
        this.innerDrawer = true
        this.$refs.dataForm.resetFields()
      },
      getInfo() {
        this.$http
          .get(`/customerquality/qmsKnowledgeBaseRequirementList/${this.dataForm.id}`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.dataForm = {
              ...this.dataForm,
              ...res.data,
            };
          })
          .catch(() => { });
      },
      getDict() {
        this.$http
          .get(this.urlOptions.getRequirementListURL, {
            params: this.listQuery
          })
          .then(({ data: res }) => {
            if (res.code === 0) {
              console.log(res);
              this.requirementList = res.data.list
            }
          })
          .catch(() => {
          })
        this.$http
          .get(this.urlOptions.getKnowledgeBaseURL, {
            params: this.listQuery
          })
          .then(({ data: res }) => {
            if (res.code === 0) {
              console.log(res);
              this.knowledgeBaseList = res.data.list
            }
          })
          .catch(() => {
          })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](this.urlOptions.submitURL, 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.innerDrawer = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => { });
        });
      },
    },
  }
</script>

<style lang="scss" scoped>

</style>
