<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" label-width="100px" size="mini">
        <el-row>
          <el-col :span="8">
            <el-form-item label="套包名称:" class="query-form-item">
              <el-input v-model="filterQuery.name" placeholder="请输入套包名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="套包编码:" class="query-form-item">
              <el-input v-model="filterQuery.code" placeholder="请输入套包编码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item>
              <el-button-group>
                <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
                <el-button type="primary" icon="search" @click="combine">选入</el-button>
              </el-button-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-table :data="list"  border highlight-current-row  @current-change="handleChange" style="width: 100%" >
        <el-table-column width="50">
          <template slot-scope="scope">
            <el-radio v-model="radio" :label="scope.row.id"><span></span></el-radio>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="套包名称" prop="name" ></el-table-column>
        <el-table-column label="套包编码" prop="code" ></el-table-column>
        <el-table-column label="规格" prop="ggxh" ></el-table-column>
        <el-table-column label="价格" prop="price" >
          <template slot-scope="scope">
            {{ scope.row.price == "null" ? "0.00":scope.row.price }}
          </template>
        </el-table-column>
<!--        <el-table-column label="备注" prop="remark" ></el-table-column>-->
        <el-table-column label="创建人" prop="createByName" ></el-table-column>
        <el-table-column label="创建时间" prop="createTime" ></el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="handleDetailClick(scope.row)"
            >详情
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :limit.sync="filterQuery.limit"
          :page.sync="filterQuery.page"
          @pagination="handleCurrentChange"
      ></pagination>
    </el-card>

    <el-dialog
      :title= " '组套名称：' + this.currentRow.name + '  组套编码：'  + this.currentRow.code"
      :visible.sync="warlockBagDetailVisible"
      :before-close="close"
      width="90%"
      append-to-body
      v-if="warlockBagDetailVisible"
      @close='closedeStinyDialog'
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >


      <el-card>
        <el-table v-loading="loading" :data="delectList" border highlight-current-row style="width: 100%">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="产品DI" prop="nameCode" width="200"></el-table-column>
          <el-table-column label="产品编码" prop="thirdId" width="200"></el-table-column>
          <el-table-column label="产品名称" prop="cpmctymc" width="200"></el-table-column>
          <el-table-column label="规格型号" prop="ggxh"></el-table-column>
          <el-table-column label="计量单位" prop="measname"></el-table-column>
          <el-table-column label="数量" prop="count">
            <template slot-scope="scope">
              <el-input size="small" v-model="scope.row.count"
                        placeholder="请输入数量" style="width: 100%"
                        type='number'
                        :disabled="scope.row.index !== selectedIndex"
                        oninput="value=value.replace(/[^\d]/g,'')"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="price"></el-table-column>
          <el-table-column label="生产企业" prop="ylqxzcrbarmc"></el-table-column>
          <el-table-column label="注册/备案号" prop="zczbhhzbapzbh"></el-table-column>
<!--          <el-table-column label="操作" width="220" >-->
<!--            <template slot-scope="scope">-->
<!--              <el-button type="text" size="small" :disabled="scope.row.index === selectedIndex"-->
<!--                         @click.native.stop="rowChange(scope.row)">编辑-->
<!--              </el-button>-->
<!--              <el-button type="text" size="small" :disabled="scope.row.index !== selectedIndex" @click.stop="true"-->
<!--                         @click.native="tableCountChange(scope.row)">保存-->
<!--              </el-button>-->
<!--              <el-button type="text" size="small" @click.native.stop="delectModifyClick(scope.row)">移除</el-button>-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>
        <pagination
          v-show="delectTotal>0"
          :total="delectTotal"
          :limit.sync="delectQuery.limit"
          :page.sync="delectQuery.page"
          @pagination="handleCurrentDelectChange"
        ></pagination>
      </el-card>

    </el-dialog>
  </div>
</template>

<script>
import {
  filterDestinyDelect,
  getBasicDestinyfilter
} from "@/api/basic/basicDestinyRel";
import { addWarlockBagProduct } from "@/api/inout/orderDetailCode";
import {  addWarlockBag } from "@/api/inout/receiveOrder";
import {  addDetailByWarlockBag } from "@/api/purchase/purApply";
import {  addOrderDetailByWarlockBag } from "@/api/purchase/purOrder";
import {  addPlanDetailByWarlockBag } from "@/api/purchase/purPlan";



