<template>
  <div>
    <el-form :model="editLogin" label-width="120px" ref="editLogin">
      <el-row :gutter="20" class="el-row" type="flex">
        <el-col :span="15" class="el-col" type="flex">
          <div class="text item">
            <el-form-item label="本地单据类型" prop="sourceAction">
              <el-select
                  v-model="editLogin.sourceAction"
                  style="width: 100%"
                  filterable
                  placeholder="请选择本地单据类型"
              >
                <el-option
                    v-for="item in bussinessTypes"
                    :key="item.action"
                    :label="item.name"
                    :value="item.action"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="el-row" type="flex">
        <el-col :span="15" class="el-col" type="flex">
          <div class="text item">
            <el-form-item label="目标部门" prop="invCode">
              <el-select v-model="editLogin.invCode"
                         style="width: 100%;"
                         placeholder="目标部门信息"
                         @change="locInvChange"
              >
                <el-option
                    v-for="item in targetInvs"
                    :key="item.name"
                    :label="item.name"
                    :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="el-row" type="flex">
        <el-col :span="15" class="el-col" type="flex">
          <div class="text item">
            <el-form-item label="目标仓库" prop="invSubCode">
              <el-select v-model="editLogin.invSubCode" placeholder="目标仓库信息"
                         style="width: 100%;" @change="locSubInvChange" filterable
              >
                <el-option
                    v-for="item in targetSubInvs"
                    :key="item.name"
                    :label="item.name"
                    :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="el-row" type="flex">
        <el-col :span="15" class="el-col" type="flex">
          <div class="text item">
            <el-form-item label="目标单据类型" prop="targetAction">
              <el-select
                  v-model="editLogin.targetAction"
                  style="width: 100%"
                  filterable
                  placeholder="请选择目标单据类型"
                  @change="targetActionChange"
              >
                <el-option
                    v-for="item in targetActions"
                    :key="item.action"
                    :label="item.name"
                    :value="item.action"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click.native.stop="cancelDialog" size="small">取消</el-button>
      <el-button type="primary" size="small" @click.native="linkSubmit()">提交</el-button>
    </div>
  </div>
</template>

<script>
import {
  getLinkPlatformList,
  getPlatform,
  getTargetActions,
  getTargetInv,
  getTargetSubInv, linkPlatform, testUserInfo, unbindPlatform
} from "@/api/userManage/platform";
import {
  getBasicUnitMaintains,
  getBasicUnitMaintains2,
  getCorpLists,
  selectCorpList
} from "@/api/basic/basicUnitMaintain";
import {getBussinessType} from "@/api/basic/bussinessType";
import {addUnit, modifyUnit} from "@/api/userManage/unitMaintain";
import {isBlank} from "@/utils/strUtil";

