<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-size: 20px">基本信息</span>
      </div>
      <el-form ref="form" :model="formData" :rules="rules" :inline="true" class="vd-form-three" label-width="120px">
        <el-row>
          <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" v-if="partType == '热表'">
            <el-form-item label="产品型号:" prop="productType">
              <el-input v-model="formData.productType" placeholder="请输入" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结'">
            <el-form-item label="是否为玻璃饼:" prop="isGlassCake">
              <el-switch v-model="formData.isGlassCake" :disabled="disabled" active-color="#13ce66" inactive-color="#ff4949" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="到期日期:" prop="dueDate">
              <el-date-picker v-model="formData.dueDate" type="date" placeholder="请选择" format="YYYY-MM-DD" />
            </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="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="partType == '烧结'">
            <el-form-item label="是否印字:" prop="printing">
              <el-radio-group v-model="formData.printing">
                <el-radio :value="3">否</el-radio>
                <el-radio :value="6">单</el-radio>
                <el-radio :value="9">双</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="partType == '热表'">
          <el-col :span="8">
            <el-form-item label="硬度:" prop="hardness">
              <el-input v-model="formData.hardness" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="材料:" prop="material">
              <el-input v-model="formData.material" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="面积(dm²):" prop="area">
              <el-input-number :max="100000" :min="0" v-model="formData.area" precision="10" controls-position="right"
                placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="镀种:" prop="plate">
              <el-input v-model="formData.plate" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="镀层代号:" prop="plateCode">
              <el-input v-model="formData.plateCode" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="镀层厚度:" prop="plateThickness">
              <el-input v-model="formData.plateThickness" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="工艺路线:" prop="craftWay" class="auto-fit">
              <el-input v-model="formData.craftWay" type="textarea" maxlength="255" show-word-limit rows="3"
                placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <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>
          <el-col :span="16">
            <el-form-item label="色标色带:" prop="memo" class="auto-fit">
              <el-input v-model="formData.memo" :rows="3" type="textarea" maxlength="900" show-word-limit
                placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="关键信息:" prop="cruxMemo" class="auto-fit">
              <el-input v-model="formData.cruxMemo" type="textarea" maxlength="2000" show-word-limit rows="3"
                placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="工艺校验:" prop="sinTer">
              <el-checkbox v-model="formData.goldMark">镀金标识</el-checkbox>
              <el-checkbox v-model="formData.markingsTest" @change="setDataLocal">涂色标</el-checkbox>
              <el-checkbox v-model="validateObj.yingGuangJian" @change="setDataLocal"
                v-if="partType == '热表'">荧光检</el-checkbox>
              <el-checkbox v-model="validateObj.heatTreatmentTest" @change="setDataLocal"
                v-if="partType == '热表'">热处理检验</el-checkbox>
              <el-checkbox v-model="validateObj.penmark" @change="setDataLocal" v-if="partType == '热表'">喷码</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="box-card" v-if="partType == '烧结'" 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"
        @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
        @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">

      </avue-crud>
    </el-card>
  </div>
