<template>
  <div>

    <el-card class="el-card">
      <el-form v-if="queryList && queryList.length > 0 && showSearch" :model="filterQuery" label-width="100px">
        <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"
                />
              </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"
                />
              </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 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="onSubmitFind">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="choose">选入</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="true" border
                @current-change="handMedicalClick"
                ref="multipleTable"
      >
        <el-table-column label width="45">
          <template slot-scope="scope">
            <el-radio :label="scope.row.id" v-model="chenck"><span></span></el-radio>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index" width="55"></el-table-column>
        <el-table-column label="医疗目录编码" prop="medCatalogCode" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="药品通用名" prop="genericName" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column label="药品剂型" prop="dosageForm" width="160"></el-table-column>
        <el-table-column label="药品规格" prop="medSpec" width="100"></el-table-column>
        <el-table-column label="包装规格" prop="packagingSpec" width="90"></el-table-column>
        <el-table-column label="包装材质" prop="packagingMaterial"></el-table-column>
        <el-table-column label="生产企业" prop="manufacturerName" width="90"></el-table-column>
        <el-table-column label="批准文号" prop="approvalNum" width="90"></el-table-column>
        <el-table-column label="药监本位码" prop="medStandardCode" width="90"></el-table-column>
        <el-table-column label="操作" width="80" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="handleDetailClick(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"
      ></pagination>
    </el-card>

    <el-dialog
      title="药品详情"
      :visible.sync="newSpDistributionVisible"
      width="80%"
      v-if="newSpDistributionVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <pharmaceuticalsDetail
        :rowData="rowData"
      >
      </pharmaceuticalsDetail>
    </el-dialog>


    <el-dialog
      title="医用药品详情"
      :visible.sync="drugDetailVisible"
      width="70%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      v-if="drugDetailVisible"
    >
      <pharmaceuticalsDetail
        :rowData="drugDetailData"
      >
      </pharmaceuticalsDetail>
    </el-dialog>

  </div>
</template>
<script>
import { delReceive } from '@/api/thrsys/thrOrderReceive'
import {
  getHead, executeFuc
} from '@/utils/customConfig'
import { chDrugList, deleteChDrug } from '@/api/thrsys/medicalSupplies'
import pharmaceuticalsDetail from "@/views/thirdSys/medicare/pharmaceuticalsDetail"

export default {
  props: {
    getData: {
      type: Function,
      required: true
    }
  },
  components: {pharmaceuticalsDetail},
  data() {
    return {
      defaultSort: { prop: 'createTime', order: 'desc' },
      showSearch: true,
      filterQuery: {
        medCatalogCode: '',
        tradeName: '',
        genericNum: '',
        genericName: '',
        page: 1,
        limit: 10
      },
      invList: [],
      formName: null,
      formMap: {
        add: '新增领用单据',
        update: '编辑领用单据'
      },
      statusMap: {
        1: '草稿',
        2: '未审核',
        3: '已审核',
        4: '已拒绝'
      },
      total: 0,
      tableHeader: [],
      queryList: [],
      fromList: [],
      fList: [],
      options: {
        finCurInv: [],
        finCorpList: []
      },
      thirdSys: [],
      thirdSysDetail: null,
      busTypes: [],
      originTypes: [],
      list: [],
      detailList: [],
      uploadFileUrl: null,
      fileList: [],
      uploadData: {
        thirdSys: 'thirdId'
      },
      invCodebe: null,
      closeConfirm: false,
      loading: false,
      actDateRange: [],
      newSpDistributionVisible: false,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      rowData: null,
      chenck: '',
      data: {},
      drugDetailVisible: false,
      drugDetailData: {}
    }
  },

  methods: {
    onReset() {
      this.$router.push({
        path: ''
      })
      this.filterQuery = {
        medCatalogCode: '',
        tradeName: '',
        genericNum: '',
        genericName: '',
        page: 1,
        limit: 10
      }
      this.getList()
    },

    onSubmitFind() {
      this.filterQuery.page = 1
      this.getList()
    },
    getList() {
      chDrugList(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)
          }
        })
        .catch(() => {
          this.list = []
          this.total = 0
        })
    },

    handleDetail(_this, row) {
      _this.rowData = row
      _this.newSpDistributionVisible = true
    },
    hideSearch() {
      this.showSearch = !this.showSearch
    },
    deleteClick(_this, row) {
      _this.$confirm('此操作将永久删除该药品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteChDrug(row).then(res => {
            if (res.code == 20000) {
              _this.$message.success('success')
              _this.getList()
            } else {
              _this.$message.error('删除失败')
            }
          })
        })
        .catch(() => {
        })
    },
    handMedicalClick(row) {
      this.chenck = row.id
      this.data = row
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page
      this.getList()
    },
    closeDialog() {
      this.newSpDistributionVisible = false
      this.getList()
      this.detailList = []
    },
    executeFuc(row, type, clickFuc, value) {
      return executeFuc(this, row, type, clickFuc, value)
    },
    executeEval(row, expression, defaultRet) {
      if (expression) {
        return eval(expression)
      }
      return defaultRet
    },
    /**
     * 选入
     */
    choose() {
      this.getData(this.data)
    },
    handleDetailClick(row){
      this.drugDetailData = row
      this.drugDetailVisible = true
    }
  },
  // components: {
  //   pharmaceuticalsDetail
  // },
  mounted() {
  },
  created() {
    this.getList()
    getHead('pharmaceuticals-1', '1').then((re) => {
      // 处理返回的数据
      this.tableHeader = re.data.tableList
      this.queryList = re.data.queryList
      this.fromList = re.data.fromList

    })

  }
}
</script>
<style scoped>
.itemTag {
  float: left;
  text-align: left;
  margin-top: 10px;
  width: 25%;
}

.text {
  font-size: 13px;
  font-family: "Microsoft YaHei";
}


.el-row {
  display: flex;
  flex-wrap: wrap;
}

.el-col {
  border-radius: 4px;
  flex-wrap: wrap;
}
</style>

