<!--
 * @Author: zhp
 * @Date: 2023-07-14 13:44:46
 * @LastEditTime: 2023-07-18 16:53:36
 * @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="knowledgeBaseFileId" :label="$t('customerquality.knowledgeBaseFileId')">
          <el-select clearable v-model="dataForm.knowledgeBaseFileId"
            :placeholder="$t('customerquality.knowledgeBaseFileId')">
            <el-option v-for="item in categoryList" :key="item.id" :label="item.dataDictionaryDetailName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="title" :label="$t('customerquality.title')">
          <el-input clearable v-model="dataForm.title" :placeholder="$t('customerquality.title')">
          </el-input>
        </el-form-item>
        <el-form-item prop="accessPath" :label="$t('customerquality.accessPath')">
          <el-input clearable v-model="dataForm.accessPath" :placeholder="$t('customerquality.accessPath')">
          </el-input>
        </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/knowledgeBaseRequirementListData",
          getDictDataDetail: '/mutual/qmsDataDictionaryDetail/page'
        },
        dataForm: {
          accessPath:null,
          id: null,
          knowledgeBaseFileId: null,
          knowledgeBaseRequirementListId: null,
          title: null,
        },
        innerDrawer: false,
        listQuery: {
          limit: 10,
          page:1,
        },
        categoryList:[],
        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()
    this.getAccessPath()
  },
    methods: {
      init(obj) {
        this.dataForm.knowledgeBaseRequirementListId = obj.knowledgeBaseRequirementListId || ''
        this.dataForm.knowledgeBaseRequirementListId = obj.knowledgeBaseRequirementListId || ''

        this.dataForm.id = obj.id || ''
        this.innerDrawer = true
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields()
          if (this.dataForm.id) {
            this.getInfo()
          } else {
            // this.getCode()
          }
        })
      },
      getDict() {
        this.$http
          .get(this.urlOptions.getDictDataDetail, {
            params: {
              limit: 999,
              page: 1,
              dataDictionaryId: '1679670942635122690',
            }
          })
          .then(({ data: res }) => {
            // this.dataListLoading = false;
            if (res.code === 0) {
              this.categoryList = res.data.list
            }
          })
      },
      getAccessPath() {
        const now = new Date();
        const year = now.getFullYear();
        const month = ('0' + (now.getMonth() + 1)).slice(-2);
        const day = ('0' + now.getDate()).slice(-2);
        const hours = ('0' + now.getHours()).slice(-2);
        const minutes = ('0' + now.getMinutes()).slice(-2);
        const formattedTime = year + month + day + hours + minutes
        console.log(formattedTime)

        var str = Math.random().toString(36).slice(-5)
        // console.log(str)
        this.dataForm.accessPath = formattedTime+ str
      },
      handleClose() {
        this.innerDrawer = true
        this.$refs.dataForm.resetFields()
      },
      getInfo() {
        this.$http
          .get(`/customerquality/knowledgeBaseRequirementListData/${this.dataForm.id}`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.dataForm = {
              ...this.dataForm,
              ...res.data,
            };
          })
          .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>
