<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="1000px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改质检项' : '新建质检项'"
    @update:visible="updateVisible"
  >
    <header-title title="基本信息"></header-title>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="标准名称:" prop="qualityStandardId">
            <el-select
              v-model="form.qualityStandardId"
              placeholder="请选择"
              filterable
              style="width: 100%"
            >
              <el-option
                :label="item.name"
                v-for="item in qualityStandardList"
                :key="item.id"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="参数编码:" prop="inspectionCode">
            <el-input
              clearable
              disabled
              v-model="form.inspectionCode"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="参数名称:" prop="inspectionName">
            <el-input
              v-model="form.inspectionName"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="参数类型:" prop="textType">
            <el-select
              v-model="form.textType"
              placeholder="请选择"
              class="ele-block"
            >
              <el-option label="数值" :value="'1'" />
              <el-option label="选择" :value="'2'" />
              <el-option label="上下限" :value="'3'" />
              <el-option label="规格" :value="'4'" />
              <el-option label="时间" :value="'5'" />
              <el-option label="范围" :value="'6'" />
              <el-option label="文本" :value="'7'" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col
          :span="12"
          v-if="
            form.textType == 1 ||
            form.textType == 3 ||
            form.textType == 4 ||
            form.textType == 5 ||
            form.textType == 7
          "
        >
          <el-form-item label="默认值:">
            <el-input
              v-model="form.defaultValue"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.textType == 2">
          <el-form-item label="默认值:" prop="defaultValue">
            <el-select
              v-model="form.defaultValue"
              placeholder="请选择"
              class="ele-block"
            >
              <el-option label="TRUE" :value="'TRUE'" />
              <el-option label="FALSE" :value="'FALSE'" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.textType == 6">
          <el-form-item label="默认值:">
            <el-input
              v-model="form.defaultValue"
              placeholder="案例： 12.7≤IC<19.05 ；IC<12.7"
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.textType == 3">
          <el-form-item label="参数上限:">
            <el-input placeholder="请输入" v-model="form.maxValue" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.textType == 3">
          <el-form-item label="参数下限:">
            <el-input placeholder="请输入" v-model="form.minValue" />
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.textType != 2 && form.textType != 5">
          <el-form-item
            label="参数单位:"
            prop="unitName"
            :rules="[
              {
                required: form.textType == 7 ? false : true,
                message: '请选择参数单位',
                trigger: 'change'
              }
            ]"
          >
            <DictSelection
              dictName="工艺参数单位"
              clearable
              filterable
              v-model="form.unitName"
            >
            </DictSelection>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.textType == 5">
          <el-form-item label="参数单位:" prop=""> h(小时) </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="工艺要求:" prop="inspectionStandard">
            <DictSelection
                style="width: 100px"
              
                clearable
                dictName="数学字符"
                v-model="form.symbol"
              ></DictSelection>
            <el-input v-model="form.inspectionStandard" type="textarea" style="width:calc(100% - 100px)">
        
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位:" prop="unit">
            <DictSelection
              clearable
              dictName="质检项标准单位"
              v-model="form.unit"
            ></DictSelection>
          </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="24">
          <el-form-item label="备注:" prop="inspectionRemark">
            <el-input placeholder="请输入" v-model="form.inspectionRemark" type="textarea" style="height: 100px;" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="附件:" prop="imgUrl">
            <fileUpload v-model="form.imgUrl" type="add" />
          </el-form-item>
        </el-col>
      </el-row>

      <headerTitle title="质检工具">
        <el-button type="primary" size="small" @click="handleAdd"
          >新增</el-button
        >
      </headerTitle>

      <el-table
        ref="multipleTable"
        :data="form.toolList"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
      >
        <el-table-column label="设备名称" prop="code" min-width="120">
          <template slot-scope="{ row }">
            {{ row.name }}
          </template></el-table-column
        >

        <el-table-column label="设备编码" prop="code" min-width="120">
          <template slot-scope="{ row }">
            {{ row.code }}
          </template></el-table-column
        >

        <el-table-column label="牌号" prop="brandNum" min-width="120">
          <template slot-scope="{ row }">
            {{ row.brandNum }}
          </template></el-table-column
        >

        <el-table-column label="型号" prop="modelType" min-width="120">
          <template slot-scope="{ row }">
            {{ row.modelType }}
          </template></el-table-column
        >

        <el-table-column label="操作" fixed="right">
          <template slot-scope="{ $index, row }">
            <el-button type="text" @click="removeItem($index, row)"
              >删除设备</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <headerTitle title="注意事项">
        <el-button type="primary" size="small" @click="addPostscript"
          >新增</el-button
        >
      </headerTitle>

      <el-table
        ref="multipleTable"
        :data="form.postscriptList"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
      >
        <el-table-column label="排序" prop="" width="100">
          <template slot-scope="{ row }">
            <el-input
              placeholder="请输入"
              type="number"
              v-model.number="row.sort"
              clearable
            ></el-input> </template
        ></el-table-column>

        <el-table-column label="注意事项" prop="" min-width="120">
          <template slot-scope="{ row }">
            <el-input
              placeholder="请输入"
              type="textarea"
              :rows="1"
              v-model="row.content"
              clearable
            ></el-input> </template
        ></el-table-column>

        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="{ $index, row }">
            <el-button type="text" @click="removePostscript($index, row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-form>

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

    <ProductModal ref="productRefs" @chooseModal="chooseModal" />
  </ele-modal>