export default {
  props: {
    data: {
      type: Object,
      required: true,
    },
    pId: {
      type: Object,
      required: true,
    },
    type: {
      type: Number,
      required: true,
    },
    codeArray: {
      type: Array,
      required: true,
    },
    closeDialog: {
      type: Function,
      required: true,
    },
    // filterType: {
    //   type: Function,
    //   required: true,
    // },
  },
  data() {
    return {
      filterQuery: {
        name: "",
        code: "",
        type: 2,
        status: 3,
        page: 1,
        limit: 20
      },
      delectQuery: {
        id: null,
        status: null,
        page: 1,
        limit: 20
      },
      delectList: [],
      delectTotal: 0,
      total: 0,
      destinyId:null,
      radio:null,
      currentRow:null,
      warlockBagDetailVisible: false,
      list: [],
    };
  },
  methods: {
    handleCurrentDelectChange(val) {
      this.delectQuery.page = val.page;
      this.filterDestinyDelect()
    },
    handleDetailClick(row) {
      this.currentRow = row;
      this.delectQuery.id = row.id
      this.delectQuery.status = row.status
      this.filterDestinyDelect()
      this.warlockBagDetailVisible = true;
    },
    filterDestinyDelect() {
      filterDestinyDelect(this.delectQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.delectList = response.data.list || [];
            this.delectTotal = response.data.total || 0;
          } else {
            this.$message.error(response.message);
            this.delectList = [];
            this.delectTotal = 0;
          }
        })
        .catch(() => {
          this.loading = false;
          this.delectList = [];
          this.delectTotal = 0;
        });
    },
    onReset() {
      this.$router.push({
        path: ""
      });
      this.filterQuery = {
        name: "",
        code: "",
        type: 2,
        status: 3,
        page: 1,
        limit: 20
      };
      this.getList();
      this.destinyId=null
    },
    onSubmit() {
      this.$router.push({
        path: "",
      });
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getBasicDestinyfilter(this.filterQuery).then(response => {
        this.loading = false;
        this.list = response.data.list || [];
        this.total = response.data.total || 0;
      }).catch(() => {
        this.loading = false;
        this.list = [];
        this.total = 0;
        this.roles = [];
      });
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    showRow (row) {
      this.destinyId= row
    },
    combine(){
      if (this.currentRow == null) {
        this.$message.error('未选择术式套包');
        return;
      }

      if (this.type == 1 ){//新增领用单
        let CQuery = {
          purReceiveEntity: this.data,
          bagId: this.currentRow.id
        }
        addWarlockBag(CQuery).then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.closeDialog(response.data);
          } else {
            this.$message.error(response.message);
          }
        }).catch(() => {
          this.loading = false;
          this.dataList = [];
          this.pageTotal = 0;
        });

      }else if ( this.type == 2){//新增业务单据
        let CQuery = {
          id: this.currentRow.id,
          orderEntity: this.data
        }
        addWarlockBagProduct(CQuery).then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.closeDialog(response.data);
          } else {
            this.$message.error(response.message);
          }
        }).catch(() => {
          this.loading = false;
          this.dataList = [];
          this.pageTotal = 0;
        });
      }else if ( this.type == 3) {//新增申购单据
        let CQuery = {
          bagId: this.currentRow.id,
          orderIdFk: this.pId
        }
        addDetailByWarlockBag(CQuery).then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.closeDialog(response.data);
          } else {
            this.$message.error(response.message);
          }
        }).catch(() => {
          this.loading = false;
          this.dataList = [];
          this.pageTotal = 0;
        });

      }else if ( this.type == 4) {//新增采购计划
        let CQuery = {
          bagId: this.currentRow.id,
          orderIdFk: this.pId
        }
        addPlanDetailByWarlockBag(CQuery).then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.closeDialog(response.data);
          } else {
            this.$message.error(response.message);
          }
        }).catch(() => {
          this.loading = false;
          this.dataList = [];
          this.pageTotal = 0;
        });
      }else if ( this.type == 5) {//新增采购订单
        let CQuery = {
          bagId: this.currentRow.id,
          orderIdFk: this.pId
        }
        addOrderDetailByWarlockBag(CQuery).then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.closeDialog(response.data);
          } else {
            this.$message.error(response.message);
          }
        }).catch(() => {
          this.loading = false;
          this.dataList = [];
          this.pageTotal = 0;
        });
      }

    },
    handleChange(val) {
      this.radio = val.id;
      this.currentRow = val;
    },
  }
  ,
  mounted() {
  }
  ,
  created() {
    this.getList();
  }
}
;
</script>
