<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-size: 20px">基本信息</span>
      </div>
      <el-form
        ref="testForm"
        :model="formData"
        :rules="rules"
        label-width="120px"
        label-position="right"
        :validate-on-rule-change="false"
        class="vd-form-three"
      >
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="零件号:" prop="partCode">
              <el-input v-model="formData.partCode" disabled placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="零件名称:" prop="partName">
              <el-input v-model="formData.partName" disabled placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品型号:" prop="productType">
              <el-input v-model="formData.productType" placeholder="请输入" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- <el-form-item label="相似零件:" prop="productType">
              
            </el-form-item> -->
          </el-col>
          <el-col :span="8" v-if="partType == '烧结'">
            <el-form-item label="是否为玻璃饼:" prop="isClassCake">
              <el-switch
                v-model="formData.isClassCake"
                :disabled="disabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结'">
            <el-form-item label="是否印字:" prop="isPrint">
              <el-radio-group v-model="formData.isPrint" v-if="!formData.isGlassCake">
                <el-radio value="1">否</el-radio>
                <el-radio value="2">单</el-radio>
                <el-radio value="3">双</el-radio>
              </el-radio-group>
              <el-radio-group v-model="formData.isPrint" v-if="formData.isGlassCake">
                <el-radio value="2">是</el-radio>
                <el-radio value="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结' && formData.isGlassCake">
            <el-form-item label="材料:" prop="material">
              <el-input v-model="formData.material" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结' && formData.isGlassCake">
            <el-form-item label="粉重:" prop="powderWeight">
              <el-input v-model="formData.powderWeight" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结' && formData.isGlassCake">
            <el-form-item label="成型厚度:" prop="formingThickness">
              <el-input v-model="formData.formingThickness" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结' && formData.isGlassCake">
            <el-form-item label="面积(dm²):" prop="area">
              <el-input v-model="formData.area" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="硬度:" prop="hardness">
              <el-input v-model="formData.hardness" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="材料:" prop="material">
              <el-input v-model="formData.material" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col
            :span="8"
            v-if="partType == '热表' || (partType == '烧结' && formData.isGlassCake)"
          >
            <el-form-item label="物料编码:" prop="plateGoodsCode">
              <el-select
                v-model="formData.plateGoodsCode"
                placeholder="请选择"
                style="width: 250px"
                clearable
                filterable
              >
                <el-option :value="'04-05-023'" label="金钾-04-05-023" />
                <el-option :value="'04-05-024'" label="银板-04-05-024" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="镀种1:" prop="plate">
              <el-select
                v-model="formData.plate"
                placeholder="请选择"
                style="width: 250px"
                clearable
                filterable
              >
                <el-option
                  v-for="item in plateList"
                  :label="item.plating"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="镀层代号1:" prop="plateCode">
              <el-input v-model="formData.plateCode" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="面积1(dm²):" prop="area">
              <el-input v-model="formData.area" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="镀种2:" prop="plateTwo">
              <el-select
                v-model="formData.plateTwo"
                placeholder="请选择"
                style="width: 250px"
                clearable
                filterable
              >
                <el-option
                  v-for="item in plateList"
                  :label="item.plating"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="镀层代号2:" prop="plateCodeTwo">
              <el-input v-model="formData.plateCodeTwo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="面积2(dm²):" prop="areaTwo">
              <el-input v-model="formData.areaTwo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '热表'">
            <el-form-item label="镀层厚度:" prop="plateThickness">
              <el-input v-model="formData.plateThickness" placeholder="请输入" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="产品应用领域:" prop="productApplicationArea">
              <el-input v-model="formData.productApplicationArea" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="工艺路线:" prop="craftWay">
              {{ formData.craftWay }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="有效期:" prop="periodOfValidity">
              <el-radio-group v-model="formData.periodOfValidity" @change="periodOfValidityFn">
                <el-radio value="1" size="large">长期有效</el-radio>
                <el-radio value="2" size="large">自定义</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="有效周期(天):" prop="expirationPeriod">
              <el-input-number
                :max="100000"
                :min="0"
                v-model="formData.expirationPeriod"
                precision="0"
                controls-position="right"
                placeholder="请输入"
                style="width: 250px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="到期提醒(天):" prop="advanceRemind">
              <el-input-number
                :max="100000"
                :min="0"
                v-model="formData.advanceRemind"
                precision="0"
                controls-position="right"
                placeholder="请输入"
                style="width: 250px"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="涂色标个数:" prop="tsbNum">
              <el-input-number
                v-model="formData.tsbNum"
                :min="1"
                :max="10"
                controls-position="right"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="涂色带个数:" prop="tsdNum">
              <el-input-number
                v-model="formData.tsdNum"
                :min="1"
                :max="10"
                controls-position="right"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="涂箭头个数:" prop="tjtNum">
              <el-input-number
                v-model="formData.tjtNum"
                :min="1"
                :max="10"
                controls-position="right"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="色标色带:" prop="memo">
              <el-input
                v-model="formData.memo"
                :rows="3"
                type="textarea"
                maxlength="150"
                show-word-limit
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="关键信息:" prop="keyInfo">
              <el-input
                v-model="formData.keyInfo"
                type="textarea"
                maxlength="150"
                show-word-limit
                rows="3"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col
            :span="24"
            v-if="partType == '热表' || (partType == '烧结' && formData.isGlassCake)"
          >
            <el-form-item label="工艺校验:" prop="sinTer">
              <el-checkbox v-model="formData.goldMark" v-if="partType == '热表'"
                >镀金标识</el-checkbox
              >
              <el-checkbox
                v-model="formData.markingsTest"
                @change="setDataLocal"
                :true-label="'1'"
                :false-label="'0'"
                >涂色标</el-checkbox
              >
              <el-checkbox
                v-model="formData.fluorescenceCheck"
                @change="setDataLocal"
                v-if="partType == '热表'"
                :true-label="'1'"
                :false-label="'0'"
                >荧光检</el-checkbox
              >
              <el-checkbox
                v-model="formData.hotDispose"
                @change="setDataLocal"
                v-if="partType == '热表'"
                :true-label="'1'"
                :false-label="'0'"
                >热处理检验</el-checkbox
              >
              <el-checkbox
                v-model="formData.inkjetPrinting"
                @change="setDataLocal"
                v-if="partType == '热表'"
                :true-label="'1'"
                :false-label="'0'"
                >喷码</el-checkbox
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card
      class="box-card"
      v-if="formData.isSintering == '1' && formData.isClassCake == '0'"
      style="margin-top: 24px"
    >
      <div slot="header" class="clearfix">
        <span style="font-size: 20px">子件信息</span>
      </div>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        v-model="form"
        v-model:page="page"
        ref="crud"
      >
      </avue-crud>
    </el-card>
  </div>
</template>
<script>
import {
  getPartDetail,
  authorizedAccomplish,
  getPlatingList,
} from '@/api/processManagement/taskProcessing';

export default {
  components: {},
  props: {
    // partType: {
    //   type: String,
    //   default: '',
    // },
    partId: {
      type: [Number, String],
      default: null,
    },
    childPage: {
      type: Boolean,
      default: false,
    },
    psId: {
      type: Number,
      default: null,
    },
    rowObject: {
      type: Object,
      default: null,
    },
    dsPartInfo: {
      type: Object,
      default: null,
    },
    updateRow: {
      type: Object,
      default: null,
    },
  },
  data() {
    var checkCode = (rule, value, callback) => {
      this.$ajax
        .post('dsPart/checkCode', {
          partId: this.formData.partId,
          partCode: value,
        })
        .then(res => {
          if (res.data) {
            callback(new Error(this.$t('global.duplicateCode')));
          } else {
            callback();
          }
        });
    };
    const proportionNum = (rule, value, callback) => {
      if (value === 0) {
        callback(new Error(this.$t('输入的值必须大于0')));
      } else {
        callback();
      }
    };
    return {
      disabled: false,
      formData: {
        sinTer: false,
        goldMark: false,
        periodOfValidity: '1',
        youxiaozhouqi: '',
        expirationPeriod: '',
        fenzhong: '',
      },
      validateObj: {
        fluorescenceCheck: false,
        inkjetPrinting: false,
        hotDispose: false,
      },
      notSinTer: true,
      notGlassCake: true,
      tsbNum: null,
      tsdNum: null,
      tjtNum: null,
      numOptions: [],
      rules: {
        plate: [{ required: true, message: '请输入', trigger: 'blur' }],
        material: [{ required: true, message: '请输入', trigger: 'blur' }],
        isGlassCake: [{ required: true, message: '请选择', trigger: 'blur' }],
        isPrint: [{ required: true, message: '请选择', trigger: 'blur' }],
        dueDate: [{ required: true, message: '请选择', trigger: 'blur' }],
        partCode: [{ required: true, message: '请选择', trigger: 'blur' }],
        partName: [{ required: true, message: '请选择', trigger: 'blur' }],
        craftWay: [{ required: true, message: '请输入', trigger: 'blur' }],
        area: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: proportionNum, trigger: 'blur' },
        ],
        expirationPeriod: [{ required: false, message: '请输入', trigger: 'blur' }],
        plate: [{ required: true, message: '请输入', trigger: 'blur' }],
        material: [{ required: true, message: '请输入', trigger: 'blur' }],
        mianji1: [{ required: true, message: '请输入', trigger: 'blur' }],
        periodOfValidity: [{ required: true, message: '请选择', trigger: 'blur' }],
        youxiaozhouqi: [{ required: false, message: '请选择', trigger: 'blur' }],
        fenzhong: [{ required: true, message: '请输入', trigger: 'blur' }],
        chengxinghoudu: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      submitLoading: false,
      data: [],
      option: {
        tip: false,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        dialogWidth: '60%',
        border: true,
        selection: false,
        viewBtn: true,
        menuWidth: 240,
        dialogClickModal: false,
        excelBtn: true,
        viewBtn: false,
        editBtnText: '编辑',
        refreshBtn: false,
        searchShowBtn: false,
        gridBtn: false,
        menu: false,
        header: false,
        index: true,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        align: 'center',
        column: [
          {
            label: '零件号',
            prop: 'partCode',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '零件名称',
            prop: 'partName',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '产品型号',
            prop: 'productType',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '数量',
            prop: 'quota',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '面积(d㎡)',
            prop: 'area',
            search: false,
            sortable: true,
            overHidden: true,
            cell: true,
            rules: [
              {
                required: true,
                message: '请输入面积',
                trigger: 'blur',
              },
              {
                validator: (rule, value, callback) => {
                  const row = rule.$row; // avue 支持 $row 获取当前行数据
                  const type = row.sinTerType;
                  const area = parseFloat(value);
                  if (type !== 2) {
                    // 非壳体
                    if (isNaN(area) || area > 1) {
                      callback(new Error('非壳体面积不能大于1'));
                    } else {
                      callback();
                    }
                  } else {
                    callback(); // 壳体不限制
                  }
                },
                trigger: 'blur',
              },
            ],
          },

          {
            label: '总面积',
            prop: 'totalArea',
            cell: true,
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '零件类型',
            prop: 'sinTerType',
            search: false,
            sortable: true,
            overHidden: true,
            type: 'select',
            filterable: true,
            clearable: true,
            cell: true,
            dicData: [
              {
                label: '插针',
                value: 1,
              },
              {
                label: '壳体',
                value: 2,
              },
              {
                label: '石墨模',
                value: 5,
              },
              {
                label: '自制玻璃饼',
                value: 3,
              },
              {
                label: '其他',
                value: 4,
              },
            ],
          },
          {
            label: '是否电镀',
            prop: 'isElectroplating',
            cell: true,
            search: false,
            sortable: true,
            overHidden: true,
            type: 'select', // 使用下拉选择
            dicData: [
              { label: '是', value: '是' },
              { label: '否', value: '否' },
            ],
            // 控制是否可编辑：仅当 partCode 包含 A1 且 sinTerType === 1 时可编辑
            editDisplay: row => {
              return row.sinTerType === 1 && row.partCode && /A1/.test(row.partCode);
            },
          },
          {
            label: '修改人',
            prop: 'updateUser',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '修改时间',
            prop: 'updateTime',
            search: false,
            sortable: true,
            overHidden: true,
          },
        ],
      },
      partType: '',
      loading: false,
      plateList: [], //
    };
  },
  created() {},
  mounted() {
    this.disabled = this.dialogType == 'view' ? true : false;
    // this.formData = this.dsPartInfo;
    this.getPartDetails();
    this.getPlatingList();
  },
  methods: {
    getPlatingList() {
      getPlatingList().then(res => {
        this.plateList = res.data.data;
      });
    },
    // 获取零件信息详情数据
    getPartDetails() {
      getPartDetail({ taskId: this.updateRow.id, version: this.updateRow.version }).then(res => {
        this.formData = res.data.data.dsPartEntity;
        this.data = res.data.data.dsPartEntities;

        this.$emit('updateVersion', res.data.data.partVersion);

        if (this.data.length > 0) {
          this.data.forEach(item => {
            item.$cellEdit = true;
            item.totalArea = (item.quota * item.area).toFixed(5); // 保留5位小数（字符串）
          });
        }

        this.partType = res.data.data.dsPartEntity.isSintering == '1' ? '烧结' : '热表';

        if (res.data.data.dsPartEntity.isGlassCake === '0') {
          this.formData.isGlassCake = false;
        } else {
          this.formData.isGlassCake = true;
        }
      });
    },

    //有效期切换
    periodOfValidityFn(value) {
      if (value == 2) {
        this.rules.youxiaozhouqi = [{ required: true, message: '请输入', trigger: 'blur' }];
        this.rules.expirationPeriod = [{ required: true, message: '请输入', trigger: 'blur' }];
      } else {
        this.rules.youxiaozhouqi = [{ required: false, message: '请输入', trigger: 'blur' }];
        this.rules.expirationPeriod = [{ required: false, message: '请输入', trigger: 'blur' }];
      }
      this.$nextTick(() => {
        this.$refs.testForm.clearValidate(); // 清空之前的校验结果
      });
    },
    clear() {
      this.formData = {};
      this.$refs.form.resetFields();
    },
    loadData(partId) {
      this.formData = { sinTer: false, goldMark: false };
      this.notSinTer = true;
      this.notGlassCake = true;
      this.$refs.form.resetFields();
      // if (partId) {
      //   this.$ajax.get('dsPart/getByKey/' + partId).then((res) => {
      //     if (this.$ifAjax(res)) {
      //       this.formData = res.data;

      //       if (this.rowObject != null) {
      //         // 由子件信息页面编辑带入信息
      //         this.formData.partName = this.rowObject.subName; // 零件名称=子件名称
      //         this.formData.productType = this.rowObject.productType; // 产品型号
      //         this.formData.material = this.rowObject.material; // 材料
      //         this.formData.area = this.rowObject.area; // 面积(dm²)
      //         this.formData.plate = this.rowObject.plate; // 镀种
      //         this.formData.plateCode = this.rowObject.plateCode; // 镀层代号
      //         this.formData.plateThickness = this.rowObject.plateThickness; // 镀层厚度
      //         if (this.rowObject.sinTerType == 3) {
      //           this.formData.glassCake = true;
      //         } else if (this.rowObject.sinTerType == 5) {
      //           this.formData.sinTer = true;
      //         } else {
      //           this.formData.glassCake = false;
      //           this.formData.sinTer = false;
      //         }
      //       }

      //       const craftWay = this.formData.craftWay;
      //       if (
      //         craftWay.indexOf('涂色标') !== -1 ||
      //         craftWay.indexOf('涂色带') !== -1
      //       ) {
      //         this.formData.markingsTest = true;
      //       }
      //       if (craftWay.indexOf('热') !== -1) {
      //         this.validateObj.hotDispose = true;
      //       }
      //       if (craftWay.indexOf('荧光检') !== -1) {
      //         this.validateObj.fluorescenceCheck = true;
      //       }
      //       if (craftWay.indexOf('喷码') !== -1) {
      //         this.validateObj.inkjetPrinting = true;
      //       }
      //       this.setDataLocal();
      //     }
      //   });
      // }
      this.$refs.craftEdit.getData(partId, true);
    },
    setDataLocal() {
      this.$setItem('零件信息信息验证', this.validateObj);
    },
    // validatorMethod(rule, value, callback) {
    //   const regex = /^[0-9]\d*$/;
    //   if (!value) {
    //     callback(new Error('请输入正整数'));
    //   } else if (!regex.test(value)) {
    //     callback(new Error('请输入正整数'));
    //   } else {
    //     callback();
    //   }
    // },
    submit(flag) {
      this.submitLoading = true;
      console.log(98989, this.formData);
      this.$refs.testForm.validate(valid => {
        if (valid) {
          let isReturn = false;
          if (this.formData.plateCode || this.formData.plateThickness) {
            if (!this.formData.plateGoodsCode) {
              isReturn = true;
            }
          }
          if (isReturn && this.notSinTer) {
            this.submitLoading = false;
            return this.$message.warning('请选择镀层物料');
          }
          if (this.formData.craftWay.indexOf('涂色') != -1) {
            if (
              this.formData.tsdNum === null &&
              this.formData.tsbNum === null &&
              this.formData.tjtNum === null
            ) {
              this.submitLoading = false;
              return this.$message.warning('一级路线包含涂色，请填写涂色数据');
            }
          }
          for (const item of this.data) {
            if (item.sinTerType === 1 && item.partCode && /A1/.test(item.partCode)) {
              if (!item.isElectroplating) {
                this.$message.warning(`子件【${item.partName}】为A1插针，必须填写“是否电镀”`);
                this.submitLoading = false;
                return;
              }
            }
          }
          this.formData.childPage = this.childPage;
          this.formData.psId = this.psId;

          // 判断单个面积是不是大于1 A1配置码的不能为空
          // this.data.forEach(item=>{
          //   if(item.type=='2'){

          //   }
          // })
          authorizedAccomplish({ dsPart: this.formData, partList: this.data }).then(res => {
            this.$message.success('操作成功');
            this.cancel(flag, res.data);
          });
          // this.$ajax.post('dsPart/addOrEdit', this.formData).then((res) => {
          //   if (this.$ifAjax(res)) {
          // this.$message.success(
          //   this.$t('videaVueLib.publics.message.success')
          // );
          //     this.cancel(flag, res.data);
          // this.$refs.craftEdit.getData(this.partId, true);
          //   }
          //   this.submitLoading = false;
          // });
        } else {
          this.submitLoading = false;
        }
      });
    },
    cancel(flag, dsPart) {
      this.$emit('cancelClose', flag, dsPart);
      // if (!flag) {
      //   this.$refs.craftEdit.refreshLoad();
      // }
    },
  },
};
</script>
<style lang="scss" scoped>
.clearfix {
  margin-bottom: 12px;
}

.auto-fit {
  width: 100%;
}

.vd-form-three {
  .el-form-item {
    width: 100%;

    .el-input,
    .el-select,
    .el-input-number,
    .el-date-editor.el-input__wrapper {
      width: 250px;
    }
  }
}
:deep(.el-table .el-table__cell) {
  height: 50px !important;
  padding: 0 !important;
  line-height: 50px !important;
}
</style>
