<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" v-show="showSearch" label-width="auto">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="药品编码:" class="query-form-item">
              <el-input v-model="filterQuery.nameCode"
                        placeholder="请输入药品编码"
                        clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="药品通用名:" class="query-form-item">
              <el-input
                clearable
                v-model="filterQuery.cpmctymc"
                placeholder="请输入药品通用名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="医保编码:" class="query-form-item">
              <el-input
                clearable
                v-model="filterQuery.ybbm"
                placeholder="请输入医保编码"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="批准文号:" class="query-form-item">
              <el-input
                clearable
                v-model="filterQuery.zczbhhzbapzbh"
                placeholder="请输入批准文号"
              ></el-input>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8">-->
          <!--            <el-form-item label="医疗器械注册人:" class="query-form-item">-->
          <!--              <el-input-->
          <!--                clearable-->
          <!--                v-model="filterQuery.ylqxzcrbarmc"-->
          <!--                placeholder="请输入医疗器械注册人"-->
          <!--              ></el-input>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="生产企业:" class="query-form-item">
              <el-input
                clearable
                v-model="filterQuery.manufactory"
                placeholder="请输入生产企业"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="对照条件:" class="query-form-item">
              <el-select v-model="filterQuery.filterType" style="width: 100%" placeholder="对照条件" clearable>
                <el-option label="全部" :value="0"></el-option>
                <el-option label="对照完成" :value="1"></el-option>
                <el-option label="未对照产品编码" :value="2"></el-option>
                <!--<el-option label="未对照" :value="3"></el-option>-->
                <el-option label="未对照其他产品编码" :value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!--<el-col :span="8">-->
          <!--  <el-form-item label="是否禁用:" class="query-form-item">-->
          <!--    <el-select v-model="filterQuery.isDisable" clearable>-->
          <!--      <el-option label="是" :value=true></el-option>-->
          <!--      <el-option label="否" :value=false></el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
        </el-row>
        <el-divider/>
      </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 type="primary" icon="el-icon-plus" @click="chooseDrug">选入药品信息</el-button>-->
          <el-button type="primary" icon="el-icon-plus" @click="chooseHouseDrug">添加院内药品</el-button>
