<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :visible.sync="dialogVisible"
    width="600px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="dataForm" :rules="dataRule" size="small" label-width="100px" label-position="right">
      <template v-if="!loading">
        <el-form-item label="工序编码" prop="procCd">
          <JnpfSelect v-model="dataForm.procCd" placeholder="请选择工序" :options="procOptions" :props="procProps" clearable :disabled="!!dataForm.id" :style="{ width: '100%' }">
          </JnpfSelect>
        </el-form-item>
        <el-form-item label="物料编码" prop="matCode">
          <JnpfSelect v-model="dataForm.matCode" placeholder="请选择物料" :options="materialOptions" :props="materialProps" clearable :style="{ width: '100%' }">
          </JnpfSelect>
        </el-form-item>
        <el-form-item label="规格型号" prop="spec">
          <JnpfInput v-model="dataForm.spec" placeholder="请输入规格型号" clearable :style="{ width: '100%' }">
          </JnpfInput>
        </el-form-item>
        <el-form-item label="机台编码" prop="machineCd">
          <JnpfSelect v-model="dataForm.machineCd" placeholder="请选择机台" :options="machineOptions" :props="machineProps" clearable :disabled="!!dataForm.id" :style="{ width: '100%' }">
          </JnpfSelect>
        </el-form-item>
        <el-form-item label="状态" prop="enabledStatus">
          <JnpfSelect v-model="dataForm.enabledStatus" placeholder="请选择状态" :options="enabledStatusOptions" :props="enabledStatusProps" clearable :style="{ width: '100%' }">
          </JnpfSelect>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <JnpfTextarea v-model="dataForm.remark" placeholder="请输入备注" :style="{ width: '100%' }" :autoSize="{ minRows: 4, maxRows: 4 }" type="textarea">
          </JnpfTextarea>
        </el-form-item>
      </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">保 存</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { mapGetters } from 'vuex'

export default {
  components: {},
  props: [],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      btnLoading: false,
      formRef: 'formRef',
      eventType: '',
      dataForm: {
        id: '',
        procCd: undefined,
        procName: undefined,
        procId: undefined,
        matCode: undefined,
        matName: undefined,
        mateId: undefined,
        spec: undefined,
        machineCd: undefined,
        machineName: undefined,
        machineId: undefined,
        enabledStatus: 1,
        remark: undefined,
      },
      dataRule: {
        procCd: [
          {
            required: true,
            message: '请选择工序',
            trigger: 'change',
          },
        ],
        matCode: [
          {
            required: true,
            message: '请选择物料',
            trigger: 'change',
          },
        ],
        machineCd: [
          {
            required: true,
            message: '请选择机台',
            trigger: 'change',
          },
        ],
        enabledStatus: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change',
          },
        ],
      },
      enabledStatusOptions: [
        { fullName: "启用", id: 1 },
        { fullName: "未启用", id: 2 },
      ],
      enabledStatusProps: { label: "fullName", value: "id" },
      procList: [],
      materialList: [],
      machineList: [],
      procOptions: [],
      materialOptions: [],
      machineOptions: [],
      procProps: { label: "label", value: "value" },
      materialProps: { label: "label", value: "value" },
      machineProps: { label: "label", value: "value" },
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {
    this.loadBaseData();
  },
  methods: {
    init(id) {
      this.dataForm.id = id || '';
      this.dialogVisible = true;
      this.loading = true;
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.resetFields();
        }

        if (this.dataForm.id) {
          request({
            url: `/api/example/machineparam/${this.dataForm.id}`,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data;
            this.loading = false;
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
          this.resetFormData();
        }
      });
    },
    resetFormData() {
      this.dataForm = {
        id: '',
        procCd: undefined,
        procName: undefined,
        procId: undefined,
        matCode: undefined,
        matName: undefined,
        mateId: undefined,
        spec: undefined,
        machineCd: undefined,
        machineName: undefined,
        machineId: undefined,
        enabledStatus: 1,
        remark: undefined,
      };
    },
    loadBaseData() {
      this.loadProcList();
      this.loadMaterialList();
      this.loadMachineList();
    },
    loadProcList() {
      request({
        url: `/api/example/proc/getProcList`,
        method: 'get'
      }).then(res => {
        this.procList = res.data || [];
        this.procOptions = this.procList.map(item => ({
          label: `${item.procCd} - ${item.procName}`,
          value: item.procCd,
          procName: item.procName,
          id: item.id
        }));
      });
    },
    loadMaterialList() {
      request({
        url: `/api/example/material/getMaterialList`,
        method: 'get'
      }).then(res => {
        this.materialList = res.data || [];
        this.materialOptions = this.materialList.map(item => ({
          label: `${item.matCode} - ${item.matName}`,
          value: item.matCode,
          matName: item.matName,
          id: item.id,
          spec: item.spec
        }));
      });
    },
    loadMachineList() {
      request({
        url: `/api/example/machine/getMachineList`,
        method: 'get'
      }).then(res => {
        this.machineList = res.data || [];
        this.machineOptions = this.machineList.map(item => ({
          label: `${item.machineCd} - ${item.machineName}`,
          value: item.machineCd,
          machineName: item.machineName,
          id: item.id
        }));
      });
    },
    dataFormSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          const isEdit = !!this.dataForm.id;
          const url = isEdit ? `/api/example/machineparam/${this.dataForm.id}` : '/api/example/machineparam';
          const method = isEdit ? 'put' : 'post';

          const proc = this.procOptions.find(item => item.value === this.dataForm.procCd);
          const material = this.materialOptions.find(item => item.value === this.dataForm.matCode);
          const machine = this.machineOptions.find(item => item.value === this.dataForm.machineCd);

          const submitData = {
            ...this.dataForm,
            procName: proc ? proc.procName : undefined,
            procId: proc ? proc.id : undefined,
            matName: material ? material.matName : undefined,
            mateId: material ? material.id : undefined,
            machineName: machine ? machine.machineName : undefined,
            machineId: machine ? machine.id : undefined,
          };

          request({
            url: url,
            method: method,
            data: submitData
          }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.btnLoading = false;
                this.dialogVisible = false;
                this.$emit('refresh', true)
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        }
      })
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit('refresh', false)
    },
  }
}
</script>
