<template>
  <div>
    <el-card class="el-card">
      <el-form v-if="queryList && queryList.length > 0 && showSearch" :model="filterQuery" class="query-form" size="mini" label-width="100px">
<!--        <el-row>-->
<!--          <el-col :span="18">-->
<!--            <el-form-item class="query-form-item" label="扫码查询:">-->
<!--              <el-input v-model="filterQuery.udiCode"-->
<!--                        style="width: 97%"-->
<!--                        placeholder="请扫描或输入UDI码"-->
<!--                        clearable="true"-->
<!--                        @keyup.enter.native="keyup_submit($event)"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-row>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="DI/物资编码:">-->
<!--              <el-input v-model="filterQuery.nameCode" placeholder="DI/物资编码"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="物资名称:">-->
<!--              <el-input v-model="filterQuery.cpmctymc" placeholder="物资名称"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="规格型号:">-->
<!--              <el-input v-model="filterQuery.ggxh" placeholder="规格型号"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="批次号:">-->
<!--              <el-input v-model="filterQuery.batchNo" placeholder="批次号"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-row>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="注册/备案号:">-->
<!--              <el-input v-model="filterQuery.zczbhhzbapzbh" placeholder="注册/备案号"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="生产企业:">-->
<!--              <el-input v-model="filterQuery.ylqxzcrbarmc" placeholder="生产企业"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="所属仓库:">-->
<!--              <el-select v-model="filterQuery.invCode" placeholder="请选择所属仓库" clearable="true"-->
<!--                         style="width: 90%"-->
<!--                         @change="invChange"-->
<!--              >-->
<!--                <el-option-->
<!--                  v-for="item in invList"-->
<!--                  :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="6">-->
<!--            <el-form-item class="query-form-item" label="所属货位:">-->
<!--              <el-select-->
<!--                v-model="filterQuery.invSpaceCode"-->
<!--                filterable-->
<!--                remote-->
<!--                clearable="true"-->
<!--                reserve-keyword-->
<!--                placeholder="请选择所属货位"-->
<!--                :remote-method="getSpaceList"-->
<!--                size="mini"-->
<!--                :loading="corpLoading"-->
<!--                style="width: 90%"-->
<!--              >-->
<!--                <el-option-->
<!--                  v-for="item in spaceList"-->
<!--                  :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.code }}</span>-->

