<template>
  <div class="app-container reviewers-data data-list">
    <div class="filter-container">
      <el-input :maxLength="$inputMax"
        v-model="listQuery.SearchName"
        size="small"
        placeholder="Name or Name CN"
        style="width:160px;margin-right:5px;"
      />
      <el-select
        v-if="hospitalList"
        v-model="listQuery.HospitalId"
        placeholder="Hospital"
        clearable
        style="width:150px"
        size="small"
      >
        <el-option
          v-for="(item) of hospitalList"
          :key="item.Id"
          :label="item.HospitalName"
          :value="item.Id"
        />
      </el-select>
      <el-button size="small" type="text" @click="handleReset">Reset</el-button>
      <el-button type="primary" size="small" @click="handleSearch">Search</el-button>
    </div>
    <div class="list-container">
      <el-table
        v-loading="listLoading"
        height="100%"
        :data="list"
        stripe
        class="table"
        size="small"
        @sort-change="sortByColumn"
      >
        <el-table-column type="index" width="40" />
        <el-table-column
          prop="LastName"
          label="Name"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        >
          <template slot-scope="scope">{{ scope.row.LastName + ' / ' + scope.row.FirstName }}</template>
        </el-table-column>
        <el-table-column
          prop="DoctorNameInBank"
          label="Name CN"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.DoctorNameInBank?scope.row.DoctorNameInBank:scope.row.ChineseName }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="Code"
          label="ID"
          show-overflow-tooltip
          min-width="90"
          sortable="custom"
        />
        <el-table-column
          prop="Hospital"
          label="Hospital"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        />
        <el-table-column prop="RankName" label="Rank" show-overflow-tooltip min-width="100" />
        <el-table-column
          prop="Additional"
          label="Personal Adjustment ($) "
          show-overflow-tooltip
          min-width="200"
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.Additional | rounding }}</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="IDCard"
          label="Resident ID "
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        />

        <el-table-column
          prop="BankName"
          label="Bank"
          show-overflow-tooltip
          min-width="110"
          sortable="custom"
        />
        <el-table-column
          prop="BankCardNumber"
          label="Account Number"
          show-overflow-tooltip
          min-width="160"
          sortable="custom"
        />
        <el-table-column
          prop="CreateTime"
          label="Date Created"
          min-width="140"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          fixed="right"
          label="Action"
          min-width="90"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              @click="handleEdit(scope.row)"
            >Edit</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <pagination :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
    <el-dialog
      :key="form.DoctorId"
      title="Edit"
      :visible.sync="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
      size="small"
    >
      <el-form ref="reviewerDataForm" label-width="170px" :rules="rules" :model="form" size="small">
        <el-form-item label="Name: ">
          <el-input :maxLength="$inputMax" v-model="form.DoctorName" readonly />
        </el-form-item>

        <el-form-item label="Name CN: " prop="DoctorNameInBank">
          <el-input :maxLength="$inputMax" v-model="form.DoctorNameInBank" />
        </el-form-item>
        <el-form-item label="ID: " prop="Code">
          <el-input :maxLength="$inputMax" v-model="form.Code" readonly />
        </el-form-item>
        <el-form-item label="Rank: " prop="RankId">
          <el-select v-model="form.RankId" style="width:100%;">
            <el-option
              v-for="(item) in rankList"
              :key="item.Id"
              :label="item.RankName"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Personal Adjustment ($): " prop="Additional">
          <el-input :maxLength="$inputMax" v-model="form.Additional" type="number" />
        </el-form-item>
        <el-form-item label="Resident ID: " prop="IDCard">
          <el-input :maxLength="$inputMax" v-model="form.IDCard" />
        </el-form-item>
        <el-form-item label="Bank: " prop="BankName">
          <el-input :maxLength="$inputMax" v-model="form.BankName" />
        </el-form-item>
        <el-form-item label="Account Number: " prop="BankCardNumber">
          <el-input :maxLength="$inputMax" v-model="form.BankCardNumber" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" :disabled="btnDisabled" size="small" @click="handleSave">Ok</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { getReviewerPayInfoList, addOrUpdateReviewerPayInfo } from '@/api/financials'
import { formatUTCTime } from '@/utils/formatter'
import store from '@/store'
import { mapGetters } from 'vuex'
const getListQueryDefault = () => {
  return {
    SearchName: '',
    // HospitalId: '',
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: ''
  }
}
export default {
  name: 'ReviewersData',
  filters: {
    rounding(value) {
      return value ? Number(value).toFixed(2) : value
    }
  },
  components: { Pagination },
  data() {
    return {
      listQuery: getListQueryDefault(),
      list: [],
      total: 0,
      listLoading: false,
      dialogVisible: false,
      btnDisabled: false,
      rankOption: [],
      form: {},
      rules: {
        DoctorNameInBank: [
          { required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }
        ],
        BankName: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        BankCardNumber: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        IDCard: [
          { required: true, message: '请注明', trigger: 'blur' },
          {
            pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: 'Incorrect ID card format'
          }
        ],
        RankId: [{ required: true, message: 'Please select', trigger: ['blur', 'change'] }]
      }
    }
  },
  computed: {
    ...mapGetters(['hospitalList', 'rankList'])
  },
  mounted() {
    store.dispatch('global/getHospital')
    store.dispatch('financials/getRank')
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getReviewerPayInfoList(this.listQuery).then(res => {
        this.listLoading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.listLoading = false })
    },
    handleEdit(row) {
      this.form = (({ DoctorId, DoctorNameInBank, IDCard, BankCardNumber, BankName, RankId, Additional, Code }) => ({ DoctorId, DoctorNameInBank, IDCard, BankCardNumber, BankName, RankId, Additional, Code }))(row)
      this.form.DoctorName = row.LastName + ' / ' + row.FirstName
      this.form.DoctorNameInBank = row.DoctorNameInBank ? row.DoctorNameInBank : row.ChineseName
      this.dialogVisible = true
    },
    handleSave() {
      this.$refs.reviewerDataForm.validate(valid => {
        if (valid) {
          this.btnDisabled = true
          const param = (({ DoctorId, DoctorNameInBank, IDCard, BankCardNumber, BankName, RankId, Additional }) => ({ DoctorId, DoctorNameInBank, IDCard, BankCardNumber, BankName, RankId, Additional }))(this.form)
          addOrUpdateReviewerPayInfo(param).then(res => {
            this.btnDisabled = false
            this.dialogVisible = false
            this.getList()
            this.$message.success('Updated successfully')
          }).catch(() => { this.btnDisabled = false })
        }
      })
    },
    handleSearch() {
      this.listQuery.PageIndex = 1
      this.getList()
    },
    handleReset() {
      Object.assign(this.listQuery, getListQueryDefault())
      this.getList()
    },
    onSelected(e) {
      const obj = this.rankOption.find(item => {
        return item.Id === e
      })
      this.form.Rank = obj.RankName
    },
    sortByColumn(column) {
      if (column.order === 'ascending') {
        this.listQuery.Asc = true
      } else {
        this.listQuery.Asc = false
      }
      this.listQuery.SortField = column.prop
      this.listQuery.PageIndex = 1
      this.getList()
    },
    timeFormatter(row) {
      if (row.CreateTime) {
        return formatUTCTime(row.CreateTime)
      }
    }
  }

}
</script>
<style lang="scss" scoped>
.reviewers-data{
  padding: 0;
  .list-container{
    height: calc(100% - 70px);
  }
}
</style>
