<!-- 用户编辑弹窗 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="1000px"
    append-to-body

    class="ele-dialog-form"

  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-if="visible">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称:" prop="pointName">
            <el-input :disabled="type=='detail'" clearable v-model="form.pointName" placeholder="请输入"  />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区域:" prop="areaName">
            <el-input :disabled="type=='detail'" clearable v-model="form.areaName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="质检分类:" prop="typeValue">
            <ele-tree-select
            :disabled="type=='detail'"
              multiple
              clearable
              :data="typeList"
              v-model="form.typeValue"
              placeholder="请选择"
              default-expand-all
              valueKey="id"
              labelKey="name"
              :checkStrictly="true"
              @change="changeList"
              ref="treeSelect"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="附件:" prop="files">
            <fileUpload
              v-model="form.files"
              module="main"
              :showLib="false"
              :limit="1"
              :disabled="type=='detail'"

            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane
            :label="item.categoryLevelName"
            :name="item.id"
            v-for="item in form.qualityLevelList"
            :key="item.id"
          >
            <el-row>
              <inspectionProjectList
                v-if="visible"
                :list="item.itemVOList"
                ref="inspectionProjectRef"
                @changeDate="changeDate"
                :disabled="type=='detail'"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
             
      <el-button type="primary" :loading="loading" @click="save"  :disabled="type=='detail'">
        保存
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { save, update } from '@/api/inspectionPoint';
import fileUpload from '@/components/upload/fileUpload';
import EquipmentDialog from './EquipmentDialog';
import inspectionProjectList from './inspectionProjectList';
import { getTreeByPid } from '@/api/classifyManage';

export default {
  components: {
    fileUpload,
    EquipmentDialog,
    inspectionProjectList
  },

  data() {
    const defaultForm = function () {
      return {
        id: '',
        pointName: '',
        files: [],
        inspectionId: '',
        itemVOList: [],
        qualityLevelList: [],
        typeValue: []
      };
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm() },
      // 表单验证规则
      rules: {
        pointName: [{ required: true, message: '请输入', trigger: 'blur' }],
        typeValue: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      visible: false,
      type: null,
      title: null,
      loading: false,
      typeList: [],
      activeName: ''
    };
  },

  created() {
    getTreeByPid(12).then((res) => {
      this.typeList = res.data;
      // console.log(res,'dsds')
    });
  },
  methods: {
    changeList(data) {
      console.log(data);
      let list = [];
      data.forEach((item) => {
        let val = this.$refs.treeSelect.getNodeByValue(item);
        list.push({
          id: val.id,
          categoryLevelName: val.name,
          itemVOList:this.form.qualityLevelList.find(item=>item.id==val.id)?.itemVOList||[]
        });
      });
      this.form.qualityLevelList = JSON.parse(JSON.stringify(list));
      if (this.form.qualityLevelList.length == 1) {
        this.activeName = this.form.qualityLevelList[0].id;
      }
    },
    changeDate(data) {
      this.form.qualityLevelList.forEach((item) => {
        if (item.id == this.activeName) {
          item.itemVOList = data;
        }
      });
      // console.log(data);
    },
    open(type, row) {
      this.title = type == 'add' ? '新增' :type == 'edit' ?  '编辑':'详情';
      this.type = type;
      if (this.type != 'add') {
        this.form = row;
        if(row.qualityLevelList.length>0){
          this.form.typeValue=row.qualityLevelList.map(item=>item.id)
          this.activeName = row.qualityLevelList[0].id;

        }
      }
      this.visible = true;
    },

    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        // this.loading = true;
        if (this.type == 'add') {
          delete this.form.id;
         
        }
        delete this.form.createTime
        // this.form.inspectionId = this.$refs.inspectionProjectRef.getValue();
        // return;
        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();
      });
    },
    handleClick() {},
    handleClose() {
      this.restForm();
      this.visible = false;
    }
  }
};
</script>

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