<template>
  <div>
    <div class="my-title">{{ !id ? "新增" : "修改" }}</div>
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-row class="my-row">
        <el-col :span="8">
          <el-form-item label="方案编码" prop="optionCode">
            <el-input
              v-model="dataForm.optionCode"
              placeholder="方案编码"
              :disabled="isEdit"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料名称" prop="productId">
            <el-select
              v-model="dataForm.productId"
              placeholder="请选择"
              filterable
              remote
              clearable
            >
              <el-option
                v-for="item in materialList"
                :key="item.materialId"
                :label="item.materialName"
                :value="item.materialId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="方案名称" prop="optionName">
            <el-input
              v-model="dataForm.optionName"
              placeholder="方案名称"
              :disabled="isEdit"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="my-row">
        <el-col :span="12">
          <el-form-item label="备注说明" prop="remark">
            <el-input
              type="textarea"
              v-model="dataForm.remark"
              placeholder="备注"
              :disabled="isEdit"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="my-row">
        <upload-component
          :title="'工艺方案附件'"
          :accept="'*'"
          :file-obj-list="fileList"
          @uploadSuccess="uploadSuccess"
        />
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
</template>

<script>
import UploadComponent from "../common/upload-component";
import { getMaterialList, getMaterialTechDetail } from "@/api/material";
export default {
  name: "material-tech-add-or-update",
  components: { UploadComponent },
  data() {
    return {
      id: 0,
      isEdit: false,
      dataForm: {
        optionCode: "",
        productId: "",
        optionName: "",
        remark: "",
      },
      fileList: [],
      materialList: [],
      dataRule: {
        optionCode: [
          { required: true, message: "请输入方案编码", trigger: "blur" },
        ],
        materialName: [
          { required: true, message: "请输入物料名称", trigger: "blur" },
        ],
        optionName: [
          { required: true, message: "请输入方案名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    onChose() {
      this.$emit("onChose");
    },
    async init(id, display) {
      this.id = id || 0;
      this.fileList = [];
      this.$refs["dataForm"].resetFields();
      this.materialList = [];

      await this.getMaterialList();

      if (!id) return;

      await getMaterialTechDetail(id).then(({ data }) => {
        if (data && data.code === "200") {
          this.dataForm.optionId = id;
          this.dataForm.productId = data.data.productId;
          this.dataForm.optionCode = data.data.optionCode;
          this.dataForm.optionName = data.data.optionName;
          this.dataForm.remark = data.data.remark;
          
          // 附件
          if (data.data.attachList) {
            data.data.attachList.forEach((item) => {
              this.fileList.push({
                name: item.fileName,
                url: item.url,
                id: item.url,
              });
            });
          }
        }
      });
    },
    uploadSuccess(fileList) {
      this.fileList = fileList;
    },
    async getMaterialList() {
      await getMaterialList().then(({ data }) => {
        if (data && data.code === "200") {
          this.materialList = data.data.records;
        }
      });
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          // 产品技术文件
          let fList = this.fileList;
          if (fList.length > 0) {
            this.dataForm.attachList = [];
            for (let i = 0; i < fList.length; i++) {
              this.dataForm.attachList.push({
                fileName: fList[i].name,
                url: fList[i].url,
              });
            }
          } else {
            this.$message.error("请上传工艺方案附件");
            return;
          }

          this.$http({
            url: !this.id
              ? this.$http.adornUrl(`/biz-service/pro-technology-option/save`)
              : this.$http.adornUrl(
                  `/biz-service/pro-technology-option/update`
                ),
            method: "post",
            data: this.$http.adornData({ ...this.dataForm, orgId: this.orgId }),
          }).then(({ data }) => {
            if (data && data.code === "200") {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.onChose();
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped></style>
