<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="所属科室" prop="deptName">
        <el-input v-model="form.deptName" placeholder="请输入标题" disabled />
      </el-form-item>
      <el-form-item label="回访模板" prop="templateName">
        <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="templateList">
        <div class="patients">
          <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-search" @click="selectPatient()" class="btn">添加</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="mfooter">
      <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: {
        deptName: this.$store.state.user.deptNameStr,
        patientList: []
      },
      templateList: [],
      rules: {
        templateName: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    };
  },
  mounted() {
    this.getTemplate();
    if (this.param.id) {
      this.ajax({ url: '/work/record/detail/' + this.param.id }).then((response) => {
        this.form = response.data;
      });
    }
  },
  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;
    },
    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;
  }
  .btn {
    margin-left: 15px;
  }
}
</style>
