<!-- 用户编辑弹窗 -->
<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"
    append-to-body

  >
    <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"
              :disabled="type=='detail'"
            ></DictSelection>
          </el-form-item>
        </el-col>

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

        <el-col :span="12">
          <el-form-item label="版本号:" prop="version">
            <el-input v-model="form.version"  :disabled="type=='detail'"></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%"
              :disabled="type=='detail'"
            >
              <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"
              :disabled="type=='detail'"

            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <!-- <el-button
            style="margin-left: 100px"
            type="primary"
            @click="addParameter"
            >选择工艺参数标准</el-button
          > -->
          <el-tabs v-model="activeName" type="card">
            <el-tab-pane
              :label="item.name"
              :name="item.name"
              v-for="item in activeList"
              :key="item.name"
            >
              <el-table
                v-show="activeName == '质检项'"
                style="margin-top: 15px"
                :data="form.parameterStandards"
                border
                height="40vh"
              >
                <el-table-column
                  :label="form.singleWeightDivision"
                  align="center"
                >
                  <el-table-column
                    label="参数上限"
                    align="center"
                    v-if="form.parameterType == 3"
                  >
                    <template slot-scope="scope">
                      <el-form-item label-width="0" prop="finalValue">
                        <el-input
                          clearable
                          :disabled="type=='detail'"
                          v-model="scope.row.finalValue"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="参数下限"
                    align="center"
                    v-if="form.parameterType == 3"
                  >
                    <template slot-scope="scope">
                      <el-form-item label-width="0" prop="initialValue">
                        <el-input
                        :disabled="type=='detail'"
                          clearable
                          v-model="scope.row.initialValue"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="默认值"
                    align="center"
                    v-if="form.parameterType != 3"
                  >
                    <template slot-scope="scope">
                      <el-form-item label-width="0" prop="defaultValue">
                        <el-input
                          clearable
                          :disabled="type=='detail'"
                          v-model="scope.row.defaultValue"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column :label="form.tolerance" align="center">
                  <el-table-column label="质检标准" align="center">
                    <template slot-scope="scope">
                      <el-form-item label-width="0" prop="inspectionStandard">
                        <el-input
                          clearable
                          v-model="scope.row.toleranceValue"
                          placeholder="请输入"
                          :disabled="type=='detail'"
                        >
                          <DictSelection
                            style="width: 100px"
                            slot="prepend"
                            clearable
                            :disabled="type=='detail'"
                            dictName="数学字符"
                            v-model="scope.row.symbol"
                          ></DictSelection>
                        </el-input>
                        <!-- </el-form-item> -->
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" align="center" width="80" v-if="type!='detail'">
                    <template slot-scope="scope">
                      <el-link
                        type="danger"
                        :underline="false"
                        @click="handleDeleteItem(scope.$index, scope.row.id)"
                      >
                        删除
                      </el-link>
                    </template>
                  </el-table-column>
                </el-table-column>
              </el-table>
              <el-table
                v-show="activeName == '质检点'"
                style="margin-top: 15px"
                :data="form.linePoints"
                border
                height="40vh"
              >
                <el-table-column label="编码" align="center" prop="pointCode">
                  <template slot-scope="scope">
                    <el-link
                      type="primary"
                      :underline="false"
                      @click="openEdit('detail', scope.row)"
                    >
                      {{ scope.row.pointCode }}
                    </el-link>
                  </template>
                </el-table-column>
                <el-table-column
                  label="名称"
                  align="center"
                  prop="pointName"
                ></el-table-column>
                <el-table-column
                  label="区域"
                  align="center"
                  prop="areaName"
                ></el-table-column>

                <el-table-column label="操作" align="center" width="80">
                  <template slot-scope="scope">
                    <el-link
                      type="danger"
                      :underline="false"
                      v-if="type!='detail'"
                      @click="handleDeleteItem(scope.$index, scope.row.id)"
                    >
                      删除
                    </el-link>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane></el-tabs
          >

          <div class="add-product" @click="add"  v-if="type!='detail'">
            <i class="el-icon-circle-plus-outline"></i>
          </div>
        </el-col>

        <!-- <inspectionProjectList v-if="visible" :linePoints="form.inspectionItems" ref="inspectionProjectRef" /> -->
      </el-row>
    </el-form>

    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save" v-if="type!='detail'">
        保存
      </el-button>
    </template>
    <standardDialog ref="standardDialogRef" @choose="choose" />
    <EquipmentDialog ref="equipmentDialogRef" @choose="choose" />
   
    <edit ref="edit" ></edit>
  </el-dialog>

