<template>
  <el-dialog
    title="自付比例"
    width="660px"
    :visible.sync="visible"
    :before-close="_close"
  >
    <el-table :data="tableData" border style="width: 100%" :row-style="{ color: '#303133' }"
              :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)', color: '#606266'}">
      <el-table-column prop="name" label="医保类型" align="center"></el-table-column>
      <el-table-column label="自付比例(%)" align="center">
        <template slot-scope="scope">
          <el-input v-model="scope.row.percent" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
    </el-table>

    <div slot="footer" class="dialog-footer">
      <el-button @click="hide">取 消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="submitAction()">保 存</el-button>
    </div>
  </el-dialog>
</template>


<script>
  import { createMedicineInsurancePercent, getMedicineInsurancePercent } from '@/api/data'

  export default {
    data() {
      return {
        visible: false,
        module:0,
        tableData: [
          {
            name: '深圳医保1档',
            medicine_insurance_type: 1,
            percent: '',
            type_id: 0
          },
          {
            name: '深圳医保2档',
            medicine_insurance_type: 2,
            type_id: 0,
            percent: ''
          },
          {
            name: '深圳医保3档',
            medicine_insurance_type: 3,
            type_id: 0,
            percent: ''
          },
          {
            name: '外地医保',
            medicine_insurance_type: 4,
            type_id: 0,
            percent: ''
          },
          {
            name: '自费',
            medicine_insurance_type: 5,
            percent: '',
            type_id: 0
          }
        ]
      }
    },
    methods: {

      submitAction: function() {
        let params = {
          'mip': this.tableData
        }
        createMedicineInsurancePercent(params,this.module).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.hide()
          }

        })

      },
      _close: function(done) {
        done()
      },
      show(id,module) {
        this.module = module
        this.tableData = []
        getMedicineInsurancePercent(id,module).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg)
              return false
            } else {
              var temptableData = [
                {
                  name: '深圳医保1档',
                  medicine_insurance_type: 1,
                  percent: '',
                  type_id: id
                },
                {
                  name: '深圳医保2档',
                  medicine_insurance_type: 2,
                  type_id: id,
                  percent: ''
                },
                {
                  name: '深圳医保3档',
                  medicine_insurance_type: 3,
                  type_id: id,
                  percent: ''
                },
                {
                  name: '外地医保',
                  medicine_insurance_type: 4,
                  type_id: id,
                  percent: ''
                },
                {
                  name: '自费',
                  medicine_insurance_type: 5,
                  percent: '',
                  type_id: id
                }
              ]
              this.tableData = temptableData

              this.visible = true
              for (let i = 0; i < this.tableData.length; i++) {
                this.tableData[i].type_id = id
              }
              if (response.data.data.mip != null && response.data.data.mip.length > 0) {

                for (let i = 0; i < response.data.data.mip.length; i++) {
                  for (let a = 0; a < this.tableData.length; a++) {
                    if (response.data.data.mip[i].medicine_insurance_type == this.tableData[a].medicine_insurance_type) {
                      this.tableData[a].percent = response.data.data.mip[i].percent.toString()
                    }
                  }
                }

              }else{
               var temptableData = [
                  {
                    name: '深圳医保1档',
                    medicine_insurance_type: 1,
                    percent: '',
                    type_id: id
                  },
                  {
                    name: '深圳医保2档',
                    medicine_insurance_type: 2,
                    type_id: id,
                    percent: ''
                  },
                  {
                    name: '深圳医保3档',
                    medicine_insurance_type: 3,
                    type_id: id,
                    percent: ''
                  },
                  {
                    name: '外地医保',
                    medicine_insurance_type: 4,
                    type_id: id,
                    percent: ''
                  },
                  {
                    name: '自费',
                    medicine_insurance_type: 5,
                    percent: '',
                    type_id: id
                  }
                ]
                this.tableData = temptableData



              }

            }

          }
        )

      },
      hide() {
        // this.clear()
        this.visible = false
      }
    }
  }
</script>
