<template>
  <div>
    <el-card>
      <el-form  :model="inputQuery"  label-width="120px" >
        <el-row>
          <el-col :span="20" >
            <el-form-item label="当前仓库:">
              <el-select v-model="inputQuery.curInv" @change="getBusType" placeholder="请选择当前仓库信息" style="width: 90%" clearable>
                <el-option
                  v-for="item in invList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code"
                >
                  <span style="float: left">{{ item.name }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{
                      item.code
                    }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20" >
            <el-form-item label="往来单位:">
              <el-select v-model="inputQuery.fromCorp" placeholder="请选择往来单位"  clearable style="width: 90%">
                <el-option
                  v-for="item in fromInvOptions"
                  :key="item.erpId"
                  :label="item.name"
                  :value="item.erpId"
                >
                  <span style="float: left">{{ item.name }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{item.code}}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20" >
            <el-form-item label="生成单据类型:">
              <el-select v-model="inputQuery.targetAction" placeholder="请选择单据类型" style="width: 90%" clearable>
                <el-option
                  v-for="item in busTypes"
                  :key="item.name"
                  :label="item.name"
                  :value="item.action">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20" >
            <el-form-item label="是否个人使用:">
              <el-radio-group v-model="inputQuery.checkUse">
                <el-radio :label=1>是</el-radio>
                <el-radio :label=0>否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col :span="20" >
          <el-form-item label="备注:">
            <el-input type="textarea" :rows="3" v-model="inputQuery.remark" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
        <div style='text-align: center; margin-bottom: 10px;margin-top: 18px ;'>
          <el-button  @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitInv">提交</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {getLocalJoinByUser} from "@/api/basic/busType";
import {filterSubAll} from "@/api/system/invSubWarehouse";
import {selectCorpList} from "@/api/basic/basicUnitMaintain"
import {addMuti,updateMuti} from "@/api/inout/orderMuti";

export default {
  name: "ioOrderMutiSetDialog",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    formName: {
      type: Object,
      required: true,
    },
    inputQuery: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      invList:[],
      fromInvOptions: [],
      busTypes: [],
    }
  },
  methods: {
    getInvList() {
      filterSubAll()
        .then((response) => {
          this.invList = response.data || [];
        })
        .catch(() => {
        });
    },
    findFromInvList(val) {
      let cQuery = {
        corpType: 4,
      };
      selectCorpList(cQuery)
        .then((response) => {
          this.fromInvOptions = response.data || [];
        })
        .catch(() => {
        });
    },
    getBusType() {
      let query = {
        code: this.inputQuery.curInv,
        enabled: true,
      };
      getLocalJoinByUser(query)
        .then((response) => {
          this.busTypes = response.data.list || [];
        })
        .catch(() => {
        });
    },
    submitInv(){
      if(this.$isBlank(this.inputQuery.curInv)){
        this.$message.error("当前仓库不能为空！");
        return
      }
      if(this.$isBlank(this.inputQuery.fromCorp)){
        this.$message.error("往来单位不能为空！");
        return
      }
      if(this.$isBlank(this.inputQuery.targetAction)){
        this.$message.error("生成单据类型不能为空！");
        return
      }
      if(this.formName==1){
        addMuti(this.inputQuery).then((response) => {
          if(response.code == 20000){
            this.closeDialog();
          }else{
            this.$message.error(response.message);
          }

        })
          .catch(() => {
            this.$message.error("添加失败");
          });
      }else{
        updateMuti(this.inputQuery).then((response) => {
          if(response.code == 20000){
            this.closeDialog();
          }else{
            this.$message.error(response.message);
          }

        })
          .catch(() => {
            this.$message.error("添加失败");
          });
      }
    },

  },
  components: {},
  created() {
    this.getInvList();
    this.findFromInvList();
    if(this.formName==2){
     this.getBusType();
    }else{
      this.inputQuery.checkUse=1;
      console.log(this.inputQuery)
    }

  },
}
</script>

<style scoped>

</style>
