<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="产品编码:">
              <el-input v-model="filterQuery.code" placeholder="请输入产品编码/商品条码/医保编码" style="width: 90%" @keyup.enter.native="keyupErp_submit($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品名称:">
              <el-input v-model="filterQuery.name" 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="true" 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.registerNo" clearable="true" style="width: 90%" placeholder="请输入注册/备案凭证"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="生产厂家:">
            <el-input v-model="filterQuery.manufactory" clearable="true" style="width: 90%" placeholder="请输入生产厂家"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="外部系统:">
            <el-select v-model="filterQuery.thirdSys" style="width: 90%" placeholder="请选择第三方系统" @change="thirdSysChange">
              <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-upload2" @click="selectExport" v-if="selectType==0">选中导出</el-button>
          <el-button type="primary" icon="el-icon-upload2" @click="allExport" v-if="selectType==0">结果全部导出</el-button>
          <el-button type="primary" icon="el-icon-top-upload" @click="uploadSmp" v-if="selectType==1">选中上传</el-button>
          <el-button type="primary" icon="el-icon-top-upload" @click="allUploadSmp" v-if="selectType==1">一键上传</el-button>
          <el-button type="primary" icon="el-icon-download" @click="downloadErp" v-if="selectType==3">选中下载</el-button>
          <el-button type="primary" icon="el-icon-download" @click="downloadAllErp" v-if="selectType==3">结果全部下载</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>


      <el-table v-loading="loading" :data="list" style="width: 100%" @selection-change="handleSelectionChange" border
                highlight-current-row>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index" width="80"></el-table-column>
        <el-table-column label="产品编码" prop="code"></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="measname" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册证号" prop="registerNo" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产厂家" prop="manufactory" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="60">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="detailDialog(scope.row)">详情</el-button>
            <!--<el-button type="text" size="small" @click.native.stop="deleteDialog(scope.row)"-->
            <!--&gt;删除-->
            <!--</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"
      ></pagination>
    </el-card>
    <el-dialog
      title="产品信息详情"
      :visible.sync="thrProductsDetailVisible"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      v-if="thrProductsDetailVisible"
    >
      <thrProductsDetail
        :data="thisData"
      ></thrProductsDetail>
    </el-dialog>
  </div>
</template>
<script>
import {
  delThrProducts, delAll, downloadAll, productsDlAll
} from "@/api/thrsys/thrProducts";
import {getInvbasdoc} from "@/api/thrsys/getErps";
import {getBasicThirdSys, filterDetailByKey} from "@/api/thrsys/basicThirdSys";
import {selectIp} from "@/api/system/systemParamConfig";
import thrProductsDetail from "./thrProductsDetail";
import {exportExcel} from "@/api/thrsys/thrProductsExport"
import {thrProductUpload} from "@/api/thrsys/smp"

export default {
  name: "thrProductsSelect",
  props: {
    selectType: {
      type: Object,
      required: true,
    },
    isDownThrSys: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      showSearch: true,
      filterQuery: {
        thrProductsEntities: [],
        code: null,
        name: "",
        thirdSys: null,
        spec: null,
        manufactory: null,
        registerNo: null,
        page: 1,
        limit: 10,
        isDownThrSys: null,
      },
      mainThirdSys:'',
      total: 0,
      thirdSys: [],
      thirdSysDetail: null,
      list: [],
      uploadFileUrl: null,
      fileList: [],
      uploadData: {
        thirdSys: "thirdId",
      },
      thrProductsDetailVisible: false,
      thisData: null,
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        thrProductsEntities: [],
        code: null,
        name: "",
        thirdSys: this.mainThirdSys,
        page: 1,
        limit: 20,
      };
      this.getList();
    },

    getList() {
      this.loading = true;
      this.filterQuery.isDownThrSys = this.isDownThrSys;
      getInvbasdoc(this.filterQuery)
        .then((response) => {
          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;
        });
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    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(() => {
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除该产品信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let dQuery = {
            id: row.id,
          };
          delThrProducts(dQuery)
            .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(() => {
        });
    },

    detailDialog(row) {
      this.thisData = row;
      this.thrProductsDetailVisible = true;
    },
    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 = [];
        });
    },
    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys,
        key: "piQueryUrl",
      };
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });

    },
    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("文件上传成功,请稍后刷新查看！");
      }
    },
    selectSysParam() {
      let query = {
        key: "thirdIpUrl",
        thirdSysFk: this.filterQuery.thirdSys
      };
      selectIp(query).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/pi/upload";
        }
      });
    },
    thirdSysChange() {
      this.uploadData.thirdSys = this.filterQuery.thirdSys;
      this.getThirdSysDetail();
    },
    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);
        }
      });

    },
    uploadSmp() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.thrProductsEntities.push(obj);
      });
      this.allUploadSmp();
    },
    allUploadSmp() {
      thrProductUpload(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message.success(response.data);
            this.$emit("cancelDialog", true);
          } else {
            this.$message.error(response.message);
            this.$emit("cancelDialog", true);
          }
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "上传失败",
          });
        });
    },

    downloadErp() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.thrProductsEntities.push(obj);
      });
      this.downloadAllErp();
    },
    downloadAllErp() {
      productsDlAll(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message.success(response.data);
            this.$emit("cancelDialog", true);
          } else {
            this.$message.error(response.message);
            this.$emit("cancelDialog", true);
          }
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "下载失败",
          });
        });
    },


    selectExport() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.thrProductsEntities.push(obj);
      });
      this.allExport();
    },
    allExport() {
      exportExcel(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message({
              type: "success",
              message: "导出成功，后台正在生成Json文件，请稍后刷新查看！",
            });
            this.$emit("cancelDialog", true);
          } else {
            this.$message({
              type: "error",
              message: response.message,
            });
            this.$emit("cancelDialog", true);
          }
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "上传失败",
          });
        });
    },
  },
  components: {
    thrProductsDetail
  },
  mounted() {
  },
  created() {
    this.getBasicThirdSys();

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