<template>
  <div class="container">
    <!-- 查询配置 -->
    <div style="padding: 0px 20px">
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="教育培训计划" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content"></div>
          <slot>
            <div>
              <Input style="width: 200px; margin-right: 10px" v-model="searchFields.KeyWord" placeholder="关键字搜索" />

              <Button type="primary" @click="search" style="margin-left: 10px">查询</Button>
              <Button type="primary" @click="edit()" style="margin-left: 10px">新增</Button>
            </div>
          </slot>
        </LxHeader>
      </div>
      <div ref="lxTable" class="table-tree">
        <el-table :data="tableData" class="v-table" :height="tableHeight" style="width: 100%">
          <el-table-column type="index" fixed width="50" align="center"> </el-table-column>
          <el-table-column :prop="column.field" :fixed="column.fixed" :align="column.align"
            v-for="(column,index) in columns" :label="column.title" :width="column.width">
            <template slot-scope="scope">
              <div v-if="column.type == 'img'">
                <img v-for="(file, vIndex) in getFilePath( scope.row[column.field], column)" :key="vIndex"
                  @click="viewImg(scope.row, column, file.path)" class="table-img" :src="file.path" />
              </div>
              <div v-else-if="column.type=='format'">
              </div>
              <div v-else-if="column.type=='opt'">
                <Button ghost size="small" @click="edit(scope.row)" type="primary"
                  style="margin-left: 10px;">编辑</Button>
                <Button ghost size="small" @click="del(scope.row)" type="error" style="margin-left: 10px;">删除</Button>
              </div>

              <div v-else>{{scope.row[column.field]}}</div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" :current-page="paginations.page"
            :page-size="paginations.page_size" background layout="prev, pager, next" :total="paginations.total">
          </el-pagination>
        </div>
      </div>

    </div>

    <el-dialog title="教育培训计划编辑" :visible.sync="dialogFormVisible" fullscreen width="90%">
      <div class="dialogConcent" :style="{height:clientHeight+'px'}">
        <el-scrollbar style="flex: 1">
          <el-form :model="form" :rules="rules" ref="form">
            <el-row>
              <el-col :span="8">
                <el-form-item label="年份" prop="year">
                  <el-date-picker v-model="form.year" type="year" placeholder="选择年份">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="批准人" prop="ratify_admin">
                  <el-autocomplete class="inline-input" v-model="form.ratify_admin" :fetch-suggestions="querySearch"
                    placeholder="请输入内容" :trigger-on-focus="false" @select="handleSelectRatify"></el-autocomplete>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="审核人" prop="audit_admin">
                  <el-autocomplete class="inline-input" v-model="form.audit_admin" :fetch-suggestions="querySearch"
                    placeholder="请输入内容" :trigger-on-focus="false" @select="handleSelectAudit"></el-autocomplete>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input type="textarea" v-model="form.remark"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="table-tree">
              <div style="display: flex;justify-content: flex-end;margin-right: 20px;margin-bottom: 10px;">
                <Button type="primary" @click="addRow()" size="small" style="margin-left: 10px;" ghost>新增</Button>
              </div>
              <el-table :data="this.form.train_plan_people_list" height="400" class="v-table"
                style="width: 100%;margin-bottom: 20px;">
                <el-table-column type="index" align="center">
                </el-table-column>
                <el-table-column prop="item_name" label="项目" width="200">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.item_name" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="person_name" align="left" label="参与人员">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.person_name" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="person_count" align="center" label="人数" width="130">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.person_count" type="number" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="host_unit" label="主办单位" width="160">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.host_unit" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="assist_unit" label="协办单位"  width="160">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.assist_unit" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="finish_time" label="完成时间" width="140">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.finish_time" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="fund" label="资金" width="140">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.fund" />
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="100" align="center">
                  <template slot-scope="scope">
                    <Button type="error" @click="delRow(scope.row,scope.$index)" size="small" style="margin-left: 10px;"
                      ghost>删除</Button>
                  </template>
                </el-table-column>
              </el-table>
            </div>


          </el-form>
        </el-scrollbar>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" v-preventReClick @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import LxHeader from "@/components/LxHeader/index.vue";
  import Tinymce from '@/components/Tinymce';
  import {
    getparameteritem
  } from "../../api/system/dictionary.js"
  import {
    save,
    store,
    del,
    get,
    listtrain
  } from "../../api/training/index.js";

  import {
    listCommondepartment,
    listCommonuser
  } from "../../api/common.js"
  export default {
    components: {
      LxHeader,
      Tinymce
    },
    data() {
      return {
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        tableHeight: 0,
        dialogFormVisible: false,
        formLabelWidth: "120px",
        clientHeight: 0,
        form: {
          id: "",
          year: "",
          audit_admin_id: "",
          audit_admin: "",
          ratify_admin_id: "",
          ratify_admin: "",
          remark: "",
          train_plan_people_list: [{
            item_name: "",
            person_name: "",
            person_count: "",
            host_unit: "",
            assist_unit: "",
            finish_time: "",
            fund: ""
          }]
        },
        tableData: [],
        rules: {
          year: [{
            required: true,
            message: '请选择年份',
            trigger: 'blur'
          }],
          audit_admin: [{
            required: true,
            message: '请选择审核人',
            trigger: 'blur'
          }],
          ratify_admin: [{
            required: true,
            message: '请选择批准人',
            trigger: 'blur'
          }]
        },
        tableHeight: 900,
        searchFields: {
          KeyWord: ""
        },
        columns: [{
            field: "year",
            title: "年份",
            type: "string",
            width: 280
          },
          {
            field: "remark",
            title: "备注",
            align: "left"
          },

          {
            field: "操作",
            title: "操作",
            width: 220,
            type: "opt",
            fixed: "right"
          }
        ],
        deptOptions: [],
        userOptions: []
      }
    },
    created() {
      this.initLoad();
      this.load();

    },
    methods: {
      addRow() {
        var len = this.form.train_plan_people_list.length;
        this.form.train_plan_people_list.push({
          item_name: "",
          person_name: "",
          person_count: "",
          host_unit: "",
          assist_unit: "",
          finish_time: "",
          fund: ""
        })
      },
      delRow(obj, index) {
        this.form.train_plan_people_list.splice(index, 1);
      },
      querySearch(queryString, cb) {

        listCommonuser({
          keyword: queryString
        }).then(res => {
          for (var mod of res.data) {
            mod.value = mod.name;
          }
          let results = res.data;
          // 调用 callback 返回建议列表的数据
          cb(results);
        }).catch(err => {})


      },
      handleSelectAudit(item) {
        this.form.audit_admin_id = item.id;
      },
      handleSelectRatify(item) {
        this.form.ratify_admin_id = item.id
      },
      initLoad() {
        var that = this;
        var clientHeight = document.documentElement.clientHeight
        var lxHeader_height = 96.5; //查询 头部
        var paginationHeight = 37; //分页的高度
        var topHeight = 50; //页面 头部
        let tableHeight = clientHeight - lxHeader_height - topHeight - paginationHeight - 20;
        that.tableHeight = tableHeight;
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      search() {
        this.paginations.page = 1;
        this.load();
      },
      load() {
        listtrain({
          page: this.paginations.page,
          page_size: this.paginations.page_size
        }).then(res => {
          this.tableData = res.data;
          this.paginations.total = res.total
        }).catch(error => {

        })
      },
      del(obj) {
        var that = this;
        if (obj) {
          this.$Modal.confirm({
            title: '确认要删除数据？',
            onOk: () => {
              del(obj.id).then(response => {
                this.$Message.success('操作成功');
                that.load();
              }).catch(error => {
                console.log(error)
                reject(error)
              })
            },
            onCancel: () => {
              //this.$Message.info('Clicked cancel');
            }
          });
        }
      },
      show(obj) {
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        this.dialogViewVisible = true;
        this.info(obj);
      },
      info(obj) {
        var that = this;
        get(obj.id).then(res => {
          let result = Object.assign(that.form, res);
          that.form = result;
          that.form.type = parseInt(result.type);

        }).catch(error => {
          //reject(error)
        })
      },
      edit(obj) {
        this.form = this.$options.data().form;
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        if (obj) {
          var that = this;
          that.info(obj);
        } else {}
        this.dialogFormVisible = true;
      },
      submitForm(formName) {
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (that.form.id) {
              save(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;

              }).catch(error => {
                //reject(error)
              })
            } else {
              store(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;
              }).catch(error => {
                //reject(error)
              })
            }
          } else {
            this.$Message.error('数据校验失败');
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        var that = this;
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      },
    }
  }
</script>

<style>
  .dialogConcent {
    overflow-y: auto;
    overflow-x: hidden !important;
    width: 99%;
  }
</style>
