<template>
  <el-form :model="generateOrderDate" label-width="120px">
    <el-row type="flex">
      <el-col :span="11" type="flex">
        <el-form-item label="来源单据号:" prop="code">
          <el-input style="width: 90%" size="small" placeholder="请输入来源单据号"
                    disabled
                    v-model.trim="generateOrderDate.billNo"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="11">
        <el-form-item prop="invCode" label="当前仓库:">
          <el-select
            v-model="generateOrderDate.invCode"
            placeholder="当前仓库"
            style="width: 90%"
            :disabled="corpOrderIdDisabled"
            @change="changeInv"
            filterable
          >
            <el-option
              v-for="item in curInvOptions"
              :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.warehouseName
                }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="11">
        <el-form-item prop="action" label="单据类型:">
          <el-select
            v-model="generateOrderDate.action"
            placeholder="请选择单据类型"
            :disabled="corpOrderIdDisabled"
            style="width: 90%"
            filterable
            @change="actionChange"
          >
            <el-option
              v-for="item in busTypeOptions"
              :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 type="flex" v-if="curAction.corpType != 1">
      <el-col :span="11">
        <el-form-item
          prop="fromCorp"
          label="往来信息:"
        >
          <el-select
            v-model="generateOrderDate.fromCorp"
            filterable
            remote
            clearable
            reserve-keyword
            style="width: 90%"
            placeholder="请输入往来信息"
            :remote-method="findMethod"
            :loading="loading"
            :disabled="corpOrderIdDisabled"
          >
            <el-option
              v-for="item in fromOptions"
              :key="item.erpId"
              :label="item.name"
              :value="item.erpId"
            >
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>


    <el-row type="flex" v-if="curAction.corpType == 1">
      <el-col :span="11">
        <el-form-item
          prop="fromCorp"
          label="患者信息:"
        >
          <el-input style="width: 90%" size="small" placeholder=""
                    disabled
                    v-model.trim="generateOrderDate.fromCorp"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <div style="text-align: center; margin-bottom: 10px;">
      <el-button type="primary" @click="generateOrder">提交</el-button>
      <el-button type="primary" @click="closeAddDialog">取消</el-button>
    </div>

  </el-form>
</template>

<script>
import {generateOrder, generateGhOrder} from '@/api/thrsys/thrInvOrder'
import {findByFrom, findInvByUser} from '../../../api/system/invSubWarehouse'
import {parseTime} from '../../../utils/coTools'
import {findByInvUser, getBusTypeList} from '@/api/basic/busType'
import {getBasicUnitMaintains} from '../../../api/basic/basicUnitMaintain'

