<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" :disabled="param.detail" label-width="100px">
      <el-row>
        <el-col :span="14">
          <el-form-item label="所属科室" prop="deptName">
            <el-input :value="$store.state.user.deptNameStr" placeholder="请输入标题" disabled />
          </el-form-item>
          <el-form-item label="提醒标题" prop="templateName" v-if="form.type == 0">
            <el-input v-model="form.templateName" placeholder="请输入提醒标题, 例如:体检异常复检" clearable />
          </el-form-item>
          <el-form-item label="回访模板" prop="templateName" v-if="form.type == 1">
            <el-select v-model="form.templateName" placeholder="请选择模板" @change="change">
              <el-option v-for="item in templateList" :key="item.title" :label="item.title" :value="item.title" clearable></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发送患者" :prop="form.id ? '' : 'patientList'">
            <div v-if="form.id">
              <el-tag size="medium" class="patient">{{ param.patientName }}</el-tag>
            </div>
            <div class="patients" v-else>
              <el-tag size="medium" class="patient" v-for="item in form.patientList" :key="item.name" closable @close="del(item)">{{ item.name }}</el-tag>
              <el-button type="primary" icon="el-icon-plus" @click="selectPatient()" class="btn">添加</el-button>
            </div>
          </el-form-item>
          <el-form-item label="提醒内容" prop="op" v-if="form.type == 0">
            <el-input v-model="form.op" type="textarea" placeholder="例如:根据您于本院做的体检结果异常,请到岑溪人民医院做复诊" rows="5" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <div class="div-mc">
            <div class="guise-mobile">
              <div class="guise-mobile-show" ref="scrollbar">
                <div class="mttv">{{ form.templateName }}</div>
                <div class="message-bubble-received" v-if="form.type == 0">{{ form.op }}</div>
                <div class="items" v-for="(item, index) in op" :key="index">
                  <div class="vtitle" style="font-weight: normal; font-size: 14px">
                    <span class="ifnull" v-if="item.ifnull == '必填'">*</span>
                    <span class="index">{{ index + 1 }},</span>
                    <span class="tm">{{ item.name }} ({{ item.input }})</span>
                  </div>
                  <div class="mts">
                    <input v-if="item.input == '填空'" placeholder="请输入" :disabled="true" />
                    <textarea v-if="item.input == '多行文本'" :disabled="true" placeholder="请输入"></textarea>
                    <input type="number" v-if="item.input == '数字'" placeholder="请输入" :disabled="true" />
                    <div class="ops">
                      <div v-for="(op, i) in item.selects" :key="op.name">
                        <div class="op">{{ op.name }}</div>
                      </div>
                    </div>
                  </div>
                </div>
                <el-button type="primary" class="tjiao" v-if="form.type == 1 && form.templateName != null">提交</el-button>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div class="mfooter" v-if="!param.detail">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import selectPatient from './selectPatient';
export default {
  data() {
    return {
      form: {
        op: '根据您于本院做的体检结果异常,请到岑溪人民医院做复诊',
        patientList: []
      },
      templateList: [],
      op: [],
      rules: {
        templateName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        patientList: [{ required: true, message: '不能为空', trigger: 'blur' }],
        op: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    };
  },
  mounted() {
    if (this.param.type == 1) {
      this.getTemplate();
    }
    if (this.param.id) {
      this.ajax({ url: '/work/record/detail/' + this.param.id }).then((response) => {
        this.form = response.data;
        this.op = JSON.parse(response.data.op);
      });
    } else {
      this.form.type = this.param.type;
      this.$forceUpdate();
    }
  },
  methods: {
    getTemplate() {
      this.ajax({ url: '/work/record/template/list' }).then((response) => {
        this.templateList = response.data;
      });
    },
    change(e) {
      let item = this.templateList.filter((item) => item.title == e)[0];
      this.form.op = item.op;
      this.op = JSON.parse(item.op);
    },
    selectPatient() {
      this.iframe({ obj: selectPatient, param: {}, title: '选择推送患者', width: '60%', height: '75%' });
    },
    del(item) {
      this.form.patientList.splice(this.form.patientList.indexOf(item), 1);
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.ajax({ method: 'post', url: '/work/record/edit', data: this.form }).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.ajax({ method: 'post', url: '/work/record/add', data: this.form }).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.patients {
  .patient {
    margin-right: 10px;
  }
}
</style>
