<template>
  <el-dialog :title="title" :visible.sync="visible" width="80%" append-to-body>
    <div class="app-container">
      <div class="filter-container">
        <el-input
          v-model="listQuery.keyWord"
          placeholder="关键字"
          style="width: 200px"
          class="filter-item"
          :maxlength="30"
          @keyup.enter.native="handleFilter"
        />
        <el-button
          v-waves
          style="margin-left: 40px"
          class="filter-item"
          type="primary"
          @click="handleFilter"
        >
          查询
        </el-button>
      </div>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        :max-height="tableHeight"
      >
        <el-table-column label="序号" align="center" width="80" type="index">
          <template scope="scope">
            <span>{{ (listQuery.pageNum - 1) * 10 + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="`${title}名称`" align="center" prop="gxName" />
        <el-table-column label="计划开始时间" align="center" prop="planStartTime" />
        <el-table-column label="计划结束时间" align="center" prop="planEndTime" />
        <el-table-column label="实际开始时间" align="center" prop="startTime" />
        <el-table-column label="实际结束时间" align="center" prop="endTime" />
        <el-table-column label="进度占比" align="center" prop="gxWeight" />
        <el-table-column label="延迟原因" align="center" prop="delaReason" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
          <template slot-scope="{ row, $index }">
            <el-button type="text" size="mini" @click="handleUpdateProcess(row, $index)">进度填报</el-button>
          </template>
        </el-table-column>
      </el-table>

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

    <el-dialog :title="`${title}进度填报`" :visible.sync="processVisible" width="80%" append-to-body>
      <div class="app-container">
        <div class="filter-container">
          <el-input
            v-model="listQuery2.keyWord"
            placeholder="关键字"
            style="width: 200px"
            class="filter-item"
            :maxlength="30"
            @keyup.enter.native="handleFilter2"
          />
          <el-button
            v-waves
            style="margin-left: 40px"
            class="filter-item"
            type="primary"
            @click="handleFilter2"
          >
            查询
          </el-button>

          <el-button class="filter-item" style="margin-left: 10px" type="primary" @click="handleCreateProcess">
            新增
          </el-button>
        </div>

        <el-table
          :key="tableKey2"
          v-loading="listLoading"
          :data="list2"
          border
          fit
          highlight-current-row
          style="width: 100%"
          :max-height="tableHeight"
        >
          <el-table-column label="序号" align="center" width="80" type="index">
            <template scope="scope">
              <span>{{ (listQuery.pageNum - 1) * 10 + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="`${title}名称`" align="center" prop="gxName" />
          <el-table-column label="工序进度" align="center" prop="gxProgress" />
          <el-table-column label="填报时间" align="center" prop="createTime" />
        </el-table>

        <pagination
          v-show="total2 > 0"
          :total="total2"
          :page.sync="listQuery2.pageNum"
          :limit.sync="listQuery2.pageSize"
          @pagination="getList"
        />
      </div>
      <el-dialog append-to-body :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="630px" @closed="handleClosedModal">
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="right"
          label-width="120px"
        >
          <el-form-item label="当前工序" prop="nowGxId">
            <el-select v-model="temp.nowGxId" placeholder="当前工序" style="width: 100%">
              <el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="工序进度(%)：" prop="gxProgress">
            <el-input v-model="temp.gxProgress" placeholder="工序进度" :maxlength="50" />
          </el-form-item>

          <el-form-item v-if="showDelaReason" label="延迟原因：" prop="delaReason">
            <el-input v-model="temp.delaReason" placeholder="延迟原因" :maxlength="50" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false"> 关闭 </el-button>
          <el-button type="primary" @click="createData">
            提交
          </el-button>
        </div>
      </el-dialog>
    </el-dialog>
  </el-dialog>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import moment from 'moment'

import _ from 'lodash/fp'
import {
  createProcessPlan,
  getDictList,
  getProgressProcessList,
  getProgressProcessProgressList
} from '@/api/basic/progress'

const defaultTmp = {
  planId: '',
  gxId: '',
  nowGxId: '',
  gxProgress: '',
  planEndTime: '',
  delaReason: '',
  proType: '',
  planName: ''
}
export default {
  name: 'ProcessTable2',
  components: { Pagination },
  directives: { waves },
  props: ['bidCode', 'processType', 'componentVisible', 'currentFormData'],
  data() {
    return {
      // modalTile: '工序计划',
      // modalTile: '杆塔计划',
      tableKey: 0,
      tableKey2: 1,
      list: [],
      list2: [],
      temp2: [],
      typeList: [],
      total: 0,
      total2: 0,
      listLoading: false,
      showDelaReason: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        bidCode: '',
        proType: ''
      },
      listQuery2: {
        pageNum: 1,
        pageSize: 10,
        planId: '',
        keyword: ''
      },
      tableHeight: 650,
      showReviewer: false,
      temp: _.cloneDeep(defaultTmp),
      dialogFormVisible: false,
      dialogStatus: '',
      processVisible: false,
      textMap: {
        update: '编辑',
        create: '新增'
      },
      processModalVisible: false,
      rules: {
        nowGxId: [{ required: true, message: '请选择', trigger: 'change' }],
        gxWeight: [{ required: true, message: '不能为空', trigger: 'blur' }],
        delaReason: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.componentVisible
      },
      set(val) {
        this.$emit('update:componentVisible', val)
      }
    },
    title() {
      return this.processType === '线路' ? '杆塔' : '工序计划'
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList()
        this.getDictList()
      }
    }
  },
  methods: {
    isDelay(data) {
      const currentTime = moment()
      const targetDate = moment(data, 'YYYY-MM-DD')
      return currentTime.isAfter(targetDate)
    },
    getDictList() {
      getDictList({ param: this.processType === '线路' ? '500' : '400' }).then(res => {
        this.typeList = res.data
      })
    },
    getList() {
      this.listLoading = true
      this.listQuery.bidCode = this.bidCode
      this.listQuery.proType = this.processType
      getProgressProcessList(this.listQuery).then((response) => {
        this.list = response.rows
        this.total = response.total
      }).finally(() => {
        this.listLoading = false
      })
    },
    getList2() {
      this.listLoading = true
      getProgressProcessProgressList(this.listQuery2).then((response) => {
        this.list2 = response.rows.map(item => {
          item.gxName = this.temp.gxName
          return item
        })
        this.total2 = response.total
      }).finally(() => {
        this.listLoading = false
      })
    },
    // 查询
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    // 查询
    handleFilter2() {
      this.listQuery2.pageNum = 1
      this.getList2()
    },
    // 新增
    handleCreateProcess() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      this.temp.proType = this.processType
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createProcessPlan(this.temp).then((response) => {
            this.$message({
              showClose: true,
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.getList2()
          }).finally(() => {
            // this.dialogFormVisible = false
          })
        }
      })
    },
    // 编辑
    handleUpdateProcess(row) {
      this.showDelaReason = this.isDelay(row.planEndTime)
      console.log(this.showDelaReason)
      this.processVisible = true
      this.temp.gxId = row.gxId
      this.listQuery2.planId = row.planId
      this.temp.planId = row.planId
      this.temp.planEndTime = row.planEndTime
      this.temp.gxName = row.gxName
      this.getList2()
    },
    handleClosedModal() {
      this.$refs['dataForm'].resetFields()
      this.temp = _.cloneDeep(defaultTmp)
    }
  }
}
</script>
