<template>
    <div>

        <el-form :model="formData" :rules="formRules" ref="dataForm" label-width="120px">
            <el-row type="flex" justify="end" v-if="editType == 1">
                <el-button-group style="display: flex;margin-bottom: 15px; margin-right: 50px">
                    <el-button
                        type="primary"
                        @click.native="saveOrder('3')"
                        :loading="loading"
                    >审核通过
                    </el-button
                    >
                    <el-button
                        type="primary"
                        @click.native="saveOrder('4')"
                        :loading="loading"
                    >拒绝申请
                    </el-button
                    >
                </el-button-group>
            </el-row>

            <el-card style="margin-top: -5px;">
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="单据号:" prop="billNo">
                            <el-input v-model="formData.billNo" style="width: 90%;" auto-complete="off" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="单据日期:" prop="billdate">
                            <el-date-picker
                                v-model="formData.billDate"
                                type="datetime"
                                style="width: 90%"
                                placeholder="选择日期"
                                :disabled="true"
                                :clearable="false"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="11">
                      <el-form-item label="申购科室:">
                        <el-input v-model="formData.targetInvName" style="width: 90%;" auto-complete="off" :disabled="true"></el-input>
                      </el-form-item>
                    </el-col>

                    <el-col :span="11">
                        <el-form-item label="申购说明:"  prop="billNo">
                            <el-input v-model="formData.remark" auto-complete="off" style="width: 90%;" type="textarea" disabled autosize></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-row>
                    <el-col :span="11">
                        <el-form-item  label="审核说明：" prop="billNo">
                            <el-input v-model="formData.auditRemark" auto-complete="off" style="width: 90%;" type="textarea" autosize></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="11">
                        <el-form-item>
                            <el-checkbox v-model="formData.audtoPlan">是否生成采购计划</el-checkbox>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row  v-if="formData.audtoPlan">
                    <el-col :span="11">
                        <el-form-item  label="采购科室：" prop="targetInv">
                            <el-select v-model="formData.targetSubInv" placeholder="当前采购科室信息" @change="setTargetInv" style="width: 90%;" clearable>
                                <el-option
                                    v-for="item in targetSubInvList"
                                    :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-row>
            </el-card>

            <el-card>
                <el-table v-loading="loading" :data="codeArray" style="width: 100%;" :row-class-name="tableRowClassName" border highlight-current-row max-height="300" height="300" ref="multipleTable">
                    <el-table-column label="序号" type="index" width="50"></el-table-column>
                    <el-table-column label="产品通用名" prop="productName" show-overflow-tooltip></el-table-column>
                    <el-table-column label="规格型号" prop="spec" show-overflow-tooltip></el-table-column>
                    <el-table-column width="150" label="单据数量" prop="count"></el-table-column>
                    <el-table-column label="注册/备案凭证号" prop="zczbhhzbapzbh" show-overflow-tooltip></el-table-column>
                    <el-table-column label="生产厂家" prop="manufactory" show-overflow-tooltip></el-table-column>
                    <el-table-column label="医疗器械注册人" prop="ylqxzcrbarmc" show-overflow-tooltip></el-table-column>
                </el-table>
            </el-card>
        </el-form>
        <el-dialog
            title="产品录入"
            :visible.sync="selectProductVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            width="80%"
            v-if="selectProductVisible"
            :append-to-body='true'
        >
            <purApplyProducts
                :closeDialog="closeDialogC2"
                :purType="1"
                :data="thisData"
            ></purApplyProducts>
        </el-dialog>
    </div>
</template>

<script>
import draggable from "vuedraggable";
import purApplyProducts from "@/views/purchase/purApply/purApplyProducts";
import {delApplyDetail, listApplyDetail, auditOrder} from "@/api/purchase/purApply";
import {filterAllByUser} from "@/api/system/invWarehouse";
import {filterSubByInv} from "@/api/system/invSubWarehouse";
import {getBusChange} from "@/api/basic/busTypeChange";

