<template>
  <div>

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

      <el-card style="margin-top: -5px;">
        <el-row>
              <el-col :span="11">
                <el-form-item label="合同编号:" prop="code">
                  <el-input v-model="formData.code" auto-complete="off" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="合同名称:" prop="name">
                  <el-input v-model="formData.name"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="11">
                <el-form-item label="合同仓库:" prop="invCode">
                  <el-select v-model="formData.invCode" placeholder="合同仓库信息" style="width: 100%"
                             clearable>
                    <el-option
                      v-for="item in invCodeList"
                      :key="item.name"
                      :label="item.name"
                      :value="item.code">
                      <span style="float: left">{{ item.name }}</span>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="合同签订日期:" prop="contractDate">
                  <el-date-picker
                    v-model="formData.contractDate"
                    style="width: 100%"
                    type="datetime"
                    placeholder="选择日期"
                    :clearable="false"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :disabled-date="disabledArrivalDate"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>

            </el-row>

            <el-row >
              <el-col :span="11">
                <el-form-item label="合同类型:" prop="type">
                  <el-select v-model="formData.type" placeholder="合同类型" style="width: 100%"
                             clearable>
                    <el-option
                      v-for="item in typeList"
                      :key="item.name"
                      :label="item.name"
                      :value="item.code">
                      <span style="float: left">{{ item.name }}</span>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="11">
                <el-form-item
                  prop="fromCorp"
                  label="供应商:"

                >
                  <el-select
                    v-model="formData.fromCorp"
                    filterable
                    remote
                    :disabled="corpOrderIdDisabled"
                    clearable
                    reserve-keyword
                    placeholder="请选择供应商"
                    :remote-method="findMethod"
                    style="width: 100%"
                  >
                    <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>

              <!--          <el-col :span="11">-->
              <!--            <el-form-item label="合同状态:" prop="status">-->
              <!--              <el-select v-model="formData.status" placeholder="合同状态" style="width: 100%" disabled-->
              <!--                         clearable>-->
              <!--                <el-option-->
              <!--                  v-for="item in statusList"-->
              <!--                  :key="item.name"-->
              <!--                  :label="item.name"-->
              <!--                  :value="item.code">-->
              <!--                  <span style="float: left">{{ item.name }}</span>-->
              <!--                </el-option>-->
              <!--              </el-select>-->
              <!--            </el-form-item>-->
              <!--          </el-col>-->



              <el-col :span="11">
                <el-form-item label="开始时间:" prop="startDate" >
                  <el-date-picker
                    style="width: 100%"
                    v-model="formData.startDate"
                    type="datetime"
                    placeholder="选择日期"
                    :clearable="false"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :disabled-date="disabledArrivalDate"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="截止时间:" prop="endDate">
                  <el-date-picker
                    style="width: 100%"
                    v-model="formData.endDate"
                    type="datetime"
                    placeholder="选择日期"
                    :clearable="false"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :disabled-date="disabledArrivalDate"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>



              <el-col :span="11">
                <el-form-item label="订单金额:" prop="amount">
                  <el-input-number
                    style="width: 60%"
                    :precision="2"
                    :step="1"
                    :min="0.00"
                    v-model="formData.amount" size="small" ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="合同金额:" prop="payAmount">
                  <el-input-number
                    style="width: 60%"
                    :precision="2"
                    :step="1"
                    :min="0.00"
                    v-model="formData.payAmount" size="small" ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>

              <el-col :span="11">
                <el-form-item label="付款日期:" prop="payDate">
                  <el-date-picker
                    style="width: 100%"
                    v-model="formData.payDate"
                    type="datetime"
                    placeholder="选择日期"
                    :clearable="false"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :disabled-date="disabledArrivalDate"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="付款方式:" prop="payType">
                  <!--              <el-select v-model="formData.payType" placeholder="付款方式" style="width: 100%"-->
                  <!--                         clearable>-->
                  <!--                <el-option-->
                  <!--                  v-for="item in payTypeList"-->
                  <!--                  :key="item.name"-->
                  <!--                  :label="item.name"-->
                  <!--                  :value="item.code">-->
                  <!--                  <span style="float: left">{{ item.name }}</span>-->
                  <!--                </el-option>-->
                  <!--              </el-select>-->
                  <el-input v-model="formData.payType" style="width: 100%"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>

              <el-col :span="11">
                <el-form-item label="备注:" prop="remark">
                  <el-input v-model="formData.remark" style="width: 100%"></el-input>
                </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="60"></el-table-column>
          <el-table-column label="DI/物资编码" prop="nameCode"></el-table-column>
          <el-table-column label="物资名称" prop="productName" show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="规格型号" prop="spec" show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="计量单位" prop="measname" show-overflow-tooltip="true" width="100"></el-table-column>
          <el-table-column label="采购数量" prop="count"></el-table-column>
          <el-table-column label="注册/备案号" prop="zczbhhzbapzbh"></el-table-column>
          <el-table-column label="生产厂家" prop="manufactory"></el-table-column>
          <el-table-column label="供应商" prop="supName"></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'-->