<!--          <el-button type="primary" icon="el-icon-plus" @click="addDrug">新增药品信息</el-button>-->
        </el-button-group>
      </div>

      <el-table v-loading="loading" :data="list" style="width: 100%" :row-style="{height: '32px' }"
      >
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="药品标识码" prop="nameCode" width="160"></el-table-column>
        <!--<el-table-column label="收费项目编码" prop="payFeeCode" width="100"></el-table-column>-->
        <el-table-column label="药品通用名称" prop="cpmctymc" width="160"></el-table-column>
        <el-table-column label="制剂规格" prop="prepnSpec" width="90"></el-table-column>
        <el-table-column label="制剂单位" prop="prepnUnit" width="90"></el-table-column>
        <el-table-column label="包装规格" prop="bzgg" width="100"></el-table-column>
        <el-table-column label="包装单位" prop="packUnit" width="90"></el-table-column>
        <!--<el-table-column label="包装比例" prop="packRatio" width="90"></el-table-column>-->
        <el-table-column label="价格" prop="price" width="90"></el-table-column>
        <el-table-column label="批准文号" prop="zczbhhzbapzbh" width="90"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" width="90"></el-table-column>
        <el-table-column label="医保编码" prop="ybbm" width="90"></el-table-column>
        <el-table-column label="药品状态" prop="majorStatus" width="90">
          <template slot-scope="scope">
            <el-tag :type="(scope.row.majorStatus) | statusFilterType">
              {{ statusMap[scope.row.majorStatus] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="editDrug(scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteDrug(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      >
      </pagination>

      <el-dialog
        :title="fromMap[fromName]"
        :visible.sync="addDrugVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="85%"
        v-if="addDrugVisible"
        @close="closeDialog"
      >
        <drugAdd
          :closeDialog="closeDialog"
          :rowData="rowData"
          :type="type"
          :relevanceEdit="relevanceEdit"
          :uuid="uuid"
          :relId="relId"
          :thisData="thisData"
        >
        </drugAdd>
      </el-dialog>

      <el-dialog
        title="选入-药品信息"
        :visible.sync="chooseDrugVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="70%"
        v-if="chooseDrugVisible"
      >
        <chooseDrug
        >
        </chooseDrug>
      </el-dialog>

      <el-dialog
        title="药品字典-从指定系统添加药品字典"
        :visible.sync="chooseHouseDrugVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="85%"
        v-if="chooseHouseDrugVisible"
      >
        <chooseHouseDrug
          :closeDialog="closeDialog"
          :uuid="uuid"
          :relId="relId"
          :isImport="isImport"
          :data="thisData"
          :defaultSys="defaultSys"
          :mainIds="mainIds"
          @closeUdi="closeDialog"
          :supEnable="false"
        ></chooseHouseDrug>
      </el-dialog>

    </el-card>

  </div>
</template>

<script>
import {
  getUdiInfos,
  deletetUdiInfos,
  updatetUdiInfos,
  updatetSingleUdiInfo,
  updateErpProduct,
  uploadSmp,
  thirdSysDetail,
  removeRl,
  sysFilterOptimize,
  spcombie,
  lockStatus,
  removeDiRl,
  disableUdi
} from '@/api/basic/product/udiRelevance'
import drugAdd from '@/views/basic/product/drug/drugAdd'
import chooseDrug from '@/views/basic/product/drug/chooseDrug'
import chooseHouseDrug from '@/views/basic/product/drug/chooseHouseDrug'

export default {
  components: {drugAdd, chooseDrug,chooseHouseDrug},
  name: 'drugMaintain',
  data() {
    return {
      showSearch: true,
      filterBadInv: true,
      query: {},
      list: [],
      mainIds:[],
      filterQuery: {
        productsType: 2,
        page: 1,
        limit: 20,
        addType: 1
      },
      addDrugVisible: false,
      chooseDrugVisible: false,
      statusMap: {
        0: "停用",
        1: "在用"
      },
      fromName: '',
      rowData: {},
      //1：新增；2：编辑
      type: null,
      fromMap: {
        'add': '新增-药品信息',
        'edit': '编辑-药品信息',
      },
      loading: false,
      chooseHouseDrugVisible: false,
      uuid: "111",
      relId: "",
      isImport: false,
      pId: 0,
      thisData: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
      },
      relevanceEdit: {
        id: null,
        thirdId: "",
        isDisable: null,
        isUseDy: false,
        isLock: null,
        isAdavence: null,
        allowNoBatch: null,
        allowNoExpire: null,
        allowNoProduct: null,
        allowNoSerial: null,
        pId: null,
      },
      total: 0

    }
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        productsType: 2,
        page: 1,
        limit: 20,
        addType: 1
      };
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList()
    },
    addDrug() {
      this.fromName = 'add'
      this.type = 1
      this.rowData = null
      this.addDrugVisible = true
    },
    chooseDrug() {
      this.chooseDrugVisible = true

    },
    chooseHouseDrug(val){
      this.defaultSys = null;
      this.uuid = val.uuid;
      this.relId = val.id;
      this.isImport = true;
      this.thisData = val;
      this.chooseHouseDrugVisible = true
    },
    getList() {
      getUdiInfos(this.filterQuery)
        .then((response) => {
          this.loading = false
          this.list = response.data.list || []
          this.mainIds = this.list.map(item => item.mainId);
          this.total = response.data.total || 0
        })
        .catch(() => {
          this.loading = false
          this.list = []
          this.total = 0
        })
    },
    deleteDrug(row) {
      this.$confirm("此操作将永久删除该药品信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let tquery = {
          id: row.id + "",
        };
        deletetUdiInfos(tquery).then(res => {
          if (res.code != 20000) {
            this.$message.error("删除错误")
            this.getList()
          } else {
            this.$message.success("删除成功！")
            this.getList()
          }
        }).catch(() => {
          this.$message.error("删除错误")
        })
      })
    },
    editDrug(row) {
      this.fromName = 'edit'

      this.type = 2
      this.uuid = row.uuid;
      this.relId = row.id;
      this.isImport = false;
      this.thisData = row;
      this.pId = row.id;
      this.relevanceEdit = {
        id: row.id,
        isUseDy: row.isUseDy,
        isDisable: row.isDisable,
        isLock: row.isLock,
        isAdavence: row.isAdavence,
      };
      this.editFormat = JSON.parse(JSON.stringify(row));
      this.rowData = row
      this.addDrugVisible = true;
    },
    closeUdi(){

    },
    closeDialog() {
      this.addDrugVisible = false;
      this.chooseHouseDrugVisible = false;
      this.getList()
    }
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        0: "info",
        1: "success",
      };
      return statusMap[status];
    }
    ,
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>

</style>