export default {
  name:'bindPlatformEditModel',
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    editQuery: {
      type: Object,
      required: true,
    },
    formQuery: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      editLogin: {
        id: null,
        unitId: null,
        name: null,
        corpName: null,
        platformId: null,
        platformUsername: null,
        platformPassword: null,
        appid: null,
        appKey: null,
        sceretKey: null,
        sourceAction: null,
        targetAction: null,
        platformList: [],
        invCode: "",
        invSubCode: "",
      },
      editDialogVisible: false,
      corpNamesList: [],
      curPlatformId: "",
      targetPlatformEdit: true, //目标平台启用编辑
      editPlatformQuery: {
        unitId: null,
        name: null,
        host: null
      },
      platformList: [],
      bussinessTypes: [],
      targetActions: [],
      targetInvs: [],
      targetSubInvs: [],

    };
  },

  methods: {
    getLocalBussinessType() {
      let tQuery = {};
      getBussinessType(tQuery).then((res) => {
        this.bussinessTypes = res.data.list || [];
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    getTargetInv() {
      this.targetActions = [];
      let query = {
        platformId: this.editLogin.platformId,
        apiKey: this.editLogin.apiKey,
        apiSecret: this.editLogin.secretKey,
      };
      getTargetInv(query).then((res) => {
        this.targetInvs = res.data || [];
      }).catch((error) => {
        this.$message.error("获取自助平台单据类型失败");
      });
    },
    locInvChange() {
      this.editLogin.invName = this.targetInvs.find(item => item.code == this.editLogin.invCode).name
      this.editLogin.invSubCode=null
      this.editLogin.targetAction=null
      this.getTargetSubInv();
    },
    getTargetSubInv() {
      this.targetActions = [];
      let query = {
        invCode: this.editLogin.invCode,
        platformId: this.editLogin.platformId,
        apiKey: this.editLogin.apiKey,
        apiSecret: this.editLogin.secretKey,
      };
      getTargetSubInv(query).then((res) => {
        this.targetSubInvs = res.data || [];
      }).catch((error) => {
        this.$message.error("获取自助平台单据类型失败");
      });
    },
    locSubInvChange() {
      this.editLogin.invSubName = this.targetSubInvs.find(item => item.code == this.editLogin.invSubCode).name
      this.editLogin.targetAction=null
      this.getTargetActions();
    },
    getTargetActions() {
      this.targetActions = [];
      let query = {
        platformId: this.editLogin.platformId,
        invSubCode: this.editLogin.invSubCode,
        apiKey: this.editLogin.apiKey,
        apiSecret: this.editLogin.secretKey,
      };
      getTargetActions(query).then((res) => {
        this.targetActions = res.data || [];
      }).catch((error) => {
        this.$message.error("获取自助平台单据类型失败");
      });
    },
    targetActionChange() {
      this.editLogin.targetName = this.targetActions.find(item => item.action == this.editLogin.targetAction).name
    },
    linkSubmit() {

      if(isBlank(this.editLogin.sourceAction)){
          return this.$message.error("本地单据类型不能为空");
      }
      if(isBlank(this.editLogin.invCode)){
        return this.$message.error("目标部门不能为空");
      }
      if(isBlank(this.editLogin.invSubCode)){
        return this.$message.error("目标仓库不能为空");
      }
      if(isBlank(this.editLogin.targetAction)){
        return this.$message.error("目标单据类型不能为空");
      }


      linkPlatform(this.editLogin)
          .then((response) => {
            if (response.code == 20000) {
              this.$message({
                type: "success",
                message: "关联成功"
              });
              this.cancelDialog();
            } else {
              this.$message.error(response.message);
            }
          })
          .catch(() => {
          });
    },
    cancelDialog() {
      this.closeDialog();
    }
  },
  created() {
    if(this.formQuery==1){
      this.editLogin.platformId = this.editQuery.platformId;
      this.editLogin.appid = this.editQuery.appid;
      this.editLogin.apiKey = this.editQuery.apiKey;
      this.editLogin.secretKey = this.editQuery.secretKey;
      this.editLogin.unitId = this.editQuery.unitId;
      this.editLogin.customerId = this.editQuery.customerId
      this.getLocalBussinessType()
      this.getTargetInv()
    }else{
      this.editLogin.id = this.editQuery.id;
      this.editLogin.platformId = this.editQuery.platformId;
      this.editLogin.appid = this.editQuery.appid;
      this.editLogin.apiKey = this.editQuery.apiKey;
      this.editLogin.secretKey = this.editQuery.secretKey;
      this.editLogin.unitId = this.editQuery.unitId;
      this.editLogin.customerId = this.editQuery.customerId
      this.editLogin.sourceAction = this.editQuery.sourceAction
      this.editLogin.invCode = this.editQuery.invCode
      this.editLogin.invSubCode = this.editQuery.invSubCode
      this.editLogin.targetAction = this.editQuery.targetAction
      this.getLocalBussinessType()
      this.getTargetInv()
      this.getTargetSubInv();
      this.getTargetActions();
    }
  }
};
</script>

<style scoped>

</style>
