<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="load" style="margin-left: 10px">查询</Button>
            </div>
          </slot>
        </LxHeader>
      </div>
      <div ref="lxTable">
        <el-table :data="tableData"  class="v-table" :height="tableHeight" style="width: 100%">
          <el-table-column type="index" width="50" align="center" fixed="left"> </el-table-column>
          <el-table-column :prop="column.field" :align="column.align" v-for="(column,index) in columns"
            :label="column.title" :width="column.width" :fixed="column.fixed">
            <template slot-scope="scope">
              <div v-for="safe in scope.row.safety_task_departments" v-if="safe.departments.id==department_user_id">
                <div v-if="column.type=='status'">
                  <div v-for="item in isStatus" v-if="safe.status==item.id">
                    {{item.name}}
                  </div>
                </div>
                <div v-else-if="column.type=='opt'">
                  <Button v-if="safe.status==0" ghost size="small" @click="edit(scope.row)" type="primary"
                    style="margin-left: 10px;">新增上报</Button>
                  <Button v-if="safe.status==1||safe.status==2" ghost size="small" @click="edit(safe,true)" type="primary"
                    style="margin-left: 10px;">编辑上报</Button>
                  <Button v-if="safe.status==2" ghost size="small"  @click="$refs['detailUpInfo'].getinfo(safe.id),$refs['detailUpInfo'].dialogFormVisible = true" type="primary"
                    style="margin-left: 10px;">查看上报</Button>
                </div>

                <div v-else-if="column.type=='format'">
                  <div v-if="column.field=='admin'">
                    {{scope.row[column.field]?scope.row[column.field].name:""}}
                  </div>
                  <div v-if="column.field=='department'">
                    {{scope.row[column.field]?scope.row[column.field].name:""}}
                  </div>

                  <div v-if="column.field=='title'">
                    <el-link type="primary" @click="$refs['detailInfo'].getinfo(scope.row.id),$refs['detailInfo'].dialogFormVisible = true">
                      {{scope.row["title"]}}
                    </el-link>
                  </div>
                </div>
                <div v-else>{{scope.row[column.field]}}</div>
              </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>

      <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" label-position="right" :label-width="formLabelWidth">
              <el-form-item label="内容" prop="content">
                <div style="width: 99.9%;">
                  <tinymce ref="tinymce" v-model="form.content" :height="300" />
                </div>
              </el-form-item>
              <el-form-item label="附件" prop="file_list">
                <el-upload class="upload-demo" :on-success="handlesuccess" :data="uploadOther"
                  action="/api/admin/upload-file" :on-remove="handleRemove" :before-remove="beforeRemove"
                  :on-exceed="handleExceed" :file-list="fileList">
                  <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
              </el-form-item>

            </el-form>
          </el-scrollbar>
        </div>
        <div slot="footer" class="dialog-footer" v-if="!isReportSave">
          <el-button @click="resetForm('form')">取 消</el-button>
          <el-button type="primary" v-preventReClick @click="submitForm('form', '2')">上 报</el-button>
      <!--    <el-button type="primary" v-preventReClick @click="submitForm('form','1')">保 存</el-button> -->
        </div>
        <div slot="footer" class="dialog-footer" v-if="isReportSave">
          <el-button @click="resetForm('form')">取 消</el-button>
         <!-- <el-button type="primary" v-preventReClick @click="submitForm('form','1')">保 存</el-button> -->
          <el-button type="primary" v-preventReClick @click="submitForm('form', '2')">上 报</el-button>
        </div>
      </el-dialog>



    </div>
    <!--    查看-->
    <detail ref="detailInfo"></detail>
    <detailUp ref="detailUpInfo"></detailUp>
  </div>
</template>

