<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:150px;" v-model="keywords"
                              class="filter-item"/>
                    <el-button size="small" style="margin-left:10px;" class="filter-item" type="primary"
                               @click="searchAction">
                        搜索
                    </el-button>
                    <!--<el-select v-model="is_put_on_record" style="width:160px;" placeholder="请选择" @change="changeStatus">-->
                        <!--<el-option-->
                                <!--v-for="item,index in datas"-->
                                <!--:key="index"-->
                                <!--:label="item.name"-->
                                <!--:value="item.id">-->
                        <!--</el-option>-->
                    <!--</el-select>-->
                </div>
                <!--<el-button size="small" type="primary" @click="dialogFormVisible = t 。rue">医保对账</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'}"
                      v-loading="patientLoading"
                      highlight-current-row>

                <el-table-column align="center" prop="name" label="流水号">
                    <template slot-scope="scope">{{scope.row.record.trt_dcla_detl_sn}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="时间">
                    <template slot-scope="scope" v-if="scope.row.record.ctime > 0">{{getTimes(scope.row.record.ctime)}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="姓名">
                    <template slot-scope="scope">{{scope.row.name}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="人员编号">
                    <template slot-scope="scope">
                        <div>{{scope.row.record.psn_no}}</div>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="联系电话">
                    <template slot-scope="scope">{{scope.row.phone}}</template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="地址">
                    <template slot-scope="scope">
                        {{scope.row.home_address}}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="name" width="160" label="鉴定定点医药机构">
                    <template slot-scope="scope">
                        {{$store.getters.xt_user.org.org_name}}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="医生">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.record.doctor_id" placeholder="请选择"
                                   @change="changeDoctor(scope.row,scope.row.record.doctor_id)">
                            <el-option
                                    v-for="(item,index) in doctorList"
                                    :key="index"
                                    :label="item.user_name"
                                    :value="item.admin_user_id">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="name" label="科室">
                    <template slot-scope="scope">
                        <el-select style="margin-right:5px;width:140px;" v-model="scope.row.record.department_id"
                                   placeholder="请选择">
                            <el-option
                                    v-for="(item,index) in department"
                                    :key="index"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>


                <el-table-column align="center" prop="name" label="险种类型">
                    <template slot-scope="scope">
                        <div v-if="scope.row.record.insutype == '310'">职工基本医疗保险</div>
                        <div v-if="scope.row.record.insutype == '320'">公务员医疗补助</div>
                        <div v-if="scope.row.record.insutype == '330'">大额医疗费用补助</div>
                        <div v-if="scope.row.record.insutype == '340'">离休人员医疗保障</div>
                        <div v-if="scope.row.record.insutype == '390'">城乡居民基本医疗保险</div>
                        <div v-if="scope.row.record.insutype == '392'">城乡居民大病医疗保险</div>
                        <div v-if="scope.row.record.insutype == '510'">生育保险</div>
                    </template>
                </el-table-column>

                <el-table-column align="center" prop="name" label="疾病类型" width="100">
                    <template slot-scope="scope">
                        <el-select  filterable style="width:100%;" v-model="scope.row.record.sick_type" placeholder="请选择">
                            <el-option
                                    v-for="(item,index) in sick"
                                    :key="index"
                                    :label="item.class_name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>

                <el-table-column prop="date" label="操作" width="80" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="primary" size="small" icon="el-icon-tickets" v-if="scope.row.record.is_cancel == 0 ||scope.row.record.is_cancel == 2" >
                            <span @click="putOnRecord(1,scope.row)">备案</span>
                        </el-button>

                        <el-button type="danger" size="small" icon="el-icon-tickets" v-if="scope.row.record.is_cancel == 1" >
                            <span @click="putOnRecord(2,scope.row)">撤消</span>
                        </el-button>


                      <el-button type="primary" size="small" icon="el-icon-tickets" v-if="scope.row.record.is_cancel == 1" >
                        <span @click="putOnRecord(3,scope.row)">查看备案信息</span>
                      </el-button>

                      <el-button type="primary" size="small" icon="el-icon-tickets" >
                        <span @click="putOnRecord(4,scope.row)">删除</span>
                      </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-dialog title="备案" :visible.sync="dialogFormVisible">
                <el-form  label-width="100px">
                    <el-form-item label="险种类型" >
                        <el-select v-model="insutype_value" 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="医院鉴定日期" >
                    <el-date-picker
                        v-model="check_date"
                        prefix-icon="el-icon-date"
                        :editable="false"
                        style="width: 100%;"
                        type="date"
                        placeholder="选择开始时间"
                        align="right"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd">
                    </el-date-picker>

                  </el-form-item>
                  <el-form-item label="开始时间" >
                    <el-date-picker
                        v-model="start_date"
                        prefix-icon="el-icon-date"
                        :editable="false"
                        style="width: 100%;"
                        type="date"
                        placeholder="选择开始时间"
                        align="right"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd">
                    </el-date-picker>
                  </el-form-item>

                  <el-form-item label="结束时间" >
                    <el-date-picker
                        v-model="end_date"
                        prefix-icon="el-icon-date"
                        :editable="false"
                        style="width: 100%;"
                        type="date"
                        placeholder="选择结束时间"
                        align="right"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd">
                    </el-date-picker>
                  </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>

        <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>
    </div>
</template>


<script>
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import { getAllDoctorList, getInitData, getRecordList,deleteNCDSRecord } from '@/api/his/his'
  import { uParseTime } from '@/utils/tools'
  import { fetchAllAdminUsers } from '@/api/doctor'
  import axios from 'axios'

  export default {
    components: {
      BreadCrumb
    },
    data() {
      return {
        is_put_on_record:0,
        start_date:'',
        end_date:"",
        check_date:"",
        datas: [
          {
            id: 0,
            name: '全部'
          }, {
            id: 1,
            name: '已备案'
          }, {
            id: 2,
            name: '未备案'
          }
        ],
        currentObject: null,
        crumbs: [
          { path: false, name: '门诊收费' },
          { path: false, name: '医保费用对照' }
        ],
        tableData: [],
        doctorList: [],
        department: [],
        patientLoading: false,
        sick: [],
        limit: 10,
        insutype_value:"",
        psn_no:"",
        page: 1,
        dialogFormVisible: false,
        total: 0,
        adminUserOptions: [],
        start_time: '',
        end_time: '',
        insutype: '',
        check_type: '',
        keywords: '',
        options: [
          {
            value: 1,
            label: '总账'
          }, {
            value: 2,
            label: '明细'
          }
        ],
        insutype_arr: [
          {
            value: '310',
            label: '职工基本医疗保险'
          }, {
            value: '320',
            label: '公务员医疗补助'
          }, {
            value: '330',
            label: '大额医疗费用补助'
          }, {
            value: '340',
            label: '离休人员医疗保障'
          },
          {
            value: '390',
            label: '城乡居民基本医疗保险'
          }, {
            value: '392',
            label: '城乡居民大病医疗保险'
          }, {
            value: '510',
            label: '生育保险'
          }

        ],

        insutypes: [
          {
            value: '310',
            label: '职工基本医疗保险'
          }, {
            value: '320',
            label: '公务员医疗补助'
          }, {
            value: '330',
            label: '大额医疗费用补助'
          }, {
            value: '340',
            label: '离休人员医疗保障'
          },
          {
            value: '390',
            label: '城乡居民基本医疗保险'
          }, {
            value: '392',
            label: '城乡居民大病医疗保险'
          }, {
            value: '510',
            label: '生育保险'
          }

        ]

      }
    },
    methods: {
      changeStatus(id){
        this.keywords = "";
        this.getList()
      },

      getGdybPatientInfo(){
        let params ={
          id_card_no:this.currentObject.id_card_no,
          admin_user_id:  this.$store.getters.xt_user.user.id,
        };
        var that = this;
        axios.get('http://127.0.0.1:9532/api/patient/info', {
          params: params
        })
          .then(function(response) {
            if (response.data.state == 0) {
              that.$message.error(response.data.msg);
              return false
            } else {

              if(response.data.data.failed_code == -10){
                // that.$message.error(response.data.data.msg)
                that.$confirm(response.data.data.msg, '医保错误信息', {
                  confirmButtonText: '确 定',
                  type: 'warning'
                }).then(() => {

                }).catch(() => {
                })
              }else{
                that.dialogFormVisible = true;
                that.insutypes = [];
                that.psn_no =  response.data.data.info.output.baseinfo.psn_no;
                for (let i =0; i < response.data.data.info.output.insuinfo.length; i++){
                  let obj = {
                    value:response.data.data.info.output.insuinfo[i].insutype,
                    label:""
                  };
                  for(let b = 0; b < that.insutype_arr.length; b++){
                    if(that.insutype_arr[b].value == response.data.data.info.output.insuinfo[i].insutype){
                      obj.label = that.insutype_arr[b].label
                    }
                  }
                  if(response.data.data.info.output.insuinfo[i].psn_insu_stas == 1){
                    obj.label =  obj.label

                  }else{
                    obj.label =  obj.label +"(已暂停)"
                  }

                  that.insutypes.push(obj)

                }
              }
            }
          })
          .catch(function(error) {

          })
      },
      putOnRecord(type, row) {
        if (type == 1) {
          this.currentObject = row;

          this.getGdybPatientInfo()

        } else if(type == 2) {
          this.$confirm('是否撤销备案', '撤销备案', {
            confirmButtonText: '确 定',
            cancelButtonText: '取 消',
            type: 'warning'
          }).then(() => {
            let params = {
              'id': row.record.id,
              'admin_user_id': this.$store.getters.xt_user.user.id

            };
            var that = this;
            axios.get('http://127.0.0.1:9532/api/psn/ncds/unputonrecord', {
              params: params
            })
              .then(function(response) {
                if (response.data.state == 0) {
                  that.$message.error(response.data.msg);
                  return false
                } else {

                  if(response.data.data.failed_code == -10){
                    // that.$message.error(response.data.data.msg)
                    that.$confirm(response.data.data.msg, '医保错误信息', {
                      confirmButtonText: '确 定',
                      type: 'warning'
                    }).then(() => {

                    }).catch(() => {
                    })
                  }else{
                    row.record.is_cancel = 2;
                    that.$message({ message: '撤销成功', type: 'success' })
                  }
                }
              })
              .catch(function(error) {

              })

          }).catch(() => {

          })

        }else if(type == 3){
          let params = {
            'id': row.record.id,
            'admin_user_id': this.$store.getters.xt_user.user.id
          };
          var that = this;
          axios.get('http://127.0.0.1:9532/api/psn/info', {
            params: params
          })
              .then(function(response) {
                if (response.data.state == 0) {
                  that.$message.error(response.data.msg);
                  return false
                } else {
                  if(response.data.data.failed_code == -10){
                    // that.$message.error(response.data.data.msg)
                    that.$confirm(response.data.data.msg, '医保错误信息', {
                      confirmButtonText: '确 定',
                      type: 'warning'
                    }).then(() => {

                    }).catch(() => {
                    })
                  }else{
                    that.$confirm(response.data.data.result, '医保信息', {
                      confirmButtonText: '确 定',
                      type: 'warning'
                    }).then(() => {

                    }).catch(() => {

                    })
                  }
                }
              })
              .catch(function(error) {
              })
        }else if(type == 4){
          this.$confirm("是否删除", '删除', {
            confirmButtonText: '确 定',
            type: 'warning'
          }).then(() => {
            let params = {
              id: row.record.id,
            }
            deleteNCDSRecord(params).then(response => {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg);
                return false
              } else {
                this.getList()
              }
            })

          }).catch(() => {
          })


        }
      },
      changeDoctor(row, id) {
        for (let i = 0; i < this.doctorList.length; i++) {
          if (id == this.doctorList[i].admin_user_id) {
            if (this.doctorList[i].department_id == 0) {
              row.record.department_id = ''
            } else {
              row.record.department_id = this.doctorList[i].department_id
            }
          }
        }
      },
      getInitData() {
        getInitData().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.doctorList = response.data.data.doctors;
            this.department = response.data.data.department;
            this.sick = response.data.data.sick

          }
        })

      },
      getSickName(id) {

      },
      confirm() {
        if(this.currentObject.record.doctor_id == '' || this.currentObject.record.doctor_id  == 0){
          this.$message.error("请选择医生");
          return
        }

        if(this.currentObject.record.department_id == '' || this.currentObject.record.department_id  == 0){
          this.$message.error("请选择科室");
          return
        }

        if(this.currentObject.record.sick_type == '' || this.currentObject.record.sick_type  == 0){
          this.$message.error("请选择疾病类型");
          return
        }

        if(this.currentObject.record.insutype_value == '' || this.currentObject.record.insutype_value  == 0){
          this.$message.error("请选择险种");
          return
        }

        if(this.start_date == '' || this.start_date  == 0){
          this.$message.error("请选择开始时间");
          return
        }

        if(this.end_date == '' || this.end_date  == 0){
          this.$message.error("请选择结束时间");
          return
        }
        if(this.check_date == '' || this.check_date  == 0){
          this.$message.error("请选择鉴定时间");
          return
        }

        let params = {
          record_type: 0,
          patient_id: this.currentObject.id,
          insutype: this.insutype_value,
          start_time: this.start_date,
          end_time: this.end_date,
          check_time: this.check_date,
          psn_no: this.psn_no,
          sick_id: this.currentObject.record.sick_type,
          doctor_id: this.currentObject.record.doctor_id,
          department_id:this.currentObject.record.department_id,
          admin_user_id: this.$store.getters.xt_user.user.id
        };

        var that = this;
        axios.get('http://127.0.0.1:9532/api/psn/ncds/putonrecord', {
          params: params
        })
          .then(function(response) {
            if (response.data.state == 0) {
              that.$message.error(response.data.msg);
              return false
            } else {
              if(response.data.data.failed_code == -10){
                // that.$message.error(response.data.data.msg)
                that.$confirm(response.data.data.msg, '医保错误信息', {
                  confirmButtonText: '确 定',
                  type: 'warning'
                }).then(() => {

                }).catch(() => {
                })
              }else{
                that.$message.success("备案成功");
                that.dialogFormVisible = false;
                for (let i = 0; i < that.tableData.length; i++) {
                  if (that.tableData[i].id == that.currentObject.id) {
                    that.tableData[i].record.is_cancel =response.data.data.result.is_cancel;
                    that.tableData[i].record.id = response.data.data.result.id;
                    that.tableData[i].record.trt_dcla_detl_sn = response.data.data.result.trt_dcla_detl_sn;
                    that.tableData[i].record.psn_no = response.data.data.result.psn_no;
                    that.tableData[i].record.doctor_id = response.data.data.result.doctor_id;
                    that.tableData[i].record.department_id = response.data.data.result.department_id;
                    that.tableData[i].record.ctime = response.data.data.result.ctime;
                    that.tableData[i].record.insutype = response.data.data.result.insutype;
                    that.tableData[i].record.sick_type = response.data.data.result.sick_type

                  }
                }
              }

            }
          })
          .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,
          'is_put_on_record':this.is_put_on_record
        };
        this.tableData = [];
        this.patientLoading = true;
        getRecordList(params).then(response => {
          if (response.data.state == 0) {
            this.patientLoading = false;

            this.$message.error(response.data.msg);
            return false
          } else {
            this.patientLoading = false;
            this.tableData = response.data.data.list;
            this.total = response.data.data.total;

            for (let i = 0; i < this.tableData.length; i++) {

              if (this.tableData[i].record.doctor_id == 0) {
                this.tableData[i].record.doctor_id = ''

              }
              if (this.tableData[i].record.department_id == 0) {
                this.tableData[i].record.department_id = ''
              }

              if (this.tableData[i].record.sick_type == 0) {
                this.tableData[i].record.sick_type = ''
              }

            }

          }
        })

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

      }, searchAction() {
        this.getList()
      }
    },
    created() {
      this.getList();
      this.getInitData();
      this.fetchAllAdminUsers()

    }
  }
</script>
