<template>
    <div class="main-contain incomeStatistics">
        <div class="position">
            <bread-crumb :crumbs='crumbs'></bread-crumb>
        </div>
        <div class="app-container">
            <div style="display: flex;justify-content: space-between;margin-bottom:10px;">
                <div>
                    <el-input size="small" style="width:130px;" v-model="keywords"
                              class="filter-item"/>
                    <el-button size="small" style="margin-left:10px;" class="filter-item" type="primary"
                               @click="searchAction">
                        搜索
                    </el-button>
                    <el-date-picker v-model="query_start_time" prefix-icon="el-icon-date"
                                    @change="handleStartTimeChange" :editable="false" :clearable="false"
                                    style="width: 130px;" type="date" placeholder="选择开始日期"
                                    format="yyyy-MM-dd"
                                    value-format="yyyy-MM-dd"
                                    align="right"></el-date-picker>
                    -

                    <el-date-picker v-model="query_end_time" prefix-icon="el-icon-date"
                                    @change="handleEndTimeChange" :editable="false" :clearable="false"
                                    style="width: 130px;margin-right:10px;" type="date" placeholder="选择结束日期"
                                    format="yyyy-MM-dd"
                                    value-format="yyyy-MM-dd"
                                    align="right"></el-date-picker>


                    <label class="title">险种<span class="name"></span> : </label>
                    <el-select v-model="query_insutype" style="width:140px;margin-right:10px;" placeholder="请选择"
                               @change="changeInsutype">
                        <el-option
                                v-for="item in insutypes"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>


                    <label class="title">清算类别<span class="name"></span> : </label>
                    <el-select v-model="query_clr_type" style="width:100px;margin-right:10px;" placeholder="请选择"
                               @change="changeClrType">
                        <el-option
                                v-for="item in clrOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>

                </div>

            </div>
            <div style="float: right;margin-bottom: 10px">
                <el-button size="small" type="primary" @click="dialogFormVisible = true">医保对账</el-button>
                <el-button size="small" type="primary" @click="getFormData">下载对账报表</el-button>
            </div>


            <el-table :data="tableData" border style="width: 100%;" :row-style="{ color: '#303133' }"
                      :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}"
                      highlight-current-row
                      @select="selectCostInfo"
                      @selection-change="changeCostInfoTableData"
                      @select-all="changeAllCostInfoTableData"
                      row-key="row_key"
                      ref="multipleTable"
            >
                <el-table-column
                        align="center"
                        type="selection"
                        width="55"
                ></el-table-column>
                <el-table-column
                        prop="date"
                        label="序号"
                        width="60"
                        align="center"
                        type="index"
                >
                </el-table-column>
                <el-table-column align="center" prop="name" label="对账结果">
                    <template slot-scope="scope">{{'平'}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="操作机构">
                    <!--<template slot-scope="scope">{{// this.$store.getters.xt_user.org.org_name}}</template>-->
                </el-table-column>
                <el-table-column align="center" prop="name" label="对账申请人">
                    <template slot-scope="scope">{{getName(scope.row.creator)}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="开始时间">
                    <template slot-scope="scope">{{getTimes(scope.row.start_time)}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="结束时间">
                    <template slot-scope="scope">{{getTimes(scope.row.end_time)}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="险种类型">
                    <template slot-scope="scope">
                        <div v-if="scope.row.insutype == '310'">职工基本医疗保险</div>
                        <div v-if="scope.row.insutype == '320'">公务员医疗补助</div>
                        <div v-if="scope.row.insutype == '330'">大额医疗费用补助</div>
                        <div v-if="scope.row.insutype == '340'">离休人员医疗保障</div>
                        <div v-if="scope.row.insutype == '390'">城乡居民基本医疗保险</div>
                        <div v-if="scope.row.insutype == '392'">城乡居民大病医疗保险</div>
                        <div v-if="scope.row.insutype == '510'">生育保险</div>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="对账类型">
                    <template slot-scope="scope">
                        <div v-if="scope.row.check_type == 1">总账</div>
                        <div v-if="scope.row.check_type == 2">明细</div>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="清算经办机构">
                    <template slot-scope="scope">{{getName(scope.row.creator)}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="定点医药机构医疗费总额(元)">
                    <template slot-scope="scope">{{scope.row.setl_optins}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="医保中心医疗费总额(元)">
                    <template slot-scope="scope">{{scope.row.cost_total}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="定点医药机构基金总额(元)">
                    <template slot-scope="scope">{{scope.row.cost_total}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="医保中心基金总额(元)">
                    <template slot-scope="scope">{{scope.row.func_total}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="定点医药机构个人账号总额(元)">
                    <template slot-scope="scope">{{scope.row.func_total}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="医保中心个人账号总额(元)">
                    <template slot-scope="scope">{{scope.row.psn_pay}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="定点医药机构结算笔数">
                    <template slot-scope="scope">{{scope.row.psn_pay}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="医保中心结算笔数">
                    <template slot-scope="scope">{{scope.row.num}}</template>
                </el-table-column>

            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :page-sizes="[10, 50, 100]"
                    :page-size="10"
                    background
                    style="margin-top:20px;float: right"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
            >
            </el-pagination>
            <el-dialog title="医保对账" :visible.sync="dialogFormVisible">
                <el-form :model="form" label-width="100px">
                    <el-form-item label="时间" label-width="100px">
                        <el-date-picker v-model="start_time" prefix-icon="el-icon-date"
                                        @change="handleStartTimeChange" :editable="false" :clearable="false"
                                        style="width: 200px;" type="date" placeholder="选择开始日期"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        align="right"></el-date-picker>
                        -

                        <el-date-picker v-model="end_time" prefix-icon="el-icon-date"
                                        @change="handleEndTimeChange" :editable="false" :clearable="false"
                                        style="width: 200px;margin-right:10px;" type="date" placeholder="选择结束日期"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        align="right"></el-date-picker>
                    </el-form-item>
                    <el-form-item label="险种类型" :label-width="formLabelWidth">
                        <el-select v-model="insutype" placeholder="请选择" style="width: 200px;">
                            <el-option
                                    v-for="item in insutypes"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="对账类型" :label-width="formLabelWidth">
                        <el-select v-model="check_type" placeholder="请选择" style="width: 200px;">
                            <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定</el-button>
                </div>
            </el-dialog>
        </div>


    </div>
</template>


<script>
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import {
    getCheckAccountFormData,
    getDoctorList,
    getExportConsumeDetailList,
    getHisOrderList,
    getList
  } from '@/api/his/his'
  import { uParseTime } from '@/utils/tools'
  import { fetchAllAdminUsers } from '@/api/doctor'
  import axios from 'axios'

  export default {
    components: {
      BreadCrumb
    },
    data() {
      return {
        form: {},
        crumbs: [
          { path: false, name: '门诊收费' },
          { path: false, name: '医保费用对照' }
        ],
        tableData: [],
        limit: 10,
        page: 1,
        dialogFormVisible: false,
        total: 0,
        adminUserOptions: [],
        start_time: '',
        end_time: '',
        insutype: '',
        check_type: '',
        keywords: '',
        query_start_time: '',
        query_end_time: '',
        selection:[],
        query_insutype: '',
        query_clr_type: '',
        fromData: [],
        options: [
          {
            value: 1,
            label: '总账'
          }, {
            value: 2,
            label: '明细'
          }
        ],
        clrOptions: [
          {
            value: '11',
            label: '门诊'
          }, {
            value: '21',
            label: '住院'
          }, {
            value: '41',
            label: '药店购药'
          }, {
            value: '99',
            label: '其他'
          }
        ],
        insutypes: [
          {
            value: '310',
            label: '职工基本医疗保险'
          }, {
            value: '320',
            label: '公务员医疗补助'
          }, {
            value: '330',
            label: '大额医疗费用补助'
          }, {
            value: '340',
            label: '离休人员医疗保障'
          },
          {
            value: '390',
            label: '城乡居民基本医疗保险'
          }, {
            value: '392',
            label: '城乡居民大病医疗保险'
          }, {
            value: '510',
            label: '生育保险'
          }

          // 1617724800

        ]

      }
    },
    methods: {
      changeInsutype() {
        let params = {
          'page': 1,
          'limit': 10,
          'keywords': this.keywords,
          'insutype': this.query_insutype,
          'clr_type': "11",
          'start_time': this.query_start_time,
          'end_time': this.query_end_time,
        };
        this.tableData = [];
        getList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.tableData = response.data.data.list;
            this.total = response.data.data.total
          }
        })

      }, changeClrType(val) {
        if (val != "11"){
          this.tableData = [];
          this.total = 0
        }else{
          let params = {
            'page': 1,
            'limit': 10,
            'keywords': this.keywords,
            'insutype': this.query_insutype,
            'clr_type': "11",
            'start_time': this.query_start_time,
            'end_time': this.query_end_time,
          };
          this.tableData = [];
          getList(params).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg);
              return false
            } else {
              this.tableData = response.data.data.list;
              this.total = response.data.data.total
            }
          })
        }
      },
      handleStartTimeChange() {

        let params = {
          'page': 1,
          'limit': 10,
          'keywords': this.keywords,
          'insutype': "",
          'clr_type': "11",
          'start_time': this.query_start_time,
          'end_time': this.query_end_time,
        };
        this.tableData = [];
        getList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.tableData = response.data.data.list;
            this.total = response.data.data.total
          }
        })

      }, handleEndTimeChange() {

        let params = {
          'page': 1,
          'limit': 10,
          'keywords': this.keywords,
          'insutype': "",
          'clr_type': "11",
          'start_time': this.query_start_time,
          'end_time': this.query_end_time,
        };
        this.tableData = [];
        getList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.tableData = response.data.data.list;
            this.total = response.data.data.total
          }
        })

      },

      searchAction() {
        let params = {
          'page': 1,
          'limit': 10,
          'keywords': this.keywords,
          'insutype': "",
          'clr_type': "11",
          'start_time': "",
          'end_time': "",
        };
        this.tableData = [];
        getList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.tableData = response.data.data.list;
            this.total = response.data.data.total
          }
        })

      },
      selectCostInfo(selection, row) {
        this.selection = selection

      }, changeCostInfoTableData(val) {
        this.selection = val
      }, changeAllCostInfoTableData(selection) {
        this.selection = selection


      }, confirm() {
        if (this.check_type == 1) {
          let params = {
            start_time: this.start_time,
            end_time: this.end_time,
            insutype: this.insutype,
            clr_type: '11'
          };
          var that = this;
          if (this.$store.getters.xt_user.org_id == 4 || this.$store.getters.xt_user.org_id == 9919 || this.$store.getters.xt_user.org_id == 10106 || this.$store.getters.xt_user.org_id == 9990 ) {
            axios.get('http://127.0.0.1:9532/api/checkaccount/get', {
              params: params
            })
              .then(function(response) {
                if (response.data.state == 0) {
                  that.$message.error(response.data.msg);
                  return false
                } else {
                  var result_desc = '';

                  if (response.data.data.stmt_rslt == 0) {
                    result_desc = '对账平'
                  } else if (response.data.data.stmt_rslt == 1) {
                    result_desc = '中心多' + '\n' + response.data.data.stmt_rslt_dscr

                  } else if (response.data.data.stmt_rslt == 2) {
                    result_desc = '机构多' + '\n' + response.data.data.stmt_rslt_dscr
                  }

                  that.$alert(result_desc, '对账结果', {
                    confirmButtonText: '确定',
                    callback: action => {
                      this.dialogFormVisible = false
                    }
                  });

                  that.page = 1;
                  that.limit = 10;
                  that.keywords = '';
                  that.query_start_time = '';
                  that.query_end_time = '';
                  that.query_insutype = '';
                  that.query_clr_type = '';
                  that.getList()
                }
              })
              .catch(function(error) {

              })

          }

        } else {
          let params = {
            start_time: this.start_time,
            end_time: this.end_time
          };
          var that = this;
          axios.get('http://127.0.0.1:9532/api/checkdetailaccount/get', {
            params: params
          })
            .then(function(response) {
              if (response.data.state == 0) {
                that.$message.error(response.data.msg);
                return false
              } else {
                this.page = 1;
                this.limit = 10;
                this.keywords = '';
                this.getList()
              }
            })
            .catch(function(error) {

            })
        }
      },
      getInsutype(type) {

      },
      fetchAllAdminUsers() {
        fetchAllAdminUsers().then(response => {
          if (response.data.state == 1) {
            this.adminUserOptions = response.data.data.users

          }
        })
      },
      getName(admin_user_id) {
        for (let i = 0; i < this.adminUserOptions.length; i++) {
          if (this.adminUserOptions[i].id == admin_user_id) {
            return this.adminUserOptions[i].name
          }
        }

      },
      getTimes(time) {
        return uParseTime(time, '{y}-{m}-{d}')
      },
      getList() {
        let params = {
          'page': this.page,
          'limit': this.limit,
          'keywords': this.keywords,
          'insutype': this.query_insutype,
          'clr_type': this.query_clr_type,
          'start_time': this.query_start_time,
          'end_time': this.query_end_time
        };
        this.tableData = [];
        getList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.tableData = response.data.data.list;
            this.total = response.data.data.total
          }
        })

      },
      handleSizeChange(limit) {
        this.limit = limit;
        this.getList()
      },
      handleCurrentChange(page) {
        this.page = page;
        this.getList()

      }, getFormData() {
        if (this.selection.length == 0) {
          this.$message.error('请选择要下载的对账记录');
          return
        }

        let params = {
          'start_time': this.getTimes(this.selection[0].start_time),
          'end_time': this.getTimes(this.selection[0].end_time),
          'insutype': this.selection[0].insutype
        };
        getCheckAccountFormData(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.fromData = response.data.data.list;
            var list = [];

            for (let i = 0; i < this.fromData.length; i++) {
              let obj = {
                '结算金额': this.fromData[i].medfee_sumamt,
                '姓名': this.fromData[i].psn_name,
                '身份证': this.fromData[i].id_card_no,
                '个人编号': this.fromData[i].psn_no,
                '门诊流水号': this.fromData[i].mdtrt_id,
                '挂号日期': this.getTimes(this.fromData[i].settle_accounts_date),
                '起付线': this.fromData[i].act_pay_dedc,
                '统筹基金': this.fromData[i].fund_pay_sumamt,
                '个人支付': this.fromData[i].psn_cash_pay,
                '个账支付': this.fromData[i].acct_pay,
                '现金支付': this.fromData[i].cash_pay,
                '总费用': this.fromData[i].sumamt,
                '冲销标志': '已结算'
              };
              list.push(obj)
            }
            var insutype_name = '';
            if (this.query_insutype == '310') {
              insutype_name = '职工基本医疗保险'
            } else if (this.query_insutype == '390') {
              insutype_name = '城乡居民基本医疗保险'
            }

            import('@/vendor/Export2Excel').then(excel => {
              const tHeader = ['结算金额', '姓名', '身份证', '个人编号', '门诊流水号', '挂号日期', '起付线', '统筹基金', '个人支付', '个账支付', '现金支付', '总费用', '冲销标志'];
              const filterVal = ['结算金额', '姓名', '身份证', '个人编号', '门诊流水号', '挂号日期', '起付线', '统筹基金', '个人支付', '个账支付', '现金支付', '总费用', '冲销标志'];
              const data = this.formatJson(filterVal, list);
              excel.export_json_to_excel({
                header: tHeader,
                data,
                filename:this.getTimes(this.selection[0].start_time)+ '~' + this.getTimes(this.selection[0].end_time)+ insutype_name + '对账报表'
              })
            })
          }
        })

      }, formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      }
    },
    created() {
      this.getList();
      this.fetchAllAdminUsers()

    }
  }
</script>
