<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.code" style="width: 90%" placeholder="请输入产品编码/商品条码/医保编码" clearable
                        @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" 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-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="注册/备案凭证:">
              <el-input v-model="filterQuery.registerNo" 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.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.thirdSys" placeholder="请选择第三方系统" @change="thirdSysChange"
                         style="width: 90%"
              >
                <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-row>
          <el-col :span="6">
            <el-form-item label="产品类型:">
              <el-select v-model="filterQuery.productType" placeholder="请选择产品类型"
                         style="width: 90%" clearable
              >
                <el-option
                  v-for="item in productTypes"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                  <span style="float: left">{{ item.label }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</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="search">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addProductVisible=true"
                     :disabled="!configParms.basicThirdProducts">新增
          </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="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="180px">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="detailDialog(scope.row)">编辑</el-button>
            <el-button type="text" @click.native.stop="deleteDialog(scope.row)" v-if="thirdSysDetail.fromType!=0"
                       :disabled="!configParms.basicThirdProducts">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>


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

    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="产品信息编辑"
      :visible.sync="thrProductsDetailVisible"
      width="60%"
      class="dialog-two"
      top="5vh"
      v-if="thrProductsDetailVisible"
    >
      <thrProductsEdit
        :newProductData="thisData"
        :closeAddDialog="closeAddDialog"
      ></thrProductsEdit>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="新增配送产品"
      :visible.sync="addProductVisible"
      width="60%"
      v-if="addProductVisible"
      @close="closeAddDialog"
      class="dialog-two"
      top="5vh"
    >

      <ThrProductsAdd
        :newType="newType"
        :closeAddDialog="closeAddDialog"></ThrProductsAdd>
    </el-dialog>
  </div>
</template>
<script>
import {
  delThrProducts, saveNewProduct
} from "@/api/thrsys/thrProducts";
import {getInvbasdoc} from "@/api/thrsys/getErps";
import {getBasicThirdSys, filterDetailByKey} from "@/api/thrsys/basicThirdSys";
import {selectIp} from "@/api/system/systemParamConfig";
import thrProductsEdit from "./thrProductsEdit";
import ThrProductsAdd from "@/views/thirdSys/product/thrProductsAdd";
import {findConfig} from "@/api/sync/spsSyncStatus";

export default {
  data() {
    return {
      showSearch: true,
      filterQuery: {
        code: null,
        name: "",
        spec: null,
        registerNo: null,
        manufactory: null,
        thirdSys: "thirdId",
        page: 1,
        limit: 20,
      },
      mainThirdSys: '',
      total: 0,
      thirdSys: [],
      thirdSysDetail: null,
      list: [],
      uploadFileUrl: null,
      fileList: [],
      uploadData: {
        thirdSys: "thirdId",
      },
      newType: 1,
      thrProductsDetailVisible: false,
      thisData: null,
      loading: false,
      addProductVisible: false,
      newProductData: {
        name: '',
        code: null,
        registerNo: null,
        manufactory: null,
        spec: null,
        measname: null,
        ylqxzcrbarmc: null,
        ylqxzcrbarywmc: null,
        cplb: null,
        flbm: null,
        sptm: null,
        ybbm: null,
        tyshxydm: null,
        qxlb: null,
        cplx: null,
        cpms: null
      },
      configParms: {},
      productTypes: [
        {
          value: 1,
          label: '耗材'
        }, {
          value: 2,
          label: '药品'
        }
      ],
    };
  },

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

    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      if (this.filterQuery.thirdSys == null) {
        this.$message.warning("请先选择第三方系统！")
        return;
      }
      this.loading = true;
      getInvbasdoc(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,
          };
          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 = [];
        });

    },
    thirdSysChange() {
      this.uploadData.thirdSys = this.filterQuery.thirdSys;
      this.getThirdSysDetail();
    },
    saveNewProduct() {
      this.addProductVisible = false;
      saveNewProduct(this.newProductData).then((res) => {
        if (res.code == 20000) {
          this.$message.success("添加成功");
          //清空弹窗数据
          Object.keys(this.newProductData).forEach(key => this.newProductData[key] = null);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      })
    },
    closeAddDialog(val) {
      this.thrProductsDetailVisible = false;
      this.addProductVisible = false;
      if (val) {
        this.getList();
      }
      Object.keys(this.newProductData).forEach(key => this.newProductData[key] = null);
    },
    getSyncConfig() {
      findConfig()
        .then((response) => {
          if (response.code == 20000) {
            this.configParms = response.data;
          }
        })
        .catch(() => {
        });
    },
  },
  components: {
    thrProductsEdit, ThrProductsAdd
  },
  mounted() {
  },
  created() {
    this.getList();
    this.getBasicThirdSys();
    this.getSyncConfig();
  },
};
</script>
<style scoped>
.el-form-item--mini.el-form-item {
  margin-bottom: 9px;
}
</style>
