<template>
  <div>
    <crumbs title="注册药品维护" :minWidth="titleWidth" class="knowledgeTitle">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="通用名：">
          <el-input size="mini" v-model="filter.commonName" placeholder="输入通用名" clearable></el-input>
        </el-form-item>
        <el-form-item label="注册名：">
          <el-input size="mini" v-model="filter.name" placeholder="输入注册名" clearable></el-input>
        </el-form-item>
        <el-form-item label="企业：">
          <el-input size="mini" v-model="filter.company" placeholder="输入企业名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="filterDatas">确认</el-button>
        </el-form-item>
        <el-form class="secLine">
          <el-form-item>
            <el-button size="mini" @click="addRule" type="warning" style="margin:0 10px">+ 新增药品</el-button>
            <el-button size="mini" @click="update">更新数据</el-button>
          </el-form-item>
        </el-form>
      </el-form>
    </crumbs>
    <div class="contents knowledgeContents">
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" :index="indexMethod" label="编号" width="60"></el-table-column>
        <el-table-column prop="drugCode" label="药品代码" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" label="注册名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="drugName" label="关联的通用名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="enName" label="英文名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="tradeName" label="商品名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="form" label="药品剂型" show-overflow-tooltip></el-table-column>
        <el-table-column prop="specification" label="注册规格" show-overflow-tooltip></el-table-column>
        <el-table-column prop="minPackQuantity" label="最小包装量" show-overflow-tooltip></el-table-column>
        <el-table-column prop="minPackUnit" label="最小包单位" show-overflow-tooltip></el-table-column>
        <el-table-column prop="company" label="药品企业" show-overflow-tooltip></el-table-column>
        <el-table-column prop="approval" label="批准文号" show-overflow-tooltip></el-table-column>
        <el-table-column prop="standardCode" label="药品本位码" show-overflow-tooltip></el-table-column>
        <el-table-column prop="insuranceType" label="医保类型" show-overflow-tooltip></el-table-column>
        <el-table-column prop="insuranceRemrk" label="医保备注" show-overflow-tooltip></el-table-column>
        <el-table-column prop="modifierName" label="操作人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="gmtModified" label="操作时间" width="180"></el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editData(scope.row)">修改</el-button>
            <span style="margin:0 3px;">|</span>
            <el-button type="text" size="small" class="delete" @click="showDelDialog(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        @current-change="currentChange"
        background
        :page-size="pageSize"
        :page-sizes="pageSizeArr"
        @size-change="handleSizeChange"
        :layout="pageLayout"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import api from '@api/icss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';

export default {
  name: 'ZskRegisteredDrug',
  data: function() {
    return {
      list: [],
      stateSelect: [
        { id: 1, name: '启用' },
        { id: 0, name: '禁用' }
      ],
      ruleTypeList: [],
      searched: false,
      filter: {
        commonName: '',
        name: '',
        company: ''
      },
      cacheData: {},
      currentPage: 1,
      pageSize: config.pageSize,
      pageSizeArr: config.pageSizeArr,
      pageLayout: config.pageLayout,
      total: 0,
      titleWidth: '1070px' //头部最小宽度
    };
  },
  created() {
    this.getDataList();
  },
  watch: {
    filter: {
      handler: function() {
        this.searched = false;
      },
      deep: true
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = utils.getCurrentPage(
        this.currentPage,
        this.total,
        this.pageSize
      );
      this.getDataList();
    },
    addRule() {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({ name: 'AddZskRegisteredDrug', params: pam });
    },
    filterDatas() {
      this.currentPage = 1;
      this.getDataList(1);
    },
    getDataList(flag, isTurnPage) {
      const params = this.getFilterItems(isTurnPage);
      this.searched = true;
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      api
        .getPageList(params)
        .then(res => {
          loading.close();
          if (res.data.code == '0') {
            const data = res.data.data;
            for (let j = 0; j < data.records.length; j++) {
              data.records[j].condition =
                data.records[j].parStatus == '1' ? '启用' : '禁用';
            }
            this.list = data.records;
            if (!flag) {
              //搜索时不缓存
              this.cacheData[params.current] = data.records;
            } else {
              this.cacheData = {};
            }
            this.total = data.total;
            if (this.inCurrentPage !== undefined) {
              this.currentPage = this.inCurrentPage;
              this.inCurrentPage = undefined;
            }
          } else {
            this.warning(res.data.msg || '获取列表数据失败');
          }
        })
        .catch(error => {
          loading.close();
          console.log(error);
        });
    },
    getFilterItems(isTurnPage) {
      //翻页时筛选条件没点确定则清空
      if (isTurnPage && !this.searched) {
        this.clearFilter();
      }
      const param = {
        current: this.inCurrentPage || this.currentPage,
        size: this.pageSize,
        commonName: this.filter.commonName,
        name: this.filter.name,
        company: this.filter.company
      };
      return param;
    },
    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    currentChange(next) {
      this.currentPage = next;
      this.getDataList(1, true);
    },
    warning(msg, type) {
      this.$message({
        showClose: true,
        message: msg,
        type: type || 'warning'
      });
    },
    showConfirmDialog(msg, resolve) {
      this.$alert(msg, '提示', {
        confirmButtonText: '确定',
        type: 'warning'
      })
        .then(() => {
          resolve();
        })
        .catch(() => {});
    },
    editData(row) {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({
        name: 'AddZskRegisteredDrug',
        params: {
          id: row.id,
          type: 2
        }
      });
    },
    showDelDialog(row) {
      const params = {
        id: row.id
      };
      const warningTxt = '是否删除该诊断依据？可能对现有系统造成影响';
      this.showConfirmDialog(warningTxt, () => {
        api.paramDelete(params)
          .then(res => {
            if (res.data.code == '0') {
              if (!this.searched) {
                //未点确认时清空搜索条件
                this.clearFilter();
              }
              this.currentPage = 1;
              this.warning(res.data.msg || '操作成功', 'success');
              this.getDataList();
            } else {
              this.warning(res.data.msg);
            }
          })
          .catch(error => {
            this.warning(error);
          });
      });
    },
    clearFilter() {
      this.filter = {
        commonName: '',
        name: '',
        company: ''
      };
    },
    update() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      api
        .getPageList()
        .then(res => {
          loading.close();
          if (res.data.code == '0') {
            this.warning('更新成功', 'success');
            this.getDataList();
          } else {
            this.warning(res.data.msg || '更新失败，请重试');
          }
        })
        .catch(error => {
          loading.close();
          this.warning('更新失败，请重试');
        });
    }
  }
};
</script>

<style lang="less" scoped>
@import '../../less/admin.less';
/deep/ .container.knowledgeTitle {
  height: 40px;
  min-width: 970px !important;
}
.demo-form-inline {
  margin: 0 20px 0 0;
}
/deep/ .contents.knowledgeContents {
  padding: 64px 20px 0;
}
/deep/ .secLine.el-form {
  float: right;
  display: block;
  position: relative;
  top: -1px;
}
.delete {
  color: red;
}
.review {
  color: #22ccc8;
}
.el-table .cell {
  overflow: hidden;
  white-space: nowrap;
}
#upFile {
  display: none !important;
}
.unvailable {
  color: #fe7d3d;
  &:hover,
  &:active,
  &:focus {
    color: #f19061;
  }
}
</style>