<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> -->
      <template v-for="(itemRow, indexRow) in fromList">
          <el-row :gutter="20" class="el-row" type="flex">
               <template v-for="(item, index) in itemRow.list">
                 <el-col :span="item.width" class="el-col" type="flex">
                   <div class="text item">
                    <el-form-item v-if="item.columnType =='input'"  :rules="item.checkRulesObj" :prop="item.columnName">
                        <span slot="label">
                          {{item.columnDesc}}
                        </span>
                      <el-input
                        v-model="inputQuery[item.columnName]"
                        :style="item.style"
                        :size="item.size"
                        :type="item.inputType"
                        :placeholder="item.columnDesc"
                        :disabled="item.disabled"
                          @change="executeFuc($event,item,'2')"
                      ></el-input>
                    </el-form-item>

                     <el-form-item v-if="item.columnType =='radio'" :prop="item.columnName">
                        <span slot="label">
                          {{item.columnDesc}}
                        </span>
                       <el-radio-group :style="item.style" v-model="inputQuery[item.columnName]">
                         <el-radio
                           v-for="dict in item.lableRuleObj"
                           :key="parseInt(dict.value)"
                           :label="parseInt(dict.value)"
                           :disabled="item.disabled"
                         >{{dict.label}}</el-radio>
                       </el-radio-group>
                     </el-form-item>

                     <el-form-item v-if="item.columnType =='select'" :prop="item.columnName">
                        <span slot="label">
                          {{item.columnDesc}}
                        </span>
                       <el-select v-model="inputQuery[item.columnName]" :style="item.style">
                         <el-option
                           v-for="dict in item.lableRuleObj"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"
                         />
                       </el-select>
                     </el-form-item>
                     <el-form-item v-if="item.columnType =='selectServer'" :prop="item.columnName">
                        <span slot="label">
                          {{item.columnDesc}}
                        </span>
                       <el-select
                         v-model="inputQuery[item.columnName]"
                         :placeholder="item.columnDesc"
                         @change="executeFuc($event,'5',item.clickFuc)"
                         :disabled="executeEval(null,item.disabledFuc,false)"
                         filterable
                         remote
                         :remote-method="(query) => executeFuc(query,'5',item.dataFuc)"
                         clearable>
                         <el-option
                           v-for="item in options[item.dataFuc]"
                           :key="item.code"
                           :label="item.label"
                           :value="item.code"
                         >
                         <span style="float: left">{{ item.label }}</span>
                         <span style="float: right; color: #8492a6; font-size: 13px">{{
                             item.code
                           }}</span>
                         </el-option>
                       </el-select>
                     </el-form-item>
                     <el-form-item v-if="item.columnType =='date'"  :rules="item.checkRulesObj" :prop="item.columnName">
                        <span slot="label">
                          {{item.columnDesc}}
                        </span>
                       <el-date-picker
                         v-model="inputQuery[item.columnName]"
                         :style="item.style"
                         value-format="yyyy-MM-dd"
                         type="date"
                         :placeholder="item.columnDesc"
                       ></el-date-picker>
                     </el-form-item>
                     <el-form-item v-if="item.columnType =='treeCustom'" :prop="item.columnName">
                        <span slot="label">
                          {{item.columnDesc}}
                        </span>
                       <treeselect
                         v-model="inputQuery[item.columnName]"
                         :options="menuOptions"
                         :normalizer="normalizer"
                         :show-count="true"
                         :placeholder="item.columnName"
                       />
                     </el-form-item>
                   </div>
                 </el-col>
              </template>
         </el-row>
      </template>
        <div style='text-align: center; margin-bottom: 10px;margin-top: 18px ;'>
          <el-button  @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitInv" :loading="submitLoading">提交</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {getLocalJoinByUserOptimize} from "@/api/basic/busType";
import {filterSubAllOptimize} from "@/api/system/invSubWarehouse";
import {selectCorpListOptimize} from "@/api/basic/basicUnitMaintain"
import {addMuti,updateMuti} from "@/api/inout/orderMuti";
import {
  getHead,executeFuc
} from "@/utils/customConfig";

export default {
  name: "ioOrderMutiSetDialog",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    formName: {
      type: Object,
      required: true,
    },
    inputQuery: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      tableHeader:[],
      queryList:[],
      fromList:[],
      tableObj:[],
      invList:[],
      options: {
        findFromInvList:[],
        getInvList:[],
        getBusType:[],
      },
      fromInvOptions: [],
      busTypes: [],
      submitLoading: false,
    }
  },
  methods: {
    getInvList(_this) {
      filterSubAllOptimize()
        .then((response) => {
          _this.options.getInvList = response.data || [];
        })
        .catch(() => {
        });
    },
    findFromInvList(_this,val) {
      let cQuery = {
        corpType: 4,
      };
      selectCorpListOptimize(cQuery)
        .then((response) => {
          _this.options.findFromInvList = response.data || [];
        })
        .catch(() => {
        });
    },
    getBusType(_this) {
      let query = {
        code: _this.inputQuery.curInv,
        enabled: true,
      };
      getLocalJoinByUserOptimize(query)
        .then((response) => {
          _this.options.getBusType = response.data || [];
        })
        .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
      }
      this.submitLoading = true
      if(this.formName==1){
        addMuti(this.inputQuery).then((response) => {
          this.submitLoading = false
          if(response.code == 20000){
            this.closeDialog();
          }else{
            this.$message.error(response.message);
          }

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

        })
          .catch(() => {
            this.submitLoading = false
            this.$message.error("添加失败");
          });
      }
    },
    executeFuc(row,type,clickFuc,value){
         return executeFuc(this,row,type,clickFuc,value);
       },
     executeEval(row,expression,defaultRet){
       if(expression){
         return eval(expression);
       }
       return defaultRet;
     }
  },
  components: {},
  created() {
    getHead("ioOrderMutiSet","1").then((re) => {
      // 处理返回的数据
      this.tableObj = re.data;
      this.tableHeader = re.data.tableList;
      this.queryList = re.data.queryList;
      this.fromList = re.data.fromList;
    });

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

  },
}
</script>

<style scoped>

</style>
