<template>
  <div>
    <crumbs title="开单合理性规则维护" style="min-width: 980px">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="条件明细：">
          <el-input size="mini" v-model="filter.conditionName" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="开单项目：">
          <el-input size="mini" v-model="filter.conceptName" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态：" class="selectMedicine">
          <el-select size="mini" v-model="filter.relationStatus" placeholder="请选择" clearable>
            <el-option v-for="item in stateList" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="dododo">
          <el-button size="mini" @click="filterDatas">检索</el-button>
          <el-button size="mini" type="warning" @click="addRelation">添加规则</el-button>
        </el-form-item>
      </el-form>
    </crumbs>
    <div class="contents">
      <el-table :data="list" border style="width: 100%">
        <el-table-column :resizable="false" type="index" :index="indexMethod" label="编号" width="80"></el-table-column>
        <el-table-column :resizable="false" prop="ruleName" label="规则名称" width="180"></el-table-column>
        <el-table-column :resizable="false" prop="conditionName" label="条件明细" show-overflow-tooltip></el-table-column>
        <el-table-column :resizable="false" prop="conceptName" label="开单项目" show-overflow-tooltip></el-table-column>
        <el-table-column :resizable="false" prop="relationName" label="开单类型" show-overflow-tooltip></el-table-column>
        <el-table-column :resizable="false" prop="ruleType" label="规则类型" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{scope.row.ruleType === 1 ? '文本类型' : ''}}</span>
            <span>{{scope.row.ruleType === 2 ? '数值类型' : ''}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="relationStatus" label="状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{scope.row.relationStatus === 1 ? '启用' : '禁用'}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.relationStatus===0"
              type="text"
              size="small"
              class="is-disabled"
            >修改</el-button>
            <el-button
              v-if="scope.row.relationStatus==1"
              type="text"
              size="small"
              @click="toEditProduct(scope.row)"
            >修改</el-button>

            <span style="margin:0 3px;">|</span>
            <el-button
              v-if="scope.row.relationStatus===0"
              type="text"
              size="small"
              @click="showReuseDialog(scope.row,'Reuse')"
            >启用</el-button>
            <el-button
              v-if="scope.row.relationStatus===1"
              type="text"
              size="small"
              class="delete"
              @click="showReuseDialog(scope.row,'Del')"
            >禁用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination pagepage">
        <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>
  </div>
</template>

<script>
import api from '@api/icss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';
export default {
  name: 'Bill',
  data() {
    return {
      list: [],
      searched: false,
      filter: {
        conditionName: '', //条件明细
        conceptName: '', //开单项目
        relationStatus: ''
      },
      stateList: [
        {
          id: "1",
          name: '启用'
        },
        {
          id: "0",
          name: '禁用'
        }
      ],
      currentPage: 1,
      pageSize: config.pageSize,
      pageSizeArr: config.pageSizeArr,
      pageLayout: config.pageLayout,
      total: 0
    };
  },
  created() {
    const that = this;
    //返回时避免参数未赋值就获取列表
    setTimeout(function() {
      that.clearFilter();
      that.getDataList();
    });
    // 非首页 编辑页返回 设置 this.currentPage
    if (Object.keys(this.$route.params).length !== 0) {
      this.currentPage = this.$route.params.currentPage;
    }
  },
  methods: {
    toEditProduct(row) {
      let ruleData = {};
      api.getRecordByRelationId({ relationId: row.relationId }).then(res => {
        // console.log(res, '============res');
        if (res.data.code === '0') {
          ruleData = res.data.data;
          const pam = this.searched
            ? {
                currentPage: this.currentPage,
                pageSize: this.pageSize,
                filter: this.filter
              }
            : { currentPage: this.currentPage, pageSize: this.pageSize };
          this.$router.push({
            name: 'AddBill',
            params: Object.assign(pam, { data: ruleData, isEdit: true })
          });
        }
      });
    },

    showReuseDialog(row, type) {
      let showInfo = type !== 'Del' ? '启用' : '禁用';
      let status = type === 'Del' ? 0 : 1;
      this.showConfirmDialog(
        `确定要${showInfo}该规则吗？`,
        () => {
          api
            .updateBillmanStatus({ relationId: row.relationId, status })
            .then(res => {
              if (res.data.code == '0') {
                this.currentPage = 1; //恢复数据跳转到筛选条件下首页
                console.log(this.currentPage);
                this.warning(res.data.msg || '操作成功', 'success');
                this.getDataList();
              } else {
                this.warning(res.data.msg);
              }
            })
            .catch(error => {
              if (error.code === '900010001') {
                return false;
              }
              this.warning(error);
            });
        },
        type
      );
    },
    showConfirmDialog(msg, resolve, type) {
      let showInfo = '启用';
      let btnNameClass = 'confirmBtn1';
      if (type === 'Del') {
        showInfo = '禁用';
        btnNameClass = 'delBtn';
      }

      this.$confirm(msg, '提示', {
        confirmButtonText: showInfo,
        cancelButtonText: '取消',
        cancelButtonClass: 'cancelBtn',
        confirmButtonClass: btnNameClass,
        type: 'warning'
      })
        .then(() => {
          resolve();
        })
        .catch(() => {});
    },
    // 获取列表数据
    getDataList(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.getBillmanPage(params).then(res => {
        loading.close();
        if (res.data.code === '0') {
          this.list = res.data.data && res.data.data.content;
        }
        this.total = res.data.data && res.data.data.totalElements;
        if (this.inCurrentPage !== undefined) {
          this.currentPage = this.inCurrentPage;
          this.inCurrentPage = undefined;
        }
      });
    },

    // 处理列表请求数据参数
    getFilterItems(isTurnPage) {
      //翻页时筛选条件没点确定则清空
      if (isTurnPage && !this.searched) {
        this.clearFilter();
      }
      const param = {
        number: this.inCurrentPage - 1 || this.currentPage - 1,
        size: this.pageSize,
        conditionName: this.filter.conditionName.trim(),
        conceptName: this.filter.conceptName.trim(),
        relationStatus: this.filter.relationStatus
        // uniqueCode: ''
      };
      return param;
    },
    // 清空搜索参数
    clearFilter() {
      this.filter = {
        conditionName: '',
        conceptName: '',
        relationStatus: ''
      };
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = utils.getCurrentPage(
        this.currentPage,
        this.total,
        this.pageSize
      );
      this.getDataList();
    },
    filterDatas() {
      this.currentPage = 1;
      this.getDataList();
    },
    addRelation() {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({ name: 'AddBill', params: pam });
    },
    // 修改诊断关联-跳转至编辑页面
    modifyRelation(row) {
      const item = Object.assign({}, row);
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({
        name: 'AddDept',
        params: Object.assign(pam, { isEdit: true, data: item })
      });
    },
    currentChange(next) {
      this.currentPage = next;
      this.getDataList(true);
      // if (this.cacheData[next]) {       //如果已请求过该页数据，则使用缓存不重复请求
      //     this.list = this.cacheData[next];
      // } else {
      //     this.getDataList();
      // }
    },

    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    getTagType(val) {
      return val;
    },
    warning(msg, type) {
      this.$message({
        showClose: true,
        message: msg,
        type: type || 'warning'
      });
    }
  }
};
</script>

<style lang="less">
@import '../../../less/admin.less';
.delete {
  color: red;
}
.delete:hover {
  color: red;
}
.pagination {
  min-width: 1010px;
}
.downTemplate {
  margin-right: 8px;
  span {
    color: #02a7f0;
  }
}
#upFile {
  display: none !important;
}
.el-message-box {
  /deep/.cancelBtn {
    background-color: #d7d7d7;
    border-color: transparent;
  }
  /deep/.confirmC {
    background-color: #ff545b !important;
    border-color: transparent !important;
  }
  /deep/.el-message-box__header {
    border-bottom: 1px solid #dcdfe6;
  }
}
.exportBox6 {
  /deep/ .el-message-box__btns {
    margin-top: 20px;
  }
  /deep/ .el-message-box__message {
    // text-align: center;
  }
  /deep/.leftbtn {
    background-color: #d7d7d7;
    border-color: transparent !important;
  }
  /deep/ .el-message-box__header {
    border-bottom: 1px solid #dcdfe6;
  }
}
.exportConfirm {
  .cancelSure {
    display: none;
  }
}
</style>