<script>
  import {
    getToken
  } from '@/utils/auth'
  import LxHeader from "@/components/LxHeader/index.vue";
  import Tinymce from '@/components/Tinymce'
  import detail from './components/viewInfo'
  import detailUp from './components/viewUpInfo'
  import {
    save,
    submitsave,
    get,
    listdept
  } from "../../api/daily/deptindex.js";
  import {
    listtask
  } from "../../api/daily/index.js";
  import {
    getInfo
  } from "../../api/user.js";

  export default {
    components: {
      LxHeader,
      Tinymce,detail,detailUp
    },
    data() {
      return {
        isReportSave: false,
        checkAll: false,
        isIndeterminate: true,
        department_user_id: "",
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        show_self: 1,
        tableHeight: 0,
        dialogFormVisible: false,
        dialogShowViewVisible: false,
        formLabelWidth: "120px",
        clientHeight: 0,
        form: {
          content: "",
          file_list: "",
        },
        fileList: [],
        tableData: [],
        rules: {
          content: [{
            required: true,
            message: '请输入内容',
            trigger: 'blur'
          }]
        },
        tableHeight: 900,
        searchFields: {
          KeyWord: ""
        },
        columns: [{
            field: "title",
            title: "任务名称",
            type: "format"
          },
          {
            field: "start_date",
            title: "开始时间",
            type: "date",
            width: 180,
            align: "center"
          },
          {
            field: "end_date",
            title: "结束时间",
            type: "date",
            width: 180,
            align: "center"
          },
          {
            field: "status",
            title: "上报状态",
            type: "status",
            width: 120,
            align: "center"
          },
          {
            field: "created_at",
            title: "任务发布时间",
            type: "string",
            width: 180,
            align: "center"
          },
          {
            field: "admin",
            title: "任务发布人",
            type: "format",
            width: 120,
            align: "center"
          },
          {
            field: "操作",
            title: "操作",
            width: 220,
            type: "opt",
            fixed: "right"
          }
        ],
        uploadOther: {
          token: ""
        },
        deptOptions: [],
        isStatus: [{
          id: 0,
          name: "未上报",
          opt: "上报"
        }, {
          id: 1,
          name: "已保存",
          opt: "编辑上报"
        }, {
          id: 2,
          name: "已上报",
          opt: "查看"
        }]
      }
    },
    created() {
      this.uploadOther.token = getToken();
      this.initLoad();
      this.load();

    },
    methods: {
      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;
        getInfo({
          // token:this.uploadOther.token;
        }).then(response => {
          this.department_user_id = response.department_id
        }).catch(error => {
          console.log(error)
          reject(error)
        });
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      load() {

        listtask({
          page: this.paginations.page,
          page_size: this.paginations.page_size,
          show_self: this.show_self,
          keyword:this.searchFields.KeyWord
        }).then(response => {
          this.tableData = response.data;
          this.paginations.page_size = response.total;
        }).catch(error => {
          console.log(error)
          reject(error)
        });

      },
      show(obj) {
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        this.dialogViewVisible = false;
        this.dialogShowViewVisible = true;
        this.info(obj);
      },
      info(obj) {
        var that = this;
        get(obj.id).then(res => {
          let result = Object.assign(that.form, res);
          // this.$set(that.form,res);
          that.form = result;
          let _files = [];
          for (var mod of result.files) {
            let m = Object.assign({}, mod);
            m.name = mod.original_name;
            _files.push(m);
          }
          that.form.safety_task_department_id = result.id;
          that.form.safety_task_id = result.safety_task_id;
          that.fileList = _files;
          this.$refs.tinymce.setContent(result.content);
        }).catch(error => {
          //reject(error)
        })
      },
      edit(obj, isnew) {
        this.form = this.$options.data().form;
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        this.$nextTick(function(){
          this.$refs.tinymce.setContent("");
        })
        if (isnew) {
          var that = this;
          that.form.content = "";
          that.info(obj);
          this.isReportSave = true;
        } else {
          this.clientHeight = document.documentElement.clientHeight - 84 - 110;
          this.form.safety_task_id = obj.id;
          this.isReportSave = false;
        }
        this.dialogFormVisible = true;
      },
      editSave(obj) {

      },
      submitForm(formName, status) {
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (status == "2") {
              that.form.status = status;
              save(that.form).then(response => {
                //console.log(response)
               this.$Message.success('操作成功');
               that.load();
               that.dialogFormVisible = false;
              }).catch(error => {
                //reject(error)
              })

            } else {
              save(that.form).then(response => {
                //console.log(response)
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;
              }).catch(error => {
                //reject(error)
              })
            }
            that.fileList = [];
          } else {
            this.$Message.error('数据校验失败');
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleRemove(file, fileList) {
        console.log(fileList);
        let listUrl = [];
        for (var m of fileList) {
          if (m.response)
            listUrl.push(m.response.id);
          else
            listUrl.push(m.id);
        }
        this.form.file_list = listUrl;
      },
      handleExceed(files, fileList) {},
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      handlesuccess(response, file, fileList) {
        console.log(fileList);
        let listUrl = [];
        for (var m of fileList) {
          if (m.response)
            listUrl.push(m.response.id);
          else
            listUrl.push(m.id);

        }
        this.form.file_list = listUrl;
      },
      resetForm(formName) {
        var that = this;
        that.fileList = [];
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
        that.dialogShowViewVisible = false;

      },
    }
  }
</script>

<style>
  .dialogConcent {
    overflow-y: auto;
  }
</style>
