<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <div v-if="num === 1">
        <div style="text-align: leftpadding-bottom: 1%font: bold 16px 新宋体">一、内容选取</div>
        <el-form-item label="施工时间" prop="planTime">
          <el-date-picker
            v-model="dataForm.planTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <div @click="selectLx()">
          <el-form-item label="立项名称" prop="planProjectId">
            <el-input readonly v-model="dataForm.project" placeholder="立项名称" @click="selectLx()"></el-input>
          </el-form-item>
        </div>
        <el-form-item label="负责工长" prop="planDdId">
          <el-input disabled v-model="dataForm.planFzgz" placeholder="负责工长"></el-input>
        </el-form-item>
        <el-form-item label="带队人" prop="planDdId">
          <el-select
            :disabled="dataForm.approval===1"
            v-model="dataForm.planDdId"
            placeholder="选择带队人"
            style="width: 100%"
          >
            <el-option
              v-for="item in ryList"
              :key="item.userId"
              :label="item.name"
              :value="item.userId"
              @click.native="selectDd(item)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="施工人员" prop="selectPersionIdList">
          <el-select
            :disabled="dataForm.approval===1"
            v-model="selectPersionIdList"
            placeholder="选择施工人员"
            style="width: 100%"
            multiple
          >
            <el-option
              v-for="item in ryList"
              :key="item.userId"
              :label="item.name"
              :value="item.userId"
              @click.native="selectSg()"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否一天" prop="isOneDay">
          <el-select
            :disabled="dataForm.approval===1"
            v-model="dataForm.isOneDay"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in isOneDayList"
              :key="item.dictId"
              :label="item.name"
              :value="item.dictId"
            ></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div v-if="num === 0" style="text-align: leftpadding: 5% 0 1% 0font: bold 16px 新宋体">一、自动生成属性信息</div>
      <div v-else style="text-align: leftpadding: 5% 0 1% 0font: bold 16px 新宋体">二、自动生成属性信息</div>
      <div style="font-size: 16px">
        <div>
          <div style="padding: 1% 0">
            <span>1.立项属性信息</span>
          </div>
          <div v-if="dataProjectList.length > 0">
            <div style="margin-left: 5%" v-for="(itemChildren,indexSon) in dataProjectList"
                 :key="itemChildren.projectId">
                <span
                  v-if="itemChildren.projectNewName !== null && itemChildren.projectNewName !== '' && itemChildren.projectNewName !== undefined">
                    ① 立项名称：{{ itemChildren.projectNewName }}<br></span>
              <span v-else>① 立项名称：{{ itemChildren.project }}<br></span>
              <span v-for="itemDic in dicJsdwList" :key="itemDic.dictId"
                    v-if="itemChildren.projectJsdwId === itemDic.dictId">② 建设单位：{{ itemDic.name }}<br></span>
              <span>③ 年份：{{ itemChildren.projectYear }}<br></span>
              <span>④ 开工时间：{{ itemChildren.projectStart }}<br></span>
              <span>⑤ 竣工时间：{{ itemChildren.projectEnd }}<br></span>
              <span v-for="itemDic in dicSJDWList" :key="itemDic.dictId"
                    v-if="itemChildren.projectSgddId === itemDic.dictId">⑥ 施工地点：{{ itemDic.name }}<br></span>
              <!--                  <span>工程类别：{{ itemDic.name }}</span>-->
              <span v-for="itemDic in dicSgjdList" :key="itemDic.dictId"
                    v-if="itemChildren.projectSgjdId === itemDic.dictId">⑦ 施工进度：{{ itemDic.name }}<br></span>
            </div>
          </div>
          <div v-else style="text-align: center">
            <span>无</span>
          </div>
        </div>
        <div style="margin-top: 10%">
          <div style="padding: 1% 0">
            2.合同属性:
          </div>
          <div v-if="dataContractList.length > 0">
            <div style="margin-left: 5%">
                <span>① 合同名称：{{
                    dataContractList[0].contractNewName ? dataContractList[0].contractNewName : dataContractList[0].contractName
                  }}<br></span>
              <span>② 直非友：{{
                  dataContractList[0].zfy ? dataContractList[0].zfy : "暂无"
                }}<br></span>
              <span>③ 合同年份：{{
                  dataContractList[0].contractYear ? dataContractList[0].contractYear : "暂无"
                }}<br></span>
              <span>④ 施工单位：{{
                  dataContractList[0].sgdw ? dataContractList[0].sgdw : "暂无"
                }}<br></span>
              <span>⑤ 监理单位：{{
                  dataContractList[0].jldw ? dataContractList[0].jldw : "暂无"
                }}<br></span>
              <span>⑥ 一审单位：{{
                  dataContractList[0].ysdw ? dataContractList[0].ysdw : "暂无"
                }}<br></span>
              <span>⑦ 二审单位：{{
                  dataContractList[0].esdw ? dataContractList[0].esdw : "暂无"
                }}<br></span>
            </div>
          </div>
          <div v-else style="margin-left: 5%text-align: center">
            <span>暂无<br></span>
          </div>
        </div>
        <div>
          <div style="padding-top: 10%">
            <div style="padding: 1% 0">
              3.人工:
            </div>
          </div>
          <div v-if="dataForm.planDdId !== null && dataForm.planDdId !== '' && dataForm.planDdId"
               style="min-height: 5vhmargin-left: 5%">
            <span>① 带队人：{{ dataForm.planDd }}</span>
            <br>
            <span
              v-if="dataForm.selectPersion !== '' && dataForm.selectPersion !== null && dataForm.selectPersion !== undefined">②   施工人员：{{
                dataForm.selectPersion
              }}</span>
          </div>
          <div v-else style="text-align: centerpadding-top: 1%">
            <span>请在上方选择</span>
          </div>
        </div>
        <div class="showDivDo" style="padding: 2%"></div>
      </div>

      <div v-if="num === 0" style="text-align: leftpadding: 5% 0 1% 0font: bold 16px 新宋体">二、施工内容</div>
      <div v-else style="text-align: leftpadding: 5% 0 1% 0font: bold 16px 新宋体">三、施工内容</div>
      <el-form-item label="施工内容" prop="planSgSummary">
        <el-input v-model="dataForm.planSgSummary" placeholder="施工内容输入"></el-input>
      </el-form-item>
      <el-form-item label="工具/材料" prop="planCl">
        <el-input v-model="dataForm.planCl" placeholder="工具/材料"></el-input>
      </el-form-item>
      <el-form-item label="机械" prop="planJx">
        <el-input v-model="dataForm.planJx" placeholder="机械"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button v-if="num === 1" type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    <el-dialog
      :title="'立项选取'"
      :visible.sync="selectLxId"
      width="45%"
      style="padding-top: 2%"
      append-to-body>
