<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-07-13 14:49:30
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()"
    label-width="120px">
    <el-form-item prop="registrationDate" :label="$t('researchquality.registrationDate')">
      <el-date-picker v-model="dataForm.registrationDate" type="datetime"
        :placeholder="$t('researchquality.registrationDate')">
      </el-date-picker>
    </el-form-item>
    <el-form-item prop="changeSource" :label="$t('researchquality.changeSource')">
      <el-select v-model="dataForm.changeSource" :placeholder="$t('researchquality.changeSource')">
        <el-option v-for="item in changeSourceList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="sourceRemark" :label="$t('researchquality.sourceRemark')">
      <el-input v-model="dataForm.sourceRemark" :placeholder="$t('researchquality.sourceRemark')">
      </el-input>
    </el-form-item>
    <el-form-item prop="owner" :label="$t('researchquality.owner')">
      <el-select v-model="dataForm.owner" :placeholder="$t('researchquality.owner')">
        <el-option v-for="item in userList" :key="item.id" :label="item.realName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="title" :label="$t('researchquality.title')">
      <el-input v-model="dataForm.title" :placeholder="$t('researchquality.title')">
      </el-input>
    </el-form-item>
    <el-form-item prop="processId" :label="$t('researchquality.processName')">
      <el-select v-model="dataForm.processId" :placeholder="$t('researchquality.processName')">
        <el-option v-for="item in processList" :key="item.id" :label="item.processName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="productId" :label="$t('researchquality.productName')">
      <el-select v-model="dataForm.productId" :placeholder="$t('researchquality.productName')">
        <el-option v-for="item in productList" :key="item.id" :label="item.productName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="processId" :label="$t('researchquality.productTypeName')">
      <el-select v-model="dataForm.processId" :placeholder="$t('researchquality.productTypeName')">
        <el-option v-for="item in productTypeList" :key="item.id" :label="item.productTypeName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="temporaryChange" :label="$t('researchquality.temporaryChange')">
      <el-switch v-model="dataForm.temporaryChange" :active-value="1" :inactive-value="0"></el-switch>
    </el-form-item>
    <!-- <el-row :gutter="20">
      <el-col :span="12"> {{ this.$t('researchquality.planPhase') }} </el-col>
      <el-col :span="12"> {{ this.$t('researchquality.planTime') }} </el-col>
    </el-row> -->
    <el-row :gutter="24">
      <el-col :span="12">
        <el-form-item prop="stageOneName" :label="$t('researchquality.stageOneName')">
          <el-input v-model="dataForm.stageOneName" :placeholder="$t('researchquality.stageOneName')">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="stageOneDate" :label="$t('researchquality.stageOneDate')">
          <el-date-picker v-model="dataForm.stageOneDate" type="datetime"
            :placeholder="$t('researchquality.stageOneDate')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="24">
      <el-col :span="12">
        <el-form-item prop="stageTwoName" :label="$t('researchquality.stageTwoName')">
          <el-input v-model="dataForm.stageTwoName" :placeholder="$t('researchquality.stageTwoName')">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="stageTwoDate" :label="$t('researchquality.stageTwoDate')">
          <el-date-picker v-model="dataForm.stageTwoDate" type="datetime"
            :placeholder="$t('researchquality.stageTwoDate')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="24">
      <el-col :span="12">
        <el-form-item prop="stageThreeName" :label="$t('researchquality.stageThreeName')">
          <el-input v-model="dataForm.stageThreeName" :placeholder="$t('researchquality.stageThreeName')">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="stageThreeDate" :label="$t('researchquality.stageThreeDate')">
          <el-date-picker v-model="dataForm.stageThreeDate" type="datetime"
            :placeholder="$t('researchquality.stageThreeDate')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="24">
      <el-col :span="12">
        <el-form-item prop="stageFourName" :label="$t('researchquality.stageFourName')">
          <el-input v-model="dataForm.stageFourName" :placeholder="$t('researchquality.stageFourName')">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="stageFourDate" :label="$t('researchquality.stageFourDate')">
          <el-date-picker v-model="dataForm.stageFourDate" type="datetime"
            :placeholder="$t('researchquality.stageFourDate')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-form-item prop="productTypeStatus" :label="$t('basic.status')">
      <el-select v-model="dataForm.productTypeStatus" :placeholder="$t('basic.status')">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item> -->
  </el-form>
</template>

<script>
import debounce from "lodash/debounce";
import basicAdd from "@/mixins/basic-add";
export default {
  mixins: [basicAdd],
  data() {
    return {
      urlOptions: {
        submitURL: "/supplier/qmsSupplier",
        infoURL: "/supplier/qmsSupplier/{id}",
        getUserURL: '/sys/user/page',
        getProcessURL: '/basic/qmsProcess/page',
        getProductURL: '/basic/qmsProduct/page',
        getProductTypeURL: '/basic/qmsProductType/page',
        getSupplierTypeListURL: '/supplier/qmsSupplierType/page'
      },
      userList: [],
      processList: [],
      productTypeList: [],
      productList: [],
      changeSourceList: [
        {
          id: 0,
          name: '工程变更'
        },
        {
          id: 1,
          name: '设计变更'
        },
        {
          id: 2,
          name: 'CCB'
        }
      ],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      dataForm: {
        id: "",
        changeSource:null,
        currentStage: null,
        owner: null,
        productId: null,
        productName: null,
        qualityChangeStatus: null,
        registrationDate: null,
        sourceRemark: null,
        stageFourDate: null,
        stageFourName: null,
        stageOneDate: null,
        stageOneName: null,
        stageThreeDate: null,
        stageThreeName: null,
        stageTwoDate: null,
        stageTwoName: null,
        temporaryChange: null,
        title: null,
      },
    };
  },
  computed: {
    dataRule() {
      return {
        code: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        ment: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        supplierStatus: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  methods: {
    init(id, ) {
      this.dataForm.id = id || ""
      // this.dataForm.dictTypeId = dictTypeId || "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.getCode()
        }
      });
    },
    getCode() {
      this.$http
        .get(this.urlOptions.getUserURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.userList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .get(this.urlOptions.getProcessURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.processList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .get(this.urlOptions.getProductURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.productList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .get(this.urlOptions.getProductTypeURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.productTypeList = res.data.list
          }
        })
        .catch(() => {
        })
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/supplier/qmsSupplier/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        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: () => {
                  console.log(1111);
                  this.visible = false;
                  this.$emit("successSubmit");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