<!--    >-->
<!--      <selectProduct-->
<!--        :closeDialog="closeDialogC2"-->
<!--        :data="thisData"-->
<!--        :purType="1"-->
<!--      ></selectProduct>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import draggable from "vuedraggable";
import {auditOrder, delApplyDetail, listApplyDetail} from "@/api/purchase/purContract";
import {findInvByUser} from "@/api/system/invSubWarehouse";

export default {
  name: "idQuery",
  props: {
    pageType: {
      type: Object,
      required: true,
    },
    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: {
        id: null,
        code: null,
        name: null,
        createTime: "",
        remark: "",
        deptCode: null,
        locStorageCode: null,
        invWarehouseCode: null,
        auditRemark: null,
        autoPurchase: false,
        targetInv: 1000,
        targetSubInv: null,
        targetBillAction: null,
        pageType: null,
      },
      formRules: {},
      codeArray: [],
      total: 0,
      loading: false,
      passLoading: false,
      noPassLoading: 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: {},
      subInvList: [],
      tarSubList: [],
      invQueryData: {},


    };
  },
  components: {
    draggable
  },
  methods: {
    findSubInvs()
    {
      this.subInvList = [];
      let query = {
        filter: 3,
        code : this.formData.invCode
      };
      findInvByUser(query)
        .then((response) => {
          this.subInvList = response.data || [];
          if (this.subInvList != null && this.subInvList.length == 1) {
            this.formData.invCode = this.subInvList[0].code;
            this.formData.deptCode = this.subInvList[0].parentId;
          }
        })
        .catch(() => {
        });
    }
    ,
    getInvList()
    {
      this.invCodeList = [];
      let query = {
        filter: 3
      };
      findInvByUser(query)
        .then((response) => {
          this.invCodeList = response.data || [];
        })
        .catch(() => {
        });
    }
    ,
    saveOrder(status) {
      let tip = "";
      if (status == "3") {
        tip = "是否确定通过该采购合同？";
      } else {
        tip = "是否确定拒绝该采购合同？";
      }
      this.$confirm(tip, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (status == "3"){
          this.passLoading = true
        }else {
          this.noPassLoading = true
        }
        this.submitFunction(status);
      }).catch(() => {
        if (status == "3"){
          this.passLoading = false
        }else {
          this.noPassLoading = false
        }
      });
    },
    submitFunction(status) {
      // this.loading = true;
      this.formData.pageType = this.pageType
      let tQuery = {
        status:status,
        code:this.formData.code
      }
      auditOrder(tQuery)
        .then(response => {
          if (status == "3"){
            this.passLoading = false
          }else {
            this.noPassLoading = false
          }
          if (response.code === 20000) {
            this.$message.success("审批成功");
            this.closeDialog(true);
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          if (status == "3"){
            this.passLoading = false
          }else {
            this.noPassLoading = 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();
      }
    },
    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;
      this.query.orderIdFk=this.idQuery.id
      listApplyDetail(this.query) //查找该单号下的所有条码
        .then((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() {
    this.formData = this.idQuery
    this.findSubInvs();
    this.getInvList();
    this.getStockOrderDetailList();
  },
};
</script>
<style scoped>

.ao-text {
  width: 100%;
  font-size: 13px;
  font-family: "Microsoft YaHei";
  float: right;
  text-align: right;
  margin-top: 10px;
}

</style>



