<template>
  <j-modal
    :title="title"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk2"
    @cancel="handleCancel"
    cancelText="关闭">

<!--    <a-spin :spinning="confirmLoading">-->
<!--      <a-form-model ref="form" :model="model" :rules="validatorRules">-->
<!--        &lt;!&ndash;        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationId" label="车间工位编号">&ndash;&gt;-->
<!--        &lt;!&ndash;          <j-select-depart placeholder="请选择工位" v-model="model.stationId"/>&ndash;&gt;-->
<!--        &lt;!&ndash;        </a-form-model-item>&ndash;&gt;-->
<!--        &lt;!&ndash;        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationId" label="工位ID">&ndash;&gt;-->
<!--        &lt;!&ndash;          <a-input placeholder="请输入工位ID" v-model="model.stationId" />&ndash;&gt;-->
<!--        &lt;!&ndash;        </a-form-model-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineId" label="设备">&ndash;&gt;-->
<!--&lt;!&ndash;          &lt;!&ndash;          <a-input placeholder="请选择设备" v-model="model.machineId" />&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;          <j-select-depart placeholder="请选择设备" v-model="model.machineId"/>&ndash;&gt;-->
<!--&lt;!&ndash;        </a-form-model-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <a-form-model-item prop="machineId" label="设备">-->
<!--          <j-dict-select-tag v-model="model.machineId" dictCode="zy_devicetype,name,id" placeholder="请选择设备" />-->
<!--        </a-form-model-item>&ndash;&gt;-->
<!--      </a-form-model>-->
<!--    </a-spin>-->

      <ZyDevicetypeListRef ref="modalForm2"></ZyDevicetypeListRef>
  </j-modal>
</template>

<script>
import {httpAction} from '@/api/manage'
import moment from "moment"
import ZyDevicetypeListRef from '@views/devicetype/ZyDevicetypeListRef'

export default {
  name: "StationMachineModal",
  components:{
    ZyDevicetypeListRef
  },
  data() {
    return {
      title: "操作",
      visible: false,
      model: {
        stationId: '',
        machineId:[],
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },

      confirmLoading: false,
      validatorRules: {
        machineId: [
          {required: true},
        ],
      },
      url: {
        add: "/stationMachine/add",
        addBatch: "/stationMachine/addBatch",
        edit: "/stationMachine/edit",
      },
    }
  },
  created() {
  },
  methods: {
    add(stationId) {
      this.model.stationId = stationId;
      console.log("Modal层接收到上级参数-stationId:" + this.model.stationId);
      this.edit(this.model);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    close() {
      this.$emit('close');
      this.visible = false;
      this.$refs.form.clearValidate();
    },

    handleOk2() {
      this.model.machineId = this.$refs["modalForm2"].selectedRowKeys.toString();
      const that = this;
      // 触发表单验证
      // this.$refs.form.validate(valid => {
      //   if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.addBatch;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
      //   } else {
      //     return false;
      //   }
      // })
    },

    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        } else {
          return false;
        }
      })
    },
    handleCancel() {
      this.close()
    },


  }
}
</script>

<style lang="less" scoped>

</style>