<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-07-14 15:21:15
 * @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="processList" :label="$t('researchquality.processName')">
      <el-select @change="getProcessList" v-model="dataForm.processList" multiple :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="productTypeList" :label="$t('researchquality.productTypeName')">
      <el-select v-model="dataForm.productTypeList" multiple :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: "/researchquality/qmsQualityChange",
        infoURL: "/researchquality/qmsQualityChange/{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,
        productTypeList: [],
        processList: [],
        stageTwoDate: null,
        stageTwoName: null,
        temporaryChange: null,
        title: null,
      },
    };
  },
  computed: {
    dataRule() {
      return {
        registrationDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        changeSource: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        title: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        processList: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        productTypeList: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        productId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  mounted () {
    this.getDict();
  },
  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 {
        }
      });
    },
    getDict() {
      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(() => {
        })
    },
    getProcessList(val) {
      console.log(val);
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/researchquality/qmsQualityChange/${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>