<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="过滤方式:">-->
<!--              <el-select-->
<!--                v-model="filterQuery.filterNoInv"-->
<!--                style="width: 90%"-->
<!--                clearable>-->
<!--                <el-option label="有库存" :value="1"></el-option>-->
<!--                <el-option label="零库存" :value="2"></el-option>-->
<!--                <el-option label="负库存" :value="3"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item class="query-form-item" label="供应商名称:">-->
<!--              <el-select-->
<!--                v-model="filterQuery.supId"-->
<!--                filterable-->
<!--                remote-->
<!--                clearable="true"-->
<!--                reserve-keyword-->
<!--                placeholder="请选择供应商名称"-->
<!--                :remote-method="findMethod"-->
<!--                size="mini"-->
<!--                :loading="corpLoading"-->
<!--                @change="corpChange"-->
<!--                style="width: 90%"-->
<!--                @clear="getcorp"-->
<!--              >-->
<!--                <el-option-->
<!--                  v-for="item in fromOptions"-->
<!--                  :key="item.name"-->
<!--                  :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 style=" display:flex;  flex-wrap: wrap; ">
          <template v-for="(item, index) in queryList" >
            <el-form-item v-if="item.columnType == 'input' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`" :key="item.id">
              <el-input
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :disabled="executeEval(null,item.disabledFuc,false)"
                @keyup.enter.native="executeFuc($event,'5',item.clickFuc)"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'select' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-select v-model="filterQuery[item.columnName]"
                         :placeholder="item.columnDesc"
                         :disabled="executeEval(null,item.disabledFuc,false)"
                         clearable>
                <el-option
                  v-for="dict in item.lableRuleObj"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
                <span style="float: left">{{ item.name }}</span>
                <span v-if="item.isShowXx == '1'" style="float: right; color: #8492a6; font-size: 13px">{{ item.code }}</span>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'selectServer' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-select
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :disabled="executeEval(null,item.disabledFuc,false)"
                filterable
                remote
                :remote-method="(query) => executeFuc(query,'5',item.clickFuc)"
                clearable>
                <el-option
                  v-for="item in options[item.clickFuc]"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
                <span style="float: left">{{ item.name }}</span>
                <span v-if="item.isShowXx == '1'" style="float: right; color: #8492a6; font-size: 13px">{{ item.code }}</span>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'datePicker' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'date' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-date-picker
                v-model="filterQuery[item.columnName]"
                :style="`width:${item.width+'px'}`"
                value-format="yyyy-MM-dd"
                :disabled="executeEval(null,item.disabledFuc,false)"
                type="date"
                :placeholder="item.columnDesc"
              ></el-date-picker>
            </el-form-item>
          </template>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="onReset"
          >重置
          </el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
          >查询
          </el-button
          >
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border>
<!--        <el-table-column label="序号" type="index"></el-table-column>-->
<!--        <el-table-column label="供应商" prop="supName" v-if="showSup" show-overflow-tooltip-->
<!--                         width="120"></el-table-column>-->
<!--        <el-table-column label="货架" prop="spaceName"-->
<!--                         v-if="filterQuery.invSpaceCode!='' && filterQuery.invSpaceCode!=null" show-overflow-tooltip-->
<!--                         width="120"></el-table-column>-->
<!--        <el-table-column label="DI/物资编码" prop="nameCode" width="150"></el-table-column>-->
<!--        <el-table-column label="物资名称" prop="cpmctymc" width="180" show-overflow-tooltip></el-table-column>-->
<!--        <el-table-column label="规格型号" prop="ggxh" show-overflow-tooltip width="180"></el-table-column>-->
<!--        <el-table-column label="批次号" prop="batchNo" width="120"></el-table-column>-->
<!--        <el-table-column label="价格" prop="price" width="120"></el-table-column>-->
<!--        <el-table-column label="入库数量" prop="inCount" width="120"></el-table-column>-->
<!--        <el-table-column label="出库数量" prop="outCount" width="120"></el-table-column>-->
<!--        <el-table-column label="结余数量" prop="reCount" width="120">-->
<!--        </el-table-column>-->
<!--        <el-table-column label="部门" prop="deptName" v-if="showSup"-->
<!--                         show-overflow-tooltip width="120"></el-table-column>-->
<!--        <el-table-column label="仓库" prop="invName" v-if="showSup"-->
<!--                         show-overflow-tooltip width="120"></el-table-column>-->
<!--        <el-table-column label="计量单位" prop="measname" width="120"></el-table-column>-->
<!--        <el-table-column label="生产日期" prop="productionDate" width="120"></el-table-column>-->
<!--        <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>-->