</template>

<script>
import { save, update, getById } from '@/api/inspectionStandard';
import fileUpload from '@/components/upload/fileUpload';
import standardDialog from './standardDialog';
import EquipmentDialog from './EquipmentDialog';

import edit from '@/views/inspectionPoint/components/edit';

export default {
  components: {
    fileUpload,
    standardDialog,
    EquipmentDialog,edit
  },

  data() {
    const defaultForm = function () {
      return {
        id: '',
        name: '',
        status: '',
        type: '',
        version: '1.0',
        standardCode: '',
        inspectionId: '',
        singleWeightDivision: '',
        tolerance: '',
        parameterStandards: [],
        linePoints: [],
        parameterType: ''
      };
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm() },
      activeName: '质检项',
      activeList: [{ name: '质检项' }, { name: '质检点' }],
      // 表单验证规则
      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' ? '新增' :'edit' ?  '编辑':'详情';
      this.type = type;
      if (this.type != 'add') {
        row.linePoints=row.linePoints||[]
        if (!row.parameterStandards) {
          row.parameterStandards = [];
        } else {
          row.singleWeightDivision =
            row.parameterStandards[0]?.singleWeightDivision;
          row.tolerance = row.parameterStandards[0]?.tolerance;
          row.parameterType = row.parameterStandards[0]?.parameterType;
        }
        this.form = JSON.parse(JSON.stringify(row));
      }
      this.visible = true;
    },
    openEdit(type, row) {
      this.$refs.edit.open(type, row);
    },
    choose(data) {
      if (this.activeName == '质检点') {
        this.form.linePoints = data;
        return;
      }
      let obj = {
        finalValue: data.parameter?.maxValue,
        initialValue: data.parameter?.minValue,
        toleranceValue: data.project?.inspectionStandard,
        symbol: data.project?.symbol,
        singleWeightDivision:
          data.parameter?.name + ' ' + data.parameter?.unitName,
        tolerance: data.project?.inspectionName + ' ' + data.project?.unit,
        parameterType: data.parameter?.textType,
        defaultValue: data.parameter?.defaultValue
      };
      this.$set(this.form, 'singleWeightDivision', obj.singleWeightDivision);
      this.$set(this.form, 'tolerance', obj.tolerance);
      this.form.parameterType = obj.parameterType;
      this.form.parameterStandards.push(obj);
    },
    add() {
      if (this.activeName == '质检点') {
        this.$refs.equipmentDialogRef.open(this.form.linePoints.map(item=>item.id));
        return;
      }
      if (!this.form.parameterStandards.length) {
        this.$refs.standardDialogRef.open();
        return;
      }
      let obj = {
        finalValue: '',
        initialValue: '',
        toleranceValue: '',
        singleWeightDivision: '',
        tolerance: ''
      };
      this.form.parameterStandards.push(obj);
    },
    handleDeleteItem(index) {
      this.form.parameterStandards.splice(index, 1);
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        if (this.type == 'add') {
          delete this.form.id;
        }
        // this.form.inspectionId = this.$refs.inspectionProjectRef.getValue();

        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;
}
:deep(
    .el-dialog:not(.ele-dialog-form)
      .el-dialog__body
      .el-form
      .el-table__body
      .el-table__row
      .el-form-item:last-child
  ) {
  margin-bottom: 0 !important;
}
.add-product {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  font-size: 30px;
  color: #1890ff;
  margin: 10px 0;
  cursor: pointer;
}
</style>
