<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    :append-to-body="true"
    :visible.sync="visible"
    :close-on-click-modal="false"
    width="60%"
    @close="cancel"
    :maxable="true"
    :resizable="true"
    :title="title"
  >
    <headerTitle title="基本信息"></headerTitle>

    <el-form
      label-width="120px"
      ref="baseInfoRef"
      :model="form"
      :rules="rules"
      class="el-form-box"
    >
      <el-row>
        <!-- <el-col :span="8">
          <el-form-item label="物品名称：" prop="categoryName">
            <el-input
              placeholder="请输入"
              v-model="form.categoryName"
              maxlength="50"
              :disabled="type == 'view'"
            ></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="样品类型：" prop="conditionType">
            <el-select
              v-model="form.conditionType"
              placeholder="请选择物品类型"
              width="100%"
              :disabled="type == 'detail'"
            >
              <el-option
                v-for="item in sampleList"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="样品编码：">
            <el-input
              disabled
              v-model="form.sampleCode"
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <headerTitle title="样品列表"></headerTitle>
    <sampleTable
      ref="inventoryTable"
      :pricingWay="form.pricingWay"
      :isSinglePrice="false"
      :isTotalCount="false"
      :isDiscount="false"
      :isTemporary="true"
      :dataList="dataList"
      pageName="businessOpportunity"
      @changesMainNumberModal="changesMainNumberModal"
    ></sampleTable>

    <div slot="footer" class="footer">
      <el-button type="primary" @click="save('baseInfoRef')">保存</el-button>

      <el-button @click="cancel">返回</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import sampleTable from './sampleTable.vue';
  import { getCode } from '@/api/login';
  export default {
    components: {
      sampleTable
    },
    mixins: [dictMixins],

    data() {
      return {
        imgs: [],
        title: '',
        visible: false,
        type: '',
        fullscreen: false,
        form: {
          pricingWay: [],
          conditionType: '',
          categoryCode: ''
        },
        // form: copyObj(formDef),
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        gList: [], //商品分类
        levelList: [],
        sampleList: [
          { id: 1, label: '整样' },
          { id: 2, label: '小样' }
        ],
        rules: {
          conditionType: [
            { required: true, message: '请选择商品分类', trigger: 'change' }
          ]
        },
        dataList: []
      };
    },

    methods: {
      //打开弹出框
      async open(type, row, contactCategoryId) {
        // this.$refs.inventoryTable.resetTable()
        // this.form.categoryLevelId = contactCategoryId;
        this.title =
          type === 'add' ? '新增' : type === 'view' ? '详情' : '修改';
        this.type = type;
        this.visible = true;
        // if (type == 'add') {
        //   this.isUpdate = false;
        // } else {
        //   this.isUpdate = true;
        // }
        // if (type != 'add') {
        //   // this.getGoodsById(row.id);
        // }
      },
      cancel() {},
      save(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dataList.length > 0) {
              
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      async changesMainNumberModal(id, data) {
        // console.log(data.code);

        const newData = {};
        data.sampleCode = await this.getSampleCode();

        console.log(data.sampleCode);
        this.form.sampleCode = data.sampleCode;

        newData.sampleCode = data.sampleCode;
        newData.categoryCode = data.code;
        newData.categoryName = data.name;
        newData.batchNo = data.brandNum;
        newData.measureUnit = data.measuringUnit;
        newData.weightUnit = data.weightUnit;
        newData.weight = data.netWeight;

        this.dataList.push(newData);

        // if (data.measureType == 1) {
        //   newData.measureUnit = data.measuringUnit;
        // } else if (data.measureType == 2) {
        //   newData.measureUnit = data.weightUnit;
        // } else if (data.measureType == 3) {
        //   newData.measureUnit = data.weightUnit;
        // } else if (data.measureType == 4) {
        // } else if (data.measureType == 5) {
        // }

        // this.dataList.push(data);
        // console.log(id, list);
        // this.form.categoryCode = data.sampleCode;
      },
      async getSampleCode() {
        return await getCode('sample_code');
      }
    }
  };
</script>

<style></style>
