<template>
  <div class="app-container template-page">
    <el-form :model="searchForm" ref="searchForm" :inline="true">
      <el-form-item :label="$t('page.system.template.sn') + '：'" prop="sn">
        <el-input v-model="searchForm.sn" :placeholder="$t('form.placeholderInput')" clearable style="width: 150px"
          @keyup.enter.native="search" />
      </el-form-item>
      <el-form-item :label="$t('page.system.template.name') + '：'" prop="name">
        <el-input v-model="searchForm.name" :placeholder="$t('form.placeholderInput')" clearable style="width: 150px"
          @keyup.enter.native="search" />
      </el-form-item>

      <el-form-item :label="$t('page.system.template.department') + '：'" prop="deptId">
        <select-dept style="width:200px" v-model="searchForm.deptId" @change="search" />
      </el-form-item>
      <el-form-item :label="$t('page.system.template.status') + '：'" prop="status">
        <el-select v-model="searchForm.status" :placeholder="$t('form.placeholderSelect')" clearable
          style="width: 100px" @change="search">
          <el-option key="0" :label="$t('page.system.template.statusEnable')" :value="10" />
          <el-option key="1" :label="$t('page.system.template.statusDisable')" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">{{ $t('form.search') }}</el-button>
        <el-button icon="el-icon-refresh" @click="reset">{{ $t('form.reset') }}</el-button>

        <el-button type="primary" plain icon="el-icon-plus" @click="edit(null)">新增-后期不需要</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('page.system.template.sn')" align="center" prop="sn" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('page.system.template.sn')" align="center" prop="showSn" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('page.system.template.name')" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('page.system.template.department')" align="center" prop="deptName" />
      <el-table-column :label="$t('page.system.template.status')" prop="status" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 10">{{ $t('page.system.template.statusEnable') }}</span>
          <span v-if="scope.row.status === 1">{{ $t('page.system.template.statusDisable') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要预填" prop="needPre" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.needPre === 10">是</span>
          <span v-if="scope.row.needPre === 1">否</span>
        </template>
      </el-table-column>
       <el-table-column label="产物" prop="product" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.product == 1">试剂</span>
          <span v-if="scope.row.product == 3">给药制剂</span>
          <span v-if="scope.row.product == 5">麻精药</span>
          <span v-if="scope.row.product == 7">无</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="模板类型" prop="type" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 10">高风险表单</span>
          <span v-if="scope.row.type === 1">普通表单</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('form.operate')" fixed="right" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)">{{ $t('page.system.template.bj') }}</el-button>
          <el-button type="text" @click="bdpz(scope.row)">{{ $t('page.system.template.detail') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
      @pagination="getList" />

    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="infoDialog.title" :visible.sync="infoDialog.visible" width="800px"
      append-to-body>
      <el-form ref="infoDialogForm" :model="infoDialog.formData" :rules="infoDialog.rules" label-width="180px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="编号：" prop="sn">
              <el-input v-model="infoDialog.formData.sn" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="名称：" prop="name">
              <el-input v-model="infoDialog.formData.name" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部门/学科：" prop="deptId">
              <select-dept v-model="infoDialog.formData.deptId" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态：" prop="status">
              <el-select v-model="infoDialog.formData.status" :placeholder="$t('form.placeholderSelect')" style="width:100%">
                <el-option label="启用" :value="10" />
                <el-option label="禁用" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否需要预填：" prop="needPre">
              <el-select v-model="infoDialog.formData.needPre" :placeholder="$t('form.placeholderSelect')" style="width:100%">
                <el-option label="是" :value="10" />
                <el-option label="否" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="产物：" prop="product">
              <el-select v-model="infoDialog.formData.product" :placeholder="$t('form.placeholderSelect')" style="width:100%">
                <el-option label="试剂" :value="1" />
                <el-option label="给药制剂" :value="3" />
                <el-option label="麻精药" :value="5" />
                <el-option label="无" :value="7" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="填报是否显示观察按钮：" prop="showGc">
              <el-select v-model="infoDialog.formData.showGc" :placeholder="$t('form.placeholderSelect')" style="width:100%">
                <el-option label="否" :value="1" />
                <el-option label="是" :value="10" />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="类型：" prop="type">
              <el-select v-model="infoDialog.formData.type" :placeholder="$t('form.placeholderSelect')" style="width:100%">
                <el-option label="普通表单" :value="1" />
                <el-option label="高风险表单" :value="10" />
              </el-select>
            </el-form-item>
          </el-col> -->

          <el-col :span="24">
            <el-form-item label="步骤库：" prop="stepGroupIds">
              <el-select multiple v-model="stepGroupIdArr" :placeholder="$t('form.placeholderSelect')" style="width:100%" @change="changeStepGroup">
                <el-option v-for="item in stepGroupList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">确 定</el-button>
        <el-button @click="infoDialog.visible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="$t('page.system.template.bdpz')" :visible.sync="tableDialog.visible"
      width="90%" append-to-body>
      <TemplateTable ref = "templateTable" :sn="tableDialog.sn"  v-if="tableDialog.visible" fillType="preFill" />
    </el-dialog>
  </div>
</template>

<script>
import { template_list, template_info, template_save, template_delete } from "@/api/business/template/template";
import { stepGroup_list } from "@/api/business/step/step";
import SelectDept from '@/views/business/comps/select/SelectDept.vue';
import TemplateTable from '@/views/business/comps/template/TemplateTable';
const EmptyDialogData = {
  id: '',
  name: '',
  sn: '',
  deptId: '',
  status:10,
  needPre: 10,
  type: 10,

  stepGroupIds: '',
}
export default {
  name: "Template",
  props: {
  },
  components: {
    SelectDept,
    TemplateTable
  },
  computed: {
  },
  filters: {
  },
  data() {
    return {
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        sn: '',
        name: '',
        deptId: null,
        status: '',
      },

      loading: true,
      total: 0,
      list: [],

      stepGroupList:[],
      
      stepGroupIdArr:[],
      infoDialog: {
        title: '',
        visible: false,
        formData: {
        },

        rules: {
          showGc: [
            { required: true, message:'请选择', trigger: "blur" }
          ],
          sn: [
            { required: true, message:'请输入', trigger: "blur" }
          ],
          name: [
            { required: true, message:'请输入', trigger: "blur" }
          ],
          deptId: [
            { required: true, message:'请选择', trigger: "change" }
          ],
          status: [
            { required: true, message:'请选择', trigger: "change" }
          ],
          product: [
            { required: true, message:'请选择', trigger: "change" }
          ],
          needPre: [
            { required: true, message:'请选择', trigger: "change" }
          ],
          type: [
            { required: true, message:'请选择', trigger: "change" }
          ],
        },
      },
      tableDialog: {
        title: '',
        visible: false,
        sn: ''
      },
    };
  },
  created() {
    this.getList();
    this.getGroupList()
  },
  methods: {
    getGroupList(){
      stepGroup_list({ pageNum: 1, pageSize:9999}).then(response => {
        this.stepGroupList = response.rows;
      });
    },
    bdpz(row) {
      this.tableDialog.title = '表单配置'
      this.tableDialog.sn=row.sn
      this.tableDialog.visible=true
    },
    getList() {
      this.loading = true;
      template_list(this.searchForm).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    search() {
      this.searchForm.pageNum = 1;
      this.getList();
    },
    reset() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        sn: '',
        name: '',
        deptId: null,
        status: '',
      }
      this.search()
    },
    edit(row) {
      this.$refs['infoDialogForm'] && this.$refs['infoDialogForm'].resetFields()
      this.infoDialog.title = '新增'
      this.infoDialog.formData = _.merge({}, EmptyDialogData)
      if (row && row.id) {
        this.infoDialog.title = '编辑'
        this.$modal.loading()
        template_info({ id: row.id }).then(({ data }) => {
          this.infoDialog.formData = data
          this.stepGroupIdArr = this.infoDialog.formData.stepGroupIds ? _.map(this.infoDialog.formData.stepGroupIds.split(','),(o)=>{return parseInt(o)}): []
        }).finally(() => {
          this.$modal.closeLoading()
        })
      }
      this.infoDialog.visible = true

      // this.infoDialog.title = this.$t('page.system.template.edit')
      // this.$modal.loading()
      // template_info({id:row.id}).then(({data}) => {
      //   this.infoDialog.formData = data
      // }).finally(() => {
      //   this.$modal.closeLoading()
      // })
    },
    changeStepGroup(val){
      this.infoDialog.formData.stepGroupIds = val.join(',')
    },
    save() {
      this.$refs['infoDialogForm'].validate(valid => {
        if (valid) {
          this.$modal.loading()
          template_save(this.infoDialog.formData).then(() => {
            this.infoDialog.visible = false
            this.getList()
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    },
    del(row) {
      this.$confirm('确定要删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$modal.loading()
          template_delete({ id: row.id }).then(() => {
            this.getList()
          })
            .finally(() => {
              this.$modal.closeLoading()
            })
        })
        .catch(() => { })
    },
  }
};
</script>
<style lang="scss"></style>