<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>
              <Button type="primary" @click="edit()" 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" fixed="left" label="序号" align="center"> </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-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-if="column.type=='daterange'">
                  {{scope.row['start_date']}}至{{scope.row['end_date']}}
                </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>

      <el-dialog title="养护计划编辑" :visible.sync="dialogFormVisible" width="40%">
        <div class="dialogConcent">
          <el-scrollbar style="flex: 1">

            <el-form :model="form" :rules="rules" ref="form" label-position="right" :label-width="formLabelWidth">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="养护计划名称" prop="name">
                    <el-input v-model="form.name" placeholder="请填写养护计划名称" autocomplete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="养护项目" prop="item_type">
                    <el-select style="width:100%" v-model="form.item_type" placeholder="请选择养护项目">
                        <el-option
                          v-for="item in plantypes"
                          :key="item.id"
                          :label="item.value"
                          :value="item.id">
                        </el-option>
                      </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="养护片区" prop="area_id">
                    <el-select style="width:100%" v-model="form.area_id" placeholder="请选择养护片区">
                        <el-option
                          v-for="item in areaList"
                          :key="item.id"
                          :label="item.name"
                          :value="item.id">
                        </el-option>
                      </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="循环机制" prop="day">
                    <el-select style="width:100%" v-model="form.day" placeholder="请选择循环机制">
                        <el-option
                          v-for="item in dayList"
                          :key="item.id"
                          :label="item.value"
                          :value="item.value">
                        </el-option>
                      </el-select>
                  </el-form-item>
                </el-col>

                <el-col :span="24">
                  <el-form-item label="养护周期" prop="dateRange">
                    <el-date-picker
                          style="width:100%"
                          v-model="dateRange"
                          type="daterange"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          value-format="yyyy-MM-dd"
                          >
                        </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="养护责任人" prop="responsible_id">
                    <el-select style="width:100%" v-model="form.responsible_id" placeholder="请选择养护责任人">
                        <el-option
                          v-for="item in usersList"
                          :key="item.id"
                          :label="item.name"
                          :value="item.id">
                        </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" v-model="form.remark" placeholder="请填写备注" autocomplete="off"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

            </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>
  </div>
</template>

<script>
  import {
    listplan,
    get,
    store,
    save,
    del
  } from '../../../api/maintainmanagement/index.js'
  import {
    listuser
  } from '../../../api/system/user.js'
  import {
    listarea
  } from '../../../api/basic/area.js'
  import { getparameteritem } from '../../../api/system/dictionary.js'
  import LxHeader from "@/components/LxHeader/index.vue";

  export default {
    components: {
      LxHeader
    },
    data() {
      return {
        tableHeight: 0,
        dialogFormVisible: false,
        formLabelWidth: "120px",
        tableData: [],
        areaList:[],
        plantypes:[{
          id:1,
          value:"日常雨水设施养护"
        },{
          id:2,
          value:"在建工地养护"
        }],
        dayList:[],
        dateRange:[],
        usersList:[],
        tableHeight: 0,
        searchFields: {
          KeyWord: ""
        },
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        form: {
          name:"",
          item_type:"",
          area_id:"",
          day:"",
          start_date:"",
          end_date:"",
          responsible_id:"",
          remark:""
        },

        rules: {
          name:[{
            required: true,
            message: '请填写养护名称',
            trigger: 'blur',
          }],
          area_id:[{
            required: true,
            message: '请选择所属片区',
            trigger: 'blur',
          }]
        },

        columns: [{
            field: "name",
            title: "养护名称",
            type: "string",
            align: "left",
            width:250
          },
          {
              field: "item_type_name",
              title: "养护项目",
              type: "string",
              align: "center",
              width:180
          },
          {
              field: "area_name",
              title: "养护片区",
              type: "string",
              width:180,
              align: "center"
          },
          {
              field: "day",
              title: "循环机制",
              type: "string",
              width:180,
              align: "center"

          },
          {
              field: "daterange",
              title: "养护周期",
              type: "daterange",
              width:220,
              align:'center'
          },

          {
            field: "responsibleName",
            title: "责任人",
            type: "string",
            align: "center",
            width:180,
          },
          {
            field: "created_at",
            title: "提交日期",
            type: "string",
            align: "center",
            width:200,
            align:'center'
          },
          {
            field: "操作",
            title: "操作",
            width: 220,
            type: "opt",
            fixed:"right"
          }
        ],
      }
    },
    created() {
      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;
        listarea({
          page: 1,
          page_size: 999,
        }).then(response => {
          that.areaList = response.data;
        }).catch(error => {
          console.log(error)
          reject(error)
        });
        listuser().then(res => {
          that.usersList = res.data;
        }).catch(error => {
          console.log(error)
          reject(error)
        });
        // getparameteritem('plan-type').then(res => {
        //   that.plantypes = res.detail
        // })
        getparameteritem('plan-day').then(res => {
          that.dayList = res.detail
        })
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      load() {
        listplan({
          page: this.paginations.page,
          page_size: this.paginations.page_size,
          keyword: this.searchFields.KeyWord
        }).then(response => {
          for(var m of response.data){
            for(var k of this.plantypes){
              if(m.item_type==k.id){
                m.item_type_name = k.value
              }
            }
            m.area_name = m.area_info.name
            m.responsibleName = m.responsible?m.responsible.name :""
          }
          this.tableData = response.data;
          this.paginations.total = response.total;
        }).catch(error => {
          console.log(error)
          reject(error)
        });

      },
      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;
          that.dateRange = [result.start_date,result.end_date]

        }).catch(error => {
          //reject(error)
        })
      },

     edit(obj) {
       this.form = this.$options.data().form
       if (obj) {
         this.info(obj)
       }
       this.dialogFormVisible = true;
     },
      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');
            }
          });
        }
      },

      submitForm(formName) {
        this.form.start_date = this.dateRange[0]
        this.form.end_date = this.dateRange[1]
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (that.form.id) {
              save(that.form).then(response => {
                this.$Message.success('操作成功');
                that.load();
                that.dialogFormVisible = false;
              }).catch(error => {
                //reject(error)
              })
            } else {
              store(that.form).then(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;
        that.dateRange = [];
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;

      }
    }
  }
</script>

<style>
  .dialogConcent {
    overflow-y: auto;
  }
  .width100{
    width:100%;
  }
</style>