<!--      <el-form :inline="true" :model="selectDataForm" @keyup.enter.native="getselectProject()">
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="selectDataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getselectProject()">查询</el-button>
       </el-form-item>
      </el-form>-->
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%">
        <el-table-column
          type="index"
          header-align="center"
          align="center"
          label="序号"
          width="50">
        </el-table-column>
        <el-table-column
          prop="projectYear"
          header-align="center"
          align="center"
          label="施工年份">
        </el-table-column>
        <el-table-column
          prop="projectJsdwId"
          header-align="center"
          align="center"
          label="建设单位">
          <template slot-scope="scope">
            <template v-for="item in dicJsdwList" v-if="scope.row.projectJsdwId === item.dictId">
              {{item.name}}
            </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="project"
          header-align="center"
          align="center"
          label="立项名称">
        </el-table-column>
        <el-table-column
          prop="projectSgddId"
          header-align="center"
          align="center"
          label="施工地点">
          <template slot-scope="scope">
            <template v-for="item in dicSgddList" v-if="scope.row.projectSgddId === item.dictId">
              {{item.name}}
            </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="projectSgjdId"
          header-align="center"
          align="center"
          label="施工进度">
          <template slot-scope="scope">
            <template v-for="item in dicSgjdList" v-if="scope.row.projectSgjdId === item.dictId">
              {{item.name}}
            </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="projectPhotoConut"
          header-align="center"
          align="center"
          label="现场张片">
        </el-table-column>
        <el-table-column
          prop="isContant"
          header-align="center"
          align="center"
          label="导入合同">
          <template slot-scope="scope">
            <span v-if="scope.row.isContant === 0">否</span>
            <span v-if="scope.row.isContant === 1">是</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="70"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="chooseProject(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <span slot="footer" class="dialog-footer">
        <el-button @click="selectLxId = false">取消</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        visible: false,
        selectLxId: false,
        dataListLoading: false,
        num: '',
        isOneDayList: [],
        dicSgddList: [],
        dicGclbList: [],
        dicSgjdList: [],
        dicJsdwList: [],
        dicZFYList: [],
        dicSGDWList: [],
        dicJLDWList: [],
        dicSJDWList: [],
        ryList: [],
        dataProjectList: [],
        dataContractList: [],
        selectPersionIdList: [],
        dataList: [], // 选择的立项名称
        dataForm: {
          id: 0,
          planProjectId: '',
          planTime: '',
          planDdId: '',
          selectPersionId: '',
          planPersonNum: '',
          isOneDay: '',
          planSgSummary: '',
          planCl: '',
          planJx: '',
          type: 0,
          status: 0,
          approval: 0,
          planDd: '',
          planFzgz: '',
          selectPersion: '',
          isOne: '',
          isTotal: 0
        },
        selectDataForm: {

        },
        dataRule: {
          planTime: [
            { required: true, message: '施工时间不能为空', trigger: 'blur' }
          ],
          project: [
            { required: true, message: '立项名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, num) {
        this.num = num
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessplan/infoNew/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.planProjectId = data.businessPlan.planProjectId
                this.dataForm.planTime = data.businessPlan.planTime
                this.dataForm.planDdId = data.businessPlan.planDdId
                data.businessPlan.selectPersionId.split(',').forEach((item) => {
                  this.selectPersionIdList.push(parseInt(item))
                })
                console.log(this.selectPersionIdList)
                this.dataForm.planPersonNum = data.businessPlan.planPersonNum
                this.dataForm.isOneDay = data.businessPlan.isOneDay
                this.dataForm.planSgSummary = data.businessPlan.planSgSummary
                this.dataForm.planCl = data.businessPlan.planCl
                this.dataForm.planJx = data.businessPlan.planJx
                this.dataForm.type = data.businessPlan.type
                this.dataForm.status = data.businessPlan.status
                this.dataForm.createTime = data.businessPlan.createTime
                this.dataForm.updateTime = data.businessPlan.updateTime
                this.dataForm.createName = data.businessPlan.createName
                this.dataForm.approval = data.businessPlan.approval
                this.ryList.forEach((item) => {
                  if (item.userId === this.dataForm.planDdId) {
                    this.dataForm.planDd = item.name
                  }
                })
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.selectPersionIdList.forEach((item) => {
          if (this.dataForm.selectPersionId === '') {
            this.dataForm.selectPersionId = item
          } else {
            this.dataForm.selectPersionId += ',' + item
          }
        })
        console.log(this.dataForm.selectPersionId)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessplan/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'planProjectId': this.dataForm.planProjectId,
                'planTime': this.dataForm.planTime,
                'planDdId': this.dataForm.planDdId,
                'selectPersionId': this.dataForm.selectPersionId,
                'planPersonNum': this.dataForm.planPersonNum,
                'isOneDay': this.dataForm.isOneDay,
                'planSgSummary': this.dataForm.planSgSummary,
                'planCl': this.dataForm.planCl,
                'planJx': this.dataForm.planJx,
                'type': this.dataForm.type,
                'status': this.dataForm.status,
                'isTotal': this.dataForm.isTotal,
                'approval': this.dataForm.approval
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 选择立项
      selectLx () {
        this.selectLxId = true
        this.getselectProject()
      },
      getselectProject () {
        this.dataListLoading = true
        this.dataList = []
        this.$http.request({
          url: this.$http.adornUrl('/business/businessplan/projectList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'projectJsdwId': this.dataForm.projectJsdwId,
            'projectYear': this.dataForm.projectYear,
            'projectSgddId': this.dataForm.projectSgddId,
            'projectGclbId': this.dataForm.projectGclbId,
            'projectSgjdId': this.dataForm.projectSgjdId,
            'isContant': this.dataForm.isContant,
            'gjz': this.dataForm.gjz,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            data.page.list.forEach((item) => {
              if (item.projectNewName !== null) {
                item.project = item.projectNewName
              }
              this.dataList.push(item)
            })
            this.totalPage = data.page.totalCount
          }
        })
        this.dataListLoading = false
      },
      // 选择立项
      chooseProject (project) {
        this.selectLxId = false
        this.dataForm.planProjectId = project.projectId
        this.dataForm.project = project.project
        this.selectDataForm = {}
        this.ryList.forEach((item) => {
          if (item.userId === project.projectFzgzId) {
            this.dataForm.planFzgz = item.name
          }
        })
        this.getProjectContent()
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getselectProject()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getselectProject()
      },
      // 获取立项和合同属性
      getProjectContent () {
        // 获取立项属性  合同属性 最新的明日计划信息
        this.dataProjectList = []
        this.dataContractList = []
        if (this.dataForm.planProjectId !== undefined) {
          this.$http.request({
            url: this.$http.adornUrl('/business/businessplan/lxHtPlanInfo/' + this.dataForm.planProjectId),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              let res = data.list
              // console.log(res)
              // 立项
              // 以防报错
              let tmpProjectId = ''
              let tmpProjectName = ''
              if (res.project.length > 0) {
                this.dataProjectList = res.project
                // console.log(this.dataProjectList)
                this.dataProjectList.forEach((item) => {
                  this.dataForm.planFzgz = item.fzgz
                  this.dataForm.planProjectId = item.projectId
                  tmpProjectId = item.projectId
                  if (item.projectNewName !== null) {
                    this.dataForm.project = item.projectNewName
                  } else {
                    this.dataForm.project = item.project
                  }
                  tmpProjectName = this.dataForm.project
                })
                // console.log(this.dataForm.project)
              }
              // 合同
              if (res.contract[this.dataForm.planProjectId].length > 0) {
                this.dataContractList = res.contract[this.dataForm.planProjectId]
                // console.log(this.dataContractList.length)
              }
              // 计划
              if (res.plan !== null && this.id === undefined) { // 新选中的立项是否有明日计划  如果有则显示信息
                this.dataForm = {
                  ...res.plan
                }
                this.dataForm.id = ''
                this.dataForm.planProjectId = tmpProjectId
                this.dataForm.project = tmpProjectName
              }
            }
          })
        }
        this.$forceUpdate()
      },
      // 选择的带队人
      selectDd (item) {
        this.dataForm.planDd = item.name
      },
      // 选择的施工人员
      selectSg () {
        this.dataForm.selectPersion = ''
        this.selectPersionIdList.forEach((item) => {
          this.ryList.forEach((itemCh) => {
            if (item === itemCh.userId) {
              if (this.dataForm.selectPersion === '') {
                this.dataForm.selectPersion = itemCh.name
              } else {
                this.dataForm.selectPersion += ',' + itemCh.name
              }
            }
          })
        })
        // console.log(this.dataForm.selectPersion)
      }
    },
    mounted () {
      this.$http({
        url: this.$http.adornUrl('/business/businessplan/dictList'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          data.data.forEach((item) => {
            if (item.dictCode === 'is_one_day') {
              this.isOneDayList = item.children
            } else if (item.dictCode === 'work_place') { // 施工地点
              this.dicSgddList = item.children
            } else if (item.dictCode === 'work_type') { // 工程类别
              this.dicGclbList = item.children
            } else if (item.dictCode === 'work_tatus') { // 施工进度
              this.dicSgjdList = item.children
            } else if (item.dictCode === 'construction_unit') { // 建设单位
              this.dicJsdwList = item.children
            } else if (item.dictCode === 'is_friend') { // 直非友
              this.dicZFYList = item.children
            } else if (item.dictCode === 'building_section') { // 施工单位
              this.dicSGDWList = item.children
            } else if (item.dictCode === 'surveillance_unit') { // 监理单位
              this.dicJLDWList = item.children
            } else if (item.dictCode === 'approval_unit') { // 审批单位(一审单位、二审单位)
              this.dicSJDWList = item.children
            }
          })
        }
      })
      this.$http.request({
        url: this.$http.adornUrl('/business/businessplan/fpWlzList'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.ryList = data.page
        }
      })
    }
  }
</script>
