<template>
  <el-card style="margin: 5px;margin-top: -15px">
    <el-form
      :model="setFormDat"
      :rules="formRules"
      ref="dataForm"
      label-width="110px"
      style="margin-bottom: -15px;margin-top: 15px"
    >
      <el-row class="order-el-row">
        <el-col :span="11">
          <el-form-item prop="invName" label="当前仓库:">
            <el-input
              v-model="setFormDat.invName"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="nameCode" label="DI/物资编码:">
            <el-input
              v-model="setFormDat.nameCode"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="cpmctymc" label="物资名称:">
            <el-input
              v-model="setFormDat.cpmctymc"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="ggxh" label="规格型号:">
            <el-input
              v-model="setFormDat.ggxh"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="measname" label="计量单位:">
            <el-input
              v-model="setFormDat.measname"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="ylqxzcrbarmc" label="生产企业:">
            <el-input
              v-model="setFormDat.ylqxzcrbarmc"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="zczbhhzbapzbh" label="注册/备案号:">
            <el-input
              v-model="setFormDat.zczbhhzbapzbh"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="invCount" label="在库数量:">
            <el-input
              v-model="setFormDat.invCount"
              auto-complete="off"
              style="width: 90%"
              clearable
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="rationCount" label="定额数量:">
            <el-input
              v-model="setFormDat.rationCount"
              auto-complete="off"
              style="width: 90%"
              type="number"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <div style="margin-top: 12px">
        <el-divider content-position="left">生成出入库单</el-divider>
        <el-row class="order-el-row" style="margin-top: 20px">
          <el-col :span="11">
            <el-form-item prop="action" label="单据类型:">
              <el-select
                v-model="setFormDat.scanType"
                placeholder="请选择单据类型"
                style="width: 90%"
                filterable
                clearable
                @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-col :span="11"
                  v-if="curAction.corpType == 3"
          >
            <!--            <el-form-item-->
            <!--              prop="fromCorp"-->
            <!--              label="供应商:"-->
            <!--              v-if="-->
            <!--                curAction.corpType === 0 ||-->
            <!--                curAction.corpType === 2 ||-->
            <!--                curAction.corpType === 4 ||-->
            <!--                (curAction.corpType === 1 && !curAction.genUnit)-->
            <!--              "-->
            <!--            >-->
            <!--              <el-select-->
            <!--                v-model="setFormDat.fromCorp"-->
            <!--                filterable-->
            <!--                remote-->
            <!--                clearable-->
            <!--                reserve-keyword-->
            <!--                style="width: 90%"-->
            <!--                placeholder="请输入往来信息"-->
            <!--                :remote-method="findMethod"-->
            <!--                :loading="loading"-->
            <!--              >-->
            <!--                <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-form-item
              prop="fromCorp"

              label="往来仓库:"
            >
              <el-select
                v-model="setFormDat.fromInvCode"
                placeholder="请选择往来仓库"
                filterable
                clearable
                style="width: 90%"
              >
                <el-option
                  v-for="item in fromInvOptions"
                  :key="item.code"
                  :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-col :span="11">
            <el-form-item label="审核状态" prop="auditStatus">
              <el-radio-group v-model="setFormDat.auditStatus">
                <el-radio :label="1">草稿</el-radio>
                <el-radio :label="2">已提交</el-radio>
              </el-radio-group>

            </el-form-item>
          </el-col>
        </el-row>
      </div>

      <div style="margin-top: 12px">
        <el-divider content-position="left">生成业务单</el-divider>
        <el-row class="order-el-row" style="margin-top: 20px">
          <el-col :span="11">
            <el-form-item label="目标业务单">
              <el-select v-model="setFormDat.busType" placeholder="目标业务单" clearable size="small"
                         @change="busActionChange"
                         style="width: 90%">
                <el-option
                  v-for="item in targetBusTypes"
                  :key="item.originAction"
                  :label="item.originName"
                  :value="item.originAction">
                  <span> {{ item.originName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item :label="invTitle">
              <el-select v-model="setFormDat.busInvCode" :placeholder="invTitle" clearable size="small"
                         style="width: 90%">
                <el-option
                  v-for="item in busInvs"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                  <span> {{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="审核状态" prop="busAuditStatus">
              <el-radio-group v-model="setFormDat.busAuditStatus">
                <el-radio :label="1">草稿</el-radio>
                <el-radio :label="2">待审核</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


      </div>

      <el-row>
        <el-col :span="11">
          <el-form-item prop="remark" label="备注:">
            <el-input
              v-model="setFormDat.remark"
              auto-complete="off"
              clearable
              type="textarea"
              style="width: 90%"
              placeholder="请输入备注信息"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>


    <div style='text-align: center; margin-bottom: 10px;margin-top: 18px ;'>
      <el-button type="primary" @click="saveSet" :loading="saveLoading">提交</el-button>
      <el-button type="primary" @click="closeDialog">取消</el-button>
    </div>
  </el-card>
</template>

<script>
import {updateSet} from "@/api/system/InvRationSet";
import {getBusTypeChangeList} from "@/api/basic/busTypeChange";
import {findChangeTarget} from "@/api/basic/busType";
import {filterGroupBySpuse, filterSubAll} from "@/api/system/invSubWarehouse";
import {getBasicUnitMaintains} from "@/api/basic/basicUnitMaintain";
import {isBlank} from "@/utils/strUtil";

export default {
  name: "editRaationSetDialog",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    setFormDat: {
      type: Function,
      required: true,
    },
  },

  data() {

    return {
      targetBusTypes: [],
      busInvs: [],
      busTypeOptions: [],
      curAction: {},
      fromInvOptions: [],
      fromOptions: [],
      invTitle: "仓库",
      formRules: {
        rationCount: [
          {required: true, message: "请输入定额数量", trigger: "blur"},
        ],
      },
      loading: false,
      saveLoading: false,
    }
  },

  components: {},
  methods: {

    saveSet() {
      let data = Object.assign(
        JSON.parse(JSON.stringify(this.setFormDat))
      );
      if (!isBlank(this.setFormDat.busType) && isBlank(this.setFormDat.busInvCode)) {
        this.$message.warning("请选择" + this.invTitle + "!");
        return;
      }
      this.saveLoading = true
      updateSet(data).then((response) => {
        this.saveLoading = false
        if (response.code === 20000) {
          this.closeDialog();
        } else {
          this.$message.error(response.message);
        }
        this.loading = false;
      });
    },

    getTargetChangeType() {
      let query = {
        enable: true
      };
      getBusTypeChangeList(query).then((res) => {
        if (res.code === 20000) {
          this.targetBusTypes = res.data.list || [];
        }
      })
    },

    getTargetScanType() {
      let query = {
        enable: true,
        mainAction: "WareHouseIn"
      };
      findChangeTarget(query).then((res) => {
        if (res.code === 20000) {
          this.busTypeOptions = res.data.list || [];
        }
      })
    },


    actionChange(row) {
      if (!isBlank(row)) {
        this.curAction = this.busTypeOptions.filter(item => item.action === row)[0];
        if (this.curAction.corpType === 3) {
          //内部科室
          this.getSubInvList();
        }
      }

    },

    busActionChange(row) {
      this.setFormDat.busInvCode = "";
      switch (row) {
        case "HCLY":
          this.invTitle = "领用仓库";
          this.getBusInv();
          break;
        case "CPSG":
          this.getBusInv();
          this.invTitle = "采购仓库";
          break;
        case "CGJH":
          this.getBusInv();
          this.invTitle = "采购仓库";
          break;
        case "CGDD":
          this.getBusInv();
          this.invTitle = "采购仓库";
          break;
        default:
          this.invTitle = "仓库";
      }
    },

    getBusInv() {
      var query = {
        spUse: true,
      }
      filterGroupBySpuse(query).then((res) => {
        this.busInvs = res.data || [];
        // this.setFormDat.busInvCode = this.busInvs [0];
      })
    },


    getSubInvList() {
      let query = {
        filter: 3,
      };
      filterSubAll(query)
        .then((response) => {
          this.fromInvOptions = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },

    //获取往来单位候选列表
    findMethod(query) {
      this.fromOptions = [];
      let cQuery = {
        key: query,
        corpType: null,
        outType: null,
        page: 1,
        limit: 20,
      };
      if (this.curAction.corpType == 3) {
        //内部科室
        cQuery.corpType = 3;
      } else if (this.curAction.corpType == 2) {
        //供应商信息
        cQuery.corpType = 2;
      } else if (this.curAction.corpType == 0) {
        cQuery.corpType = 2;
      } else if (this.curAction.corpType == 1) {
        cQuery.corpType = 1;
        cQuery.outType = 2;
      } else if (this.curAction.corpType == 4) {
        //供应商信息
        cQuery.corpType = 4;
      } else
        return;
      getBasicUnitMaintains(cQuery)
        .then((response) => {
          this.loading = false;
          this.fromOptions = response.data.list || [];
        })
        .catch(() => {
          this.loading = false;
        });
    },

  },

  created() {
    this.getTargetChangeType();
    this.getTargetScanType();
    this.getBusInv();
  },
}
</script>

<style scoped>

</style>