</template>

<script>
  import { save, update, getById } from '@/api/inspectionProject';
  import { getCode } from '@/api/login';

  import { getList } from '@/api/inspectionStandard';
  import ProductModal from './ProductModal.vue';
  import fileUpload from '@/components/addDoc/index.vue';

  export default {
    components: {
      ProductModal,
      fileUpload
    },

    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        qualityStandardId: '',
        inspectionCode: '',
        inspectionName: '',
        textType: '1',

        defaultValue: '',

        maxValue: '',
        minValue: '',
        unitName: '',

        status: 1,

        inspectionStandard: '',
        unit: '',
        symbol: '',
        inspectionRemark: '',
        imgUrl: [],

        toolList: [],
        postscriptList: [],
        toolRemoveIds: [],
        postscriptRemoveIds: []
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },

        qualityStandardList: [],
        // 表单验证规则
        rules: {
          qualityStandardId: [
            { required: true, message: '请选择质检标准', trigger: 'blur' }
          ],
          inspectionName: [
            { required: true, message: '请输入质检名称', trigger: 'blur' }
          ],
          inspectionCode: [
            { required: true, message: '请输入质检编码', trigger: 'blur' }
          ],

          // unitName:
          textType: [
            { required: true, message: '请选择参数类型', trigger: 'blur' }
          ],

          inspectionStandard: [
            {
              required: true,
              message: '请输入',
              trigger: 'blur'
            }
          ]
        },

        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },

    created() {
      this.getListFn();
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          if (!this.isUpdate) {
            delete this.form.id;
          }
          const saveOrUpdate = this.isUpdate ? update : save;

          const defaultValue = Number(this.form.defaultValue);
          const maxValue = Number(this.form.maxValue);
          const minValue = Number(this.form.minValue);
          if ((defaultValue != '') & (maxValue != '')) {
            if (defaultValue > maxValue || defaultValue == maxValue) {
              this.loading = false;
              return this.$message.warning('默认值应小于参数上限');
            }
          }
          if ((defaultValue != '') & (minValue != '')) {
            if (defaultValue < minValue || defaultValue == minValue) {
              this.loading = false;
              return this.$message.warning('默认值应大于参数下限');
            }
          }
          if ((maxValue != '') & (minValue != '')) {
            if (maxValue < minValue || maxValue == minValue) {
              this.loading = false;
              return this.$message.warning('参数上限应大于参数下限');
            }
          }
          if (this.form.textType == 5) {
            this.form.unitName = 'h';
          }
          saveOrUpdate(this.form)
            .then((msg) => {
              this.loading = false;
              this.form = {};
              const info = this.isUpdate ? '修改成功' : '新增成功';
              this.$message.success(info);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },

      getListFn() {
        let param = {
          pageNum: 1,
          size: -1
        };

        getList(param).then((res) => {
          this.qualityStandardList = res.list;
        });
      },

      handleAdd() {
        this.$refs.productRefs.open(this.form.toolList);
      },

      chooseModal(data) {
        this.form.toolList = [...this.form.toolList, ...data];
      },

      removeItem(idx, row) {
        this.$confirm(`是否删除这个设备？`).then(async () => {
          this.form.toolList.splice(idx, 1);

          if (row.id) {
            this.form.toolRemoveIds.push(row.id);
          }
        });
      },

      addPostscript() {
        this.form.postscriptList.push({ sort: null, content: '' });
      },
      removePostscript(idx, row) {
        this.$confirm(`是否删除这个注意事项？`).then(async () => {
          this.form.postscriptList.splice(idx, 1);
          if (row.id) {
            this.form.postscriptRemoveIds.push(row.id);
          }
        });
      }
    },

    watch: {
      async visible(visible) {
        if (visible) {
          if (this.data) {
            const res = await getById(this.data.id);

            this.$util.assignObject(this.form, {
              ...res.data
            });
            this.isUpdate = true;
          } else {
            const code = await getCode('inspection_code');
            this.form.inspectionCode = code;

            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();

          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>

<style lang="scss" scoped>
  .btn_case {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 6px 0;

    span {
      display: inline-block;
      margin: 0 10px;
      font-size: 18px;
      cursor: pointer;
    }
  }
</style>