export default {
  name: 'thrInvOrderGenerate',
  props: {
    generateOrderDate: {
      type: Object,
      required: true
    },
    closeAddDialog: {
      type: Function,
      required: true
    },
    originTypes: {
      type: Object,
      required: true
    },
    sourceType: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      thirdSys: [],
      options: [{
        value: 1,
        label: '客户信息'
      }, {
        value: 2,
        label: '供应商信息'
      },
        {
          value: 3,
          label: '内部科室'
        }, {
          value: 4,
          label: '特殊往来'
        }],

      curInvOptions: [],
      fromOptions: [],
      busTypeOptions: [],
      fromInvOptions: [],
      curPreInInvOptions: [],    //预验收仓库
      curPreInSpaceOptions: [],  //预验收货位
      curPreSpaceOptions: [],    //寄售货位
      curSpaceOptions: [], //当前货位
      curAction: {
        corpType: 0,
        genUnit: false,
        checkWebNew: null,
        mainAction: null
      },
      loading: false,
    }

  },
  methods: {
    //提交
    generateOrder() {
      if (this.sourceType === 1 || this.sourceType === 2) {
        generateOrder(this.generateOrderDate).then((res) => {
          if (res.code == 20000) {
            this.$message.success('生成成功!')
            //清空弹窗数据
            this.$emit('closeAddDialog', true)
            this.closeAddDialog(true)
          } else {
            this.$message.error(res.message)
          }
        }).catch((error) => {
          this.$message.error(error.message)
        })
      } else if (this.sourceType === 3) {
        generateGhOrder(this.generateOrderDate).then((res) => {
          if (res.code == 20000) {
            this.$message.success('生成成功!')
            //清空弹窗数据
            this.$emit('closeAddDialog', true)
            this.closeAddDialog(true)
          } else {
            this.$message.error(res.message)
          }
        }).catch((error) => {
          this.$message.error(error.message)
        })
      }
    },
    //获取用户仓库列表
    findInvListByUser() {
      this.curInvOptions = []
      findInvByUser()
        .then((response) => {
          this.curInvOptions = response.data || []
          if (
            this.curInvOptions != null &&
            this.curInvOptions.length > 0 &&
            this.generateOrderDate.invWarehouseCode == null
          ) {
            //this.orderFormData.invCode = this.curInvOptions[0].code;
            this.getBusTypeByInv()
          }
        })
        .catch(() => {
        })
    },
    //仓库改变
    changeInv(row) {
      this.generateOrderDate.action = null
      this.generateOrderDate.checkPreInOrders = null
      this.checkPreInArray = []
      if (this.documentShow) {
        this.documentShow = false
      }

      this.curSpaceShow = false
      this.preInSpaceShow = false
      this.preSpaceShow = false
      this.generateOrderDate.curSpaceCode = false
      this.generateOrderDate.checkPreInSpaceCode = false
      this.generateOrderDate.preCurSpaceCode = false
      this.getBusTypeByInv()
    },
    //单据类型改变
    actionChange(item) {
      this.generateOrderDate.checkPreInOption = null
      this.generateOrderDate.checkPreInOrders = null
      this.checkPreInArray = []
      this.curAction = this.getActionItem(item)
      this.bizShow = false
      //2. 切换往来仓库
      this.generateOrderDate.fromInvCode = null
      this.findMethod()
    },
    //根据仓库,用户获取单据类型
    getBusTypeByInv(key) {
      let query = {
        code: this.generateOrderDate.invCode,
        action: key,//可编辑
      }
      findByInvUser(query)
        .then((response) => {
          this.busTypeOptions = response.data || []
          this.curAction = this.getActionItem(this.generateOrderDate.action)
        })
        .catch(() => {
        })
    },
    //获取当前单据类型
    getActionItem(action) {
      for (let i = 0; i < this.busTypeOptions.length; i++) {
        if (this.busTypeOptions[i].action == action) {
          return this.busTypeOptions[i]
        }
      }
    },
    //获取往来单位候选列表
    findMethod(query) {
      this.fromOptions = []
      let cQuery = {
        key: query,
        corpType: this.curAction.corpType,
        outType: null,
        page: 1,
        limit: 20
      }
      if (this.curAction.corpType == 0) {
        cQuery.corpType = null;
      }
      getBasicUnitMaintains(cQuery)
        .then((response) => {
          this.loading = false
          this.fromOptions = response.data.list || []
        })
        .catch(() => {
          this.loading = false
        })
    }

  },
  created() {
    //页面初始化
    this.generateOrderDate.code = ''
    this.generateOrderDate.orderType = this.viewType
    //新增页面
    this.corpOrderIdDisabled = false
    let date = new Date()
    this.generateOrderDate.createTime = parseTime(
      date,
      '{y}-{m}-{d} {h}:{i}:{s}'
    )

    this.generateOrderDate.corpOrderId =
      parseTime(date, '{y}{m}{d}{h}{i}{s}') +
      Math.ceil(Math.random() * 89 + 10)
    this.generateOrderDate.action = this.generateOrderDate.billType;
    this.getBusTypeByInv(this.generateOrderDate.billType);
    this.findInvListByUser()
    this.findMethod(this.generateOrderDate.fromCorp)

  }
}
</script>

<style scoped>

</style>
