<!-- 用户编辑弹窗 -->
<template>
  <el-dialog class="ele-dialog-form" :title="title" :visible.sync="visible" :before-close="handleClose"
    :close-on-click-modal="false" :close-on-press-escape="false" width="1000px">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>


        <el-col :span="12">
          <el-form-item label="标准类型:" prop="type">
            <DictSelection dictName="质检标准类型" v-model="form.type"></DictSelection>
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="标准名称:" prop="name">
            <el-input clearable v-model="form.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标准代码:" prop="standardCode">
            <el-input v-model="form.standardCode"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="版本号:" prop="version">
            <el-input v-model="form.version"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <el-select v-model="form.status" placeholder="请选择" style="width: 100%">
              <el-option label="停用" :value="0" />
              <el-option label="启用" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>


        <el-col :span="8">
          <el-form-item prop="imgUrl" label="附件">
            <fileUpload v-model="form.imgUrl" module="main" :showLib="false" :limit="1" />
          </el-form-item>
        </el-col>






      </el-row>
    </el-form>








    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>




  </el-dialog>
</template>

<script>

import { save, update, getById } from '@/api/inspectionStandard';
import fileUpload from '@/components/upload/fileUpload';
export default {
  components: {
    fileUpload
  },



  data() {
    const defaultForm = function () {
      return {
        id: '',
        name: '',
        status: '',
        type: '',
        version: '1.0',
        standardCode: '',

      };
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm() },
      // 表单验证规则
      rules: {

        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        version: [{ required: true, message: '请输入', trigger: 'blur' }],
        type: {
          required: true,
          message: '请选择',
          trigger: 'change'
        },

        status: {
          required: true,
          message: '请选择',
          trigger: 'change'
        },

      },
      visible: false,
      type: null,
      title: null,
      loading: false,

    };
  },

  created() {

  },
  methods: {
    open(type, row) {
      this.title = type == 'add' ? '新增' : '编辑'
      this.type = type;
      if (this.type == 'edit') {

        this.getDetail(row.id)
      }
      this.visible = true;


    },

    getDetail(id) {
      getById(id).then(res => {
        this.form = res.data

      })
    },

    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        if (this.type == 'add') {
          delete this.form.id;
        }
        let URL = this.type == 'add' ? save : update
        URL(this.form)
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.handleClose();
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
          });
      });
    },
    restForm() {
      this.form = { ...this.defaultForm() };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.restForm();
      this.visible = false;
    },









  }
};
</script>

<style lang="scss" scoped>
.location-warp {
  display: flex;

  .detail {
    margin-left: 10px;
  }
}

:deep(.el-dialog:not(.ele-dialog-form) .el-dialog__body .el-form .el-form-item:last-child) {
  margin-bottom: 22px;
}
</style>
