<template>
    <el-dialog
      title="长期处方模版"
      width="1200px"
      :visible.sync="visible"
      :before-close="_close"
    >
        <div>
            <el-form :model="form" class="adviceForm" :rules="rules" ref="form" label-width="90px">

                <el-form-item label="模板名称:"  prop="name" style="width: 100%;margin-bottom:0;">
                    <el-input v-model="form.name"  placeholder=""></el-input>
                </el-form-item>

                <el-form-item label="透析模式:" style="width: 100%;margin-top: 20px" prop="mode">
                    <el-select v-model="form.mode" style="width:100%;" placeholder="请选择">
                        <el-option
                                v-for="item in modeOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>


            <div v-for="(item,index) in allPrescription" :key="index">
                    <div v-for="(subItem,index2) in item.prescriptions" :key="index2">
                        <el-table v-if="subItem.advices.length > 0" :data="subItem.advices" border
                                  :row-style="{ color: '#303133' }"
                                  :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}"
                                  highlight-current-row>
                            <el-table-column align="center" type="index" width="60" label="序号"></el-table-column>
                            <el-table-column align="center" prop="drug_name" width="160" label="名称">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.drug_name }}</span>
                                </template>
                            </el-table-column>

                            <el-table-column align="center" prop="single_dose" width="90" label="单次用量">
                                <template slot-scope="scope">
                                    <div>{{scope.row.single_dose}}{{scope.row.single_dose_unit}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="delivery_way" width="100" label="用法">
                                <template slot-scope="scope">
                                    {{scope.row.delivery_way}}
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="execution_frequency" width="100" label="频率">
                                <template slot-scope="scope">
                                    {{scope.row.execution_frequency}}
                                </template>
                            </el-table-column>

                            <el-table-column align="center" prop="day" width="60" label="天数">
                                <template slot-scope="scope">
                                    {{scope.row.day}}
                                </template>
                            </el-table-column>

                            <el-table-column align="center" prop="prescribing_number" width="100" label="总量">
                                <template slot-scope="scope">
                                    <div>{{scope.row.prescribing_number}} {{scope.row.prescribing_number_unit}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="retail_price" width="80" label="单价">
                                <template slot-scope="scope">
                                    <div>{{scope.row.retail_price}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="remark" label="备注">
                                <template slot-scope="scope">
                                    <div>{{scope.row.remark}}</div>
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-table v-if="subItem.project.length > 0" :data="subItem.project" border style="marin-top:10px;"
                                  :row-style="{ color: '#303133' }"
                                  :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}"
                                  highlight-current-row>
                            <el-table-column align="center" type="index" width="60" label="序号"></el-table-column>
                            <el-table-column align="center" prop="project_name" width="160" label="名称">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.project_name}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="statistical_classification" width="100" label="组">
                                <template slot-scope="scope">{{ scope.row.type == 2
                                    ?getGroup(scope.row.statistical_classification):'耗材'}}
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="single_dose" width="90" label="单次用量">
                                <template slot-scope="scope">
                                    <!--<el-input v-model="scope.row.single_dose" placeholder=""></el-input>-->
                                    <span>{{ scope.row.single_dose }}</span>

                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="delivery_way" width="100" label="用法">
                                <template slot-scope="scope">
                                    <!--<el-input v-model="scope.row.delivery_way" placeholder=""></el-input>-->
                                    <span>{{ scope.row.delivery_way }}</span>

                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="execution_frequency" width="100" label="频率">
                                <template slot-scope="scope">
                                    <!--<el-input v-model="scope.row.execution_frequency" placeholder=""></el-input>-->
                                    <span>{{ scope.row.execution_frequency }}</span>

                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="number_days" width="70" label="天数">
                                <template slot-scope="scope">
                                    <!--<el-input v-model="scope.row.number_days" placeholder=""></el-input>-->
                                    <span>{{ scope.row.number_days }}</span>

                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="total" width="70" label="总量">
                                <template slot-scope="scope">
                                    <div>{{ scope.row.total }}{{scope.row.unit}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="name" width="60" label="单价">
                                <template slot-scope="scope">
                                    <!--<el-input v-model="scope.row.price" placeholder="" readonly></el-input>-->
                                    <span>{{ scope.row.price }}</span>

                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="name" label="备注">
                                <template slot-scope="scope">
                                    <!--<el-input v-model="scope.row.remark" :title="scope.row.remark"></el-input>-->
                                    <span>{{ scope.row.remark }}</span>

                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
            </div>


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

<script>
  import { getDictionaryDataConfig } from '@/utils/data'

  export default {
    data() {
      return {
        rules: {
          name: [{required: true, message: "模版名字不能为空", trigger: 'blur'}],
          mode: [{required: true, message: "请选择透析模式", trigger: 'blur'}],
        },
        visible: false,
        modeOptions: {},
        allPrescription: [],

        form: {
          name: '',
          mode: ''
        },
        radio: '1',
        options: [],
        value: ''
      }
    },
    methods: {
      getGroup(id) {
        var name = ''
        var statistics_category = getDictionaryDataConfig('system', 'statistics_category')
        for (let i = 0; i < statistics_category.length; i++) {
          if (id == statistics_category[i].id) {
            name = statistics_category[i].name
          }
        }
        return name
      },
      _close: function(done) {
        // this.clear()
        done()
      },
      clear: function() {
        this.form.id = 0
        this.form.name = ''
        this.form.intro = ''
      }, submitAction() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let checkGroup = []
            let allPrescriptions = []
            let targetPrescriptions = []

            let prescriptions = []
            for (let i = 0; i < this.allPrescription.length; i++) {
              for (let a = 0; a < this.allPrescription[i].prescriptions.length; a++) {
                allPrescriptions.push(this.allPrescription[i].prescriptions[a])
              }
            }

            for (let i = 0; i < allPrescriptions.length; i++) {
                  targetPrescriptions.push(allPrescriptions[i])
            }
            for(let i = 0; i < targetPrescriptions.length; i++){
              if(targetPrescriptions[i].advices.length > 0 && targetPrescriptions[i].project.length == 0){
                targetPrescriptions[i]["type"] = 1
              } else if(targetPrescriptions[i].advices.length == 0 && targetPrescriptions[i].project.length > 0){
                targetPrescriptions[i]["type"] = 2
              }
              for (let b = 0; b < targetPrescriptions[i].advices.length; b++) {
                targetPrescriptions[i].advices[b].single_dose = targetPrescriptions[i].advices[b].single_dose.toString()
                targetPrescriptions[i].advices[b].prescribing_number =targetPrescriptions[i].advices[b].prescribing_number.toString()
                targetPrescriptions[i].advices[b].retail_price = targetPrescriptions[i].advices[b].retail_price.toString()
                targetPrescriptions[i].advices[b].day = parseInt(targetPrescriptions[i].advices[b].day)
              }
              for (let b = 0; b < targetPrescriptions[i].project.length; b++) {
                targetPrescriptions[i].project[b].price =targetPrescriptions[i].project[b].price.toString()
                targetPrescriptions[i].project[b].total =targetPrescriptions[i].project[b].total.toString()
                targetPrescriptions[i].project[b].delivery_way =targetPrescriptions[i].project[b].delivery_way.toString()
              }
            }
            let params = {
              name: this.form.name,
              mode: this.form.mode,
              prescriptions: targetPrescriptions,
            }
            this.$emit('save_template', params)
          }
        })
      },
      show(prescriptions) {
        this.allPrescription = []
        this.modeOptions = this.$store.getters.treatment_mode
        let obj = {
          prescriptions: prescriptions
        }
        this.allPrescription.push(obj)
        this.visible = true
      },
      hide() {
        // this.clear()
        this.visible = false
      }
    }
  }
</script>