</template>
<script>
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,
    },
  },
  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,
      },
      validateObj: {
        yingGuangJian: false,
        penmark: false,
        heatTreatmentTest: 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' }],
        printing: [{ required: true, message: '请选择', trigger: 'blur' }],
        dueDate: [{ required: true, message: '请选择', trigger: 'blur' }],
        partCode: [
          { required: true },
          { validator: checkCode, 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: true, message: '请输入', trigger: 'blur' }],
        plate: [{ required: true, message: '请输入', trigger: 'blur' }],
        material: [{ 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,
        menuAlign: 'left',
        searchLabelPosition:'left',
searchLabelPosition:'left',
 searchGutter:24,
  searchSpan:6,
menuAlign: 'left',
gridBtn:false,
searchMenuPosition:'right',
addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        column: [
          {
            label: '零件号',
            prop: 'subCode',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '零件名称',
            prop: 'subName',
            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: '零件类型',
            prop: 'productType',
            search: false,
            sortable: true,
            overHidden: true,
            type: "select",
            cell: true,
            dicData: [
              {
                label: '插针',
                value: "1"
              },
              {
                label: '壳体',
                value: "2"
              },
              {
                label: '石墨模',
                value: "5"
              },
              {
                label: '自制玻璃饼',
                value: "3"
              },
              {
                label: '其他',
                value: "4"
              }
            ]
          }
        ]
      }
    };
  },
  created() {

  },
  mounted() {
    this.disabled = this.dialogType == 'view' ? true : false
    console.log(999999,this.dialogType)
  },
  methods: {
    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.heatTreatmentTest = true;
      //       }
      //       if (craftWay.indexOf('荧光检') !== -1) {
      //         this.validateObj.yingGuangJian = true;
      //       }
      //       if (craftWay.indexOf('喷码') !== -1) {
      //         this.validateObj.penmark = 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;
      this.$refs.form.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('一级路线包含涂色，请填写涂色数据');
            }
          }
          this.formData.childPage = this.childPage;
          this.formData.psId = this.psId;

          // 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();
      // }
    },
    onLoad() {
      this.data = [
        {
          $cellEdit: true,
          "area": 3.0,
          "configNo": "B1",
          "keyValue": 195827,
          "material": "XXXX",
          "memo": null,
          "partCode": "21E8-040-17188-A1",
          "plate": "XXXX",
          "plateCode": "6",
          "plateThickness": null,
          "prepared": false,
          "productType": "1",
          "psId": 195827,
          "quota": 1.0,
          "sinTerType": 5,
          "sinTerTypeTitle": "石墨模",
          "subCode": "21E2-505-522-B1",
          "subName": "设备架喷漆部件",
          "subType": "R",
          "subTypeTitle": "原材料",
          "updateMan": {
            "accountId": 622,
            "card": null,
            "createTime": "2025-06-26",
            "curStatus": 1,
            "deleted": false,
            "deptName": null,
            "dimissionTime": null,
            "factoryId": 1,
            "head": 1,
            "jobName": null,
            "keyValue": 601,
            "ldapName": "zd001",
            "mail": null,
            "mobile": null,
            "pfDepartment": {
              "appLink": null,
              "createTime": "2022-08-31",
              "deleted": false,
              "deptCode": "001",
              "deptId": 1,
              "deptName": "MES开发部",
              "deptType": 1,
              "deptTypeTitle": "厂",
              "keyValue": 1,
              "memo": null,
              "orders": "00",
              "parentDeptId": null,
              "parentPath": null,
              "ucDeptId": null,
              "updateTime": "2022-08-31 16:32:46"
            },
            "pinyinIndex": "ZHANGDI,ZD",
            "ucUserId": null,
            "updateTime": "2025-06-26 15:31:23",
            "userCode": "zd001",
            "userId": 601,
            "userName": "张迪",
            "userSex": 1,
            "userSources": "张迪",
            "userString": "601:A:张迪",
            "userType": "A"
          },
          "updateTime": "2025-07-11 17:46:06"
        },
        {
          $cellEdit: true,
          "area": 60.0,
          "configNo": "L79",
          "keyValue": 195828,
          "material": null,
          "memo": null,
          "partCode": "21E8-040-17188-A1",
          "plate": "12",
          "plateCode": null,
          "plateThickness": "3",
          "prepared": false,
          "productType": "3",
          "psId": 195828,
          "quota": 1.0,
          "sinTerType": 2,
          "sinTerTypeTitle": "壳体",
          "subCode": "21E8-570-23998-L79",
          "subName": "锌黄丙烯酸聚氨酯底漆",
          "subType": "R",
          "subTypeTitle": "原材料",
          "updateMan": {
            "accountId": null,
            "card": null,
            "createTime": "2022-08-31",
            "curStatus": 1,
            "deleted": false,
            "deptName": null,
            "dimissionTime": null,
            "factoryId": 41,
            "head": 0,
            "jobName": "总工",
            "keyValue": 1,
            "ldapName": "admin",
            "mail": "13699999999@136.com",
            "mobile": "13699999999",
            "pfDepartment": {
              "appLink": null,
              "createTime": "2022-08-31",
              "deleted": false,
              "deptCode": "001",
              "deptId": 1,
              "deptName": "MES开发部",
              "deptType": 1,
              "deptTypeTitle": "厂",
              "keyValue": 1,
              "memo": null,
              "orders": "00",
              "parentDeptId": null,
              "parentPath": null,
              "ucDeptId": null,
              "updateTime": "2022-08-31 16:32:46"
            },
            "pinyinIndex": "ADMIN,ADMIN",
            "ucUserId": null,
            "updateTime": "2022-08-31 16:32:46",
            "userCode": "00",
            "userId": 1,
            "userName": "admin",
            "userSex": 1,
            "userSources": "admin",
            "userString": "1:A:admin",
            "userType": "A"
          },
          "updateTime": "2025-09-23 13:24:24"
        },
        {
          $cellEdit: true,
          "area": 101.0,
          "configNo": "A1",
          "keyValue": 222030,
          "material": null,
          "memo": null,
          "partCode": "21E8-040-17188-A1",
          "plate": "12",
          "plateCode": null,
          "plateThickness": null,
          "prepared": false,
          "productType": "2",
          "psId": 222030,
          "quota": 1.0,
          "sinTerType": 2,
          "sinTerTypeTitle": "壳体",
          "subCode": "21ET8-571-2901-A1",
          "subName": "锌黄丙烯酸聚氨酯底漆1",
          "subType": "R",
          "subTypeTitle": "原材料",
          "updateMan": {
            "accountId": null,
            "card": null,
            "createTime": "2022-08-31",
            "curStatus": 1,
            "deleted": false,
            "deptName": null,
            "dimissionTime": null,
            "factoryId": 41,
            "head": 0,
            "jobName": "总工",
            "keyValue": 1,
            "ldapName": "admin",
            "mail": "13699999999@136.com",
            "mobile": "13699999999",
            "pfDepartment": {
              "appLink": null,
              "createTime": "2022-08-31",
              "deleted": false,
              "deptCode": "001",
              "deptId": 1,
              "deptName": "MES开发部",
              "deptType": 1,
              "deptTypeTitle": "厂",
              "keyValue": 1,
              "memo": null,
              "orders": "00",
              "parentDeptId": null,
              "parentPath": null,
              "ucDeptId": null,
              "updateTime": "2022-08-31 16:32:46"
            },
            "pinyinIndex": "ADMIN,ADMIN",
            "ucUserId": null,
            "updateTime": "2022-08-31 16:32:46",
            "userCode": "00",
            "userId": 1,
            "userName": "admin",
            "userSex": 1,
            "userSources": "admin",
            "userString": "1:A:admin",
            "userType": "A"
          },
          "updateTime": "2025-07-11 15:35:56"
        },
        {
          $cellEdit: true,
          "area": 8.0,
          "configNo": "L16",
          "keyValue": 222031,
          "material": "3",
          "memo": null,
          "partCode": "21E8-040-17188-A1",
          "plate": "12",
          "plateCode": "77",
          "plateThickness": null,
          "prepared": true,
          "productType": "4",
          "psId": 222031,
          "quota": 1.0,
          "sinTerType": 3,
          "sinTerTypeTitle": "自制玻璃饼",
          "subCode": "21E8-571-4251-L16",
          "subName": "锌黄丙烯酸聚氨酯底漆1",
          "subType": "R",
          "subTypeTitle": "原材料",
          "updateMan": {
            "accountId": null,
            "card": null,
            "createTime": "2022-08-31",
            "curStatus": 1,
            "deleted": false,
            "deptName": null,
            "dimissionTime": null,
            "factoryId": 41,
            "head": 0,
            "jobName": "总工",
            "keyValue": 1,
            "ldapName": "admin",
            "mail": "13699999999@136.com",
            "mobile": "13699999999",
            "pfDepartment": {
              "appLink": null,
              "createTime": "2022-08-31",
              "deleted": false,
              "deptCode": "001",
              "deptId": 1,
              "deptName": "MES开发部",
              "deptType": 1,
              "deptTypeTitle": "厂",
              "keyValue": 1,
              "memo": null,
              "orders": "00",
              "parentDeptId": null,
              "parentPath": null,
              "ucDeptId": null,
              "updateTime": "2022-08-31 16:32:46"
            },
            "pinyinIndex": "ADMIN,ADMIN",
            "ucUserId": null,
            "updateTime": "2022-08-31 16:32:46",
            "userCode": "00",
            "userId": 1,
            "userName": "admin",
            "userSex": 1,
            "userSources": "admin",
            "userString": "1:A:admin",
            "userType": "A"
          },
          "updateTime": "2025-07-11 15:36:02"
        }
      ]
    }
  },
};
</script>
<style lang="scss" scoped>
.clearfix {
  margin-bottom: 12px;
}

.auto-fit {
  width: 100%;
}
</style>