<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="产品编码:">
              <el-input v-model="filterQuery.inventoryCode" clearable style="width: 90%" placeholder="请输入产品编码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品名称:">
              <el-input v-model="filterQuery.inventoryName" clearable style="width: 90%" placeholder="请输入产品名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="规格型号:">
              <el-input v-model="filterQuery.spec" clearable style="width: 90%" placeholder="请输入规格型号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="120px"  label="注册/备案凭证号:">
              <el-input v-model="filterQuery.registerCertNo" clearable style="width: 90%" placeholder="请输入注册/备案凭证号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="生厂厂家:">
              <el-input v-model="filterQuery.manufactory" clearable style="width: 90%" placeholder="请输入生厂厂家"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="库存:">
              <el-select v-model="filterQuery.filterCount" style="width: 90%" placeholder="过滤条件">
                <el-option label="全部" value="0"></el-option>
                <el-option label="库存等于0" value="1"></el-option>
                <el-option label="库存大于0" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="外部系统:">

              <el-select v-model="filterQuery.thirdSys" style="width: 90%" placeholder="请选择第三方系统">
                <el-option v-for="item in thirdSys"
                  :key="item.value"
                  :label="item.thirdName"
                  :value="item.thirdId">
                  <span style="float: left">{{ item.thirdName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>


      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <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="getList">查询</el-button>
          <el-button type="primary" icon="el-icon-delete-solid" @click="clearAll"
                     v-if="!this.thirdSysDetail.enabled">清空全部
          </el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="70"></el-table-column>
        <el-table-column label="产品编码" prop="code" show-overflow-tooltip></el-table-column>
        <el-table-column label="产品名称" prop="name" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="spec" show-overflow-tooltip></el-table-column>
        <el-table-column label="批次号" prop="batchNo" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产日期" prop="manufacturingDate" show-overflow-tooltip></el-table-column>
        <el-table-column label="失效日期" prop="expirationDate" show-overflow-tooltip></el-table-column>
        <el-table-column label="数量" prop="count" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册证号" prop="registerCertNo" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" show-overflow-tooltip></el-table-column>
        <el-table-column label="仓位" prop="warehouseName" show-overflow-tooltip></el-table-column>
        <el-table-column label="货位" prop="spaceName" show-overflow-tooltip></el-table-column>


        <el-table-column label="操作" v-if="thirdSysDetail.fromType!=0" width="60">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="deleteDialog(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {delInvProducts, delAll, downloadAll} from "@/api/thrsys/thrInvProducts";
import {getBasicThirdSys, filterDetailByKey} from "@/api/basic/basicThirdSys";
import {selectIp} from "@/api/param/systemParamConfig";
import {getOnhands} from "@/api/inventory/onHand.js";

export default {
  data() {
    return {
      showSearch: true,
      filterQuery: {
        inventoryCode: null,
        inventoryName: null,
        spec: null,
        registerCertNo: null,
        manufactory: null,
        thirdSys: null,
        page: 1,
        limit: 100,
        filterCount: null,
      },
      mainThirdSys:'',
      total: 0,
      list: [],
      thirdSys: [],
      thirdSysDetail: null,
      fileList: [],
      uploadData: {
        thirdSys: "thirdId",
      },
      uploadFileUrl: null,
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        inventoryCode: null,
        inventoryName: null,
        thirdSys: this.mainThirdSys,
        spec: null,
        registerCertNo: null,
        manufactory: null,
        page: 1,
        limit: 100,
        filterCount: null,
      };
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      if (this.filterQuery.thirdSys == null) {
        this.$message.warning("请先选择第三方系统！")
        return;
      }
      this.loading = true;
      getOnhands(this.filterQuery)
        .then((response) => {
          this.loading = false;
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除该产品信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {

          let dQuery = {
            id: row.id,
          };
          delInvProducts(dQuery)
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
                this.getList();
              } else {
                this.$message.error(response.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });

        })
        .catch(() => {
        });
    },
    clearAll() {
      this.$confirm("此操作将清空所有库存产品信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delAll()
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
                this.getList();
              } else {
                this.$message.success(response.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });

        })
        .catch(() => {
        });
    },
    handleChange(response, files, fileList) {
      console.log(response);
      if (response.code != 20000) {
        this.$message.error(response.message);
      } else {
        console.log(files[0] + "\n" + this.fileList[0] + "\n" + fileList[0]);
        this.$message.success("文件上传成功,请稍后刷新查看！");
      }
    },
    getBasicThirdSys() {
      let query = {
        enabled: true,
      };
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
          for(var i=0;i<this.thirdSys.length;i++){
            if(this.thirdSys[i].mainSys){
              this.filterQuery.thirdSys = this.thirdSys[i].thirdId;
              this.mainThirdSys = this.thirdSys[i].thirdId;
            }
          }
          this.uploadData.thirdSys = this.filterQuery.thirdSys;
          this.getThirdSysDetail();
          this.selectSysParam();
          this.getList();
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
    selectSysParam() {
      let query = {
        key: "thirdIpUrl",
        thirdSysFk: this.filterQuery.thirdSys
      };
      selectIp(query).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/invpi/upload";
        }
      });
    },
    thirdSysChange() {
      this.uploadData.thirdSys = this.filterQuery.thirdSys;
      this.getThirdSysDetail();
    },
    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys,
        key: "invPiUrl",
      };
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });

    },
    downloadDatas() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys
      };
      downloadAll(query).then((response) => {
        if (response.code == 20000) {
          this.$message.success(response.data);
        } else {
          this.$message.error(response.message);
        }
      });

    },
  },
  components: {},
  mounted() {
  },
  created() {
    this.getBasicThirdSys();

  },
};


</script>

<style scoped>
.el-form-item--mini.el-form-item {
  margin-bottom: 9px;
}
</style>