<!--        <el-table-column label="生产企业" prop="ylqxzcrbarmc" show-overflow-tooltip-->
<!--                         width="120"></el-table-column>-->
<!--        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" width="160" show-overflow-tooltip></el-table-column>-->
<!--        <el-table-column label="操作" width="80" fixed="right">-->
<!--          <template slot-scope="scope">-->
<!--            <el-button-->
<!--              type="text"-->
<!--              size="small"-->
<!--              @click.native.stop="detailDialog(scope.row)"-->
<!--            >详情-->
<!--            </el-button-->
<!--            >-->
<!--            &lt;!&ndash;            <el-button&ndash;&gt;-->
<!--            &lt;!&ndash;              type="text"&ndash;&gt;-->
<!--            &lt;!&ndash;              size="small"&ndash;&gt;-->
<!--            &lt;!&ndash;              @click.native.stop="deleteDialog(scope.row.id)"&ndash;&gt;-->
<!--            &lt;!&ndash;            >删除&ndash;&gt;-->
<!--            &lt;!&ndash;            </el-button&ndash;&gt;-->
<!--            &lt;!&ndash;            >&ndash;&gt;-->
<!--          </template>-->
<!--        </el-table-column>-->
        <template v-for="(item, index) in tableHeader">
          <el-table-column
            v-if="item.columnType == 'id' && executeEval(row,item.expression,true)"
            type="index" :label="item.columnDesc"></el-table-column>
          <el-table-column
            v-if="item.columnType == 'selection'"
            type="selection"
            :width="item.width"
            :selectable="(row,number) => executeFuc(row,'3',item.clickFuc)"
          ></el-table-column>

          <el-table-column
            v-if="item.columnType == 'radio' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template  slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="radioCheck"><span></span></el-radio>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'laber' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template   slot-scope="scope">
              <span :style="{color: executeFuc(scope.row,'4',item.lableRuleObj[scope.row[item.columnName]])}">{{ item.lableRuleObj[scope.row[item.columnName]] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'eltag' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template   slot-scope="scope">
              <el-tag :type="executeFuc(scope.row,'4',item,item.lableRuleObj?item.lableRuleObj[scope.row[item.columnName]]:scope.row[item.columnName])">
                <span>{{ item.lableRuleObj?item.lableRuleObj[scope.row[item.columnName]]:scope.row[item.columnName] }}</span>
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'button' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :width="item.width"
            :key="item.columnName"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button v-for="(buttonItem, buttonIndex) in item.buttonRulObj"
                         :type="buttonItem.type"
                         :size="buttonItem.size"
                         :style="buttonItem.style"
                         :key="buttonItem"
                         v-if="executeEval(scope.row,buttonItem.hasPermi,true)"
                         :disabled="executeEval(scope.row,buttonItem.disabledFuc,false)"
                         @click.native.stop="executeFuc(scope.row,'1',buttonItem.clickFuc)"
              >{{ buttonItem.name }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'text' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template slot-scope="scope">
              <span :style="{color: executeFuc(scope.row,'4',item,scope.row[item.columnName])}">{{ scope.row[item.columnName] }}</span>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

    <el-dialog
      title="库存详情"
      :visible.sync="detailDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="85%"
      v-if="detailDialogVisible"
    >
      <invPreProductsDetail
        :inputQuery="inputQuery"
        v-on:closeDetailDialog="closeDetailDialog"
      ></invPreProductsDetail>
    </el-dialog>

  </div>
</template>

<script>
import {findAllInvByUserOptimize} from "@/api/system/invWarehouse";
import {getInvPreProduct, deleteInvPreProduct} from "@/api/inventory/invPreProduct";
import {getBasicUnitMaintainsOptimize} from "@/api/basic/basicUnitMaintain";
import {isBlank} from "@/utils/strUtil";
import invPreProductsDetail from "@/views/inventory/InvPreProductsDetail.vue";
import {getInvSpaceListOptimize} from "@/api/inventory/invSpace";
import {selectUser} from "@/api/auth/authAdmin";
import {
  getHead,executeFuc
} from "@/utils/customConfig";

export default {
  name: "InvPreProducts",
  data() {
    return {
      showSearch: true,
      filterQuery: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        batchNo: null,
        supId: null,
        zczbhhzbapzbh: null,
        ylqxzcrbarmc: null,
        invCode: null,
        invSpaceCode: null,
        page: 1,
        limit: 10,
        filterNoInv: "1",
      },
      list: [],
      total: 0,
      tableHeader:[],
      queryList:[],
      fromList:[],
      options: {
        getInvList:[],
        findMethod:[],
        getSpaceList:[],
      },
      invList: [],
      invId: null,
      spaceList: [],
      loading: false,
      deleteData: {
        id: "",
        status: 10,
      },
      corpLoading: false,
      dialogVisible: false,
      fromOptions: [],
      showSup: false,
      detailDialogVisible: false,
      inputQuery: {
        relId: null,
        cpmctymc: null,
        nameCode: null,
        batchNo: null,
        ggxh: null,
        productionDate: null,
        expireDate: null,
        supId: null,
        deptCode: null,
        invCode: null,
        price: null,

      }
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        batchNo: null,
        supId: null,
        zczbhhzbapzbh: null,
        ylqxzcrbarmc: null,
        invCode: null,
        page: 1,
        limit: 10,
      };
      this.getList();
      this.findMethod(this);
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    handleSizeChange(val) {
      this.filterQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val;
      this.getList();
    },
    keyup_submit(_this,event) {
      _this.filterQuery.page = 1;
      _this.getList();
      event.target.select();
    },
    getList() {
      this.loading = true;
      // this.filterQuery.supId = this.$store.getters.customerId;
      getInvPreProduct(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    getInvList(_this) {
      let query = {
        advanceType: 3, //获取寄售库1：入账，2：预验收，3：寄售
      };
      findAllInvByUserOptimize(query)
        .then((response) => {
          _this.options.getInvList = response.data || [];
          for (var i = 0; i < _this.options.getInvList.length; i++) {
            if (_this.options.getInvList[i].advanceType == 3) {
              _this.filterQuery.invCode = _this.options.getInvList[i].code;
            }
          }
          _this.getList();
          _this.getSpaceList(_this);
        })
        .catch(() => {
        });
    },
    findMethod(_this,key) {
      _this.corpLoading = true;
      _this.fromOptions = [];
      let params = {
        key: key,
        corpType: 2,
        page: 1,
        limit: 20
      };
      getBasicUnitMaintainsOptimize(params).then((res) => {
        _this.corpLoading = false;
        _this.options.findMethod = res.data.list || [];
      }).catch(() => {
        _this.corpLoading = false;
      });
    },
    corpChange(_this,value) {
      if (!isBlank(value)) {
        _this.findMethod(_this,value);
      }
    },
    getcorp() {
      this.findMethod(this);
    },
    detailDialog(_this,row) {
      _this.detailDialogVisible = true;
      _this.inputQuery = {
        relId: row.relIdFk,
        cpmctymc: row.cpmctymc,
        nameCode: row.nameCode,
        batchNo: row.batchNo,
        ggxh: row.ggxh,
        productionDate: row.productionDate,
        expireDate: row.expireDate,
        supId: row.supId,
        deptCode: row.deptCode,
        invCode: row.invCode, price: row.price

      }
    },
    closeDetailDialog() {
      this.detailDialogVisible = false;
    },
    deleteDialog(id) {
      this.$confirm("此操作将永久删除此库存信息，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.deleteInvPreProduct(id);
      }).catch(() => {
      });
    },
    deleteInvPreProduct(id) {
      let params = {id: id};
      deleteInvPreProduct(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("删除成功！");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    getSpaceList(_this) {

      if (isBlank(_this.filterQuery.invCode)) {
        return;
      }
      let params = {
        invWarehouseCode: _this.filterQuery.invCode,
        status: 1
      };
      getInvSpaceListOptimize(params).then((res) => {
        _this.options.getSpaceList = res.data.list || [];
      })
    },
    querySearchAsync(queryString, cb) {
      let restaurants = this.spaceList;
      restaurants.forEach((item) => {
        item.value = item.name;
      })

      let results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
      if (!isBlank(queryString)) {
        this.spaceSearchResult = results;
      }
      cb(results);
    },
    invChange(_this) {
      _this.getSpaceList();
    },
    executeFuc(row,type,clickFuc,value){
      return executeFuc(this,row,type,clickFuc,value);
    },
    executeEval(row,expression,defaultRet){
      if(expression){
        return eval(expression);
      }
      return defaultRet;
    }
  },
  components: {
    invPreProductsDetail
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    selectUser().then((res) => {
      let supId = res.data.customerId
      if (supId == "110") {
        this.showSup = true;
      }
    }).catch(() => {
      this.corpLoading = false;
    });
    this.findMethod(this);
    this.getInvList(this);
    getHead("invPreProducts","1").then((re) => {
      // 处理返回的数据
      this.tableObj = re.data;
      this.tableHeader = re.data.tableList;
      this.queryList = re.data.queryList;
      this.fromList = re.data.fromList;
    });
    //this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