export default {
    name: "idQuery",
    props: {
        closeDialog: {
            type: Function,
            required: true,
        },
        idQuery: {
            type: Object,
            required: true,
        },
        editType: {
            type: Object,
            required: true,
        },

    },
    data() {
        return {
            code: "",
            query: {
                orderIdFk: "",
                page: 1,
                limit: 10,
            },
            formData: {
                status: null,
                id: null,
                billNo: null,
                billDate: "",
                remark: "",
                deptCode: null,
                invCode: null,
                targetInvCode: null,
                targetDeptCode: null,
                targetBillType: null,
                targetInvName:null,
                auditRemark: "",
                audtoPlan: false,
                targetSubInv:null,
            },
           targetSubInvList: [],
            formRules: {},
            codeArray: [],
            total: 0,
            loading: false,
            index: null,
            formLoading: false,
            formVisible: false,
            deleteLoading: false,
            orderNo: null,
            statusMap: {
                1: "草稿",
                2: "未审核",
                3: "已审核",
            },
            typeMap: {
                1: "预入库",
                2: "普通采购",
            },
            orderEditor: true,
            sOptions: [],
            sValue: [],
            sList: [],
            sLoading: false,
            busTypes: [],
            currentRow: {},
            selectedIndex: "",
            selectProductVisible: false,
            thisData: {},
            targetInvList: [],
            targettargetInvList: [],
            invQueryData: {},

        };
    },
    components: {
        draggable,
      purApplyProducts,
    },
    methods: {
        saveOrder(status) {
            let tip = "";
            if (status == "3") {
                tip = "是否确定通过该申购请求？";
                if (this.formData.audtoPlan) {
                    if (this.$isBlank(this.formData.targetSubInv)) {
                        this.$message.warning("请选择当前采购科室！");
                        return;
                    }
                }
            } else {
                tip = "是否确定拒绝该申购请求？";
            }
            this.$confirm(tip, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.submitFunction(status);
            }).catch(() => {
                this.loading = false;
            });
        },
        submitFunction(status) {
            this.loading = true;
            this.formData.status = status;
            auditOrder(this.formData)
                .then(response => {
                    this.loading = false;
                    if (response.code === 20000) {
                        this.$message.success("更新成功！");
                        this.closeDialog(true);
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                    this.loading = false;
                })
        },
        selectProductFunction() {
            this.selectProductVisible = true;
        },
        closeDialogC2(rData) {
            this.selectProductVisible = false;
            this.thisData = {};
            if (this.$isNotBlank(rData)) {
                this.codeArray = [];
                rData.forEach((obj, index) => {
                    this.codeArray.unshift(obj);
                });
                this.$refs.multipleTable.setCurrentRow(this.codeArray[0]);
                this.currentRow = this.codeArray[0];
                this.selectedIndex = 0;
            }
        },
        rowChange(val) {
            this.currentRow = val;
            this.selectedIndex = val.index;
        },
        tableCountChange(row) {
            if (this.$isNotBlank(row)) {
                // row.count = row.reCount;
            }
        },
        tableRowClassName({row, rowIndex}) {
            row.index = rowIndex;
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
      findTargetSubInvs() {
        this.targetSubInvList = [];
        let query = {
          filter:3
        };
        filterSubByInv(query)
          .then((response) => {
            this.targetSubInvList = response.data || [];
            if (this.targetSubInvList != null && this.targetSubInvList.length == 1) {
              this.formData.targetSubInv = this.targetSubInvList[0].code;
            }
          })
          .catch(() => {
          });
      },

      deleteCodeArray(index, row) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.$refs.multipleTable.setCurrentRow();
                this.currentRow = {};
                this.selectedIndex = "";

                if (this.orderEditor) {
                    this.detailLoading = true;
                    if (this.$isNotBlank(row.id)) {
                        delApplyDetail(row.id)
                            .then(response => {
                                this.detailLoading = false;
                                if (response.code === 20000) {
                                    this.$message.success(response.data);
                                    // this.getStockOrderDetailList();
                                    this.codeArray.splice(index, 1);
                                } else {
                                    this.$message.error(response.message);
                                }
                            })
                            .catch(() => {
                                this.detailLoading = false;
                            })
                    } else {
                        this.$message.success('删除成功');
                        this.codeArray.splice(index, 1);
                    }
                } else {
                    this.$message.success('删除成功');
                    this.codeArray.splice(index, 1);
                }
            }).catch(() => {

            });
        },
        getStockOrderDetailList() {
            this.loading = true;
            listApplyDetail(this.query) //查找该单号下的所有条码
                .then((response) => {
                    console.log(response)
                    this.codeArray = response.data.list || [];
                    this.total = response.data.total || 0;
                    this.loading = false;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
    },
    filters: {},
    mounted() {
        document.body.ondrop = function (event) {
            event.preventDefault();
            event.stopPropagation();
        };
    },
    created() {

        if (this.$isNotBlank(this.idQuery.id)) {
            this.query.limit = 100;
            this.query.orderIdFk = this.idQuery.id;
            this.formData = this.idQuery.formData;
            this.orderEditor = true;
            this.sValue = this.formData.corpName;
            this.getStockOrderDetailList();
        } else {
            this.formData = {
                id: null,
                billNo: null,
                billDate: "",
                remark: "",
                deptCode: null,
                invCode: null,
            };
            this.orderEditor = false;
        }
      this.findTargetSubInvs();
        this.codeArray = [];
    },
};
</script>
<style>



</style>



