<template>
  <el-dialog
    :title="!dataForm.totalId ? '新增' : '修改'"
    :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: left;padding-bottom: 1%;font: bold 16px 新宋体">一、内容选取</div>
        <el-form-item label="施工时间" prop="planTime">
          <el-date-picker
            :disabled="dataForm.approval===1"
            v-model="dataForm.planTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <div>
          <el-form-item label="立项名称" prop="planProjectId">
            <el-input readonly v-model="dataForm.project" placeholder="立项名称"></el-input>
          </el-form-item>
        </div>
        <el-form-item label="负责工长" prop="planDdId">
          <el-input disabled v-model="dataForm.fzgz" placeholder=" "></el-input>
        </el-form-item>
        <el-form-item label="带队人" prop="totalDdId">
          <el-select
            :disabled="dataForm.approval===1"
            v-model="dataForm.totalDdId"
            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="margin-bottom: 3%;text-align: center;font-size: 16px">
        <span style="padding:2% 2% 2% 2%;background-color: #c5c8ce">{{dataForm.planTime}}施工项目</span>
      </div>
      <div v-if="num === 0" style="text-align: left;padding: 0 0 1% 0;font: bold 16px 新宋体">一、自动生成属性信息</div>
      <div v-else style="text-align: left;padding: 5% 0 1% 0;font: bold 16px 新宋体">二、自动生成属性信息</div>
      <div style="font-size: 16px">
        <div>
          <div style="padding: 1% 0">
            <span style="margin-bottom: 2%">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="padding-top: 10%">
          <div style="padding: 1% 0">
            <span style="margin-bottom: 2%">2.合同属性信息</span>
          </div>
          <div v-if="dataContractList.length > 0">
            <div style="margin-left: 5%" v-for="(itemCh,indexSon) in dataContractList" :key="indexSon">
                <span v-if="itemCh.contractNewName !== null && itemCh.contractNewName !== '' && itemCh.contractNewName !== undefined">
                    ① 合同名称：{{ itemCh.contractNewName }}<br></span>
              <span v-else>① 合同名称：{{ itemCh.contractName }}<br></span>
              <span v-for="itemDic in dicZFYList" :key="itemDic.dictId"
                    v-if="itemCh.contractZfyId === itemDic.dictId">② 直非友：{{
                  itemDic.name
                }};<br></span>
              <span>③ 合同年份：{{ itemCh.contractYear }};<br></span>
              <span v-for="itemDic in dicSGDWList" :key="itemDic.dictId"
                    v-if="itemCh.contractSgdwId === itemDic.dictId">④ 施工单位：{{
                  itemDic.name
                }};<br></span>
              <span v-for="itemDic in dicJLDWList" :key="itemDic.dictId"
                    v-if="itemCh.contractJldw === itemDic.dictId">⑤ 监理单位：{{
                  itemDic.name
                }};<br></span>
              <span v-for="itemDic in dicSJDWList" :key="itemDic.dictId"
                    v-if="itemCh.contractYsdwId === itemDic.dictId">⑥ 一审单位：{{
                  itemDic.name
                }};<br></span>
              <span v-for="itemDic in dicSJDWList" :key="itemDic.dictId"
                    v-if="itemCh.contractEsdw === itemDic.dictId">⑦ 二审单位：{{
                  itemDic.name
                }};<br></span>
            </div>
          </div>
          <div v-else style="text-align: center">
            <span>无</span>
          </div>
        </div>
        <div>
          <div style="padding: 1% 0;min-height: 5vh">
            <span>3.人员信息</span>
          </div>
          <div v-if="dataForm.totalDdId !== null && dataForm.totalDdId !== '' && dataForm.totalDdId"
               style="min-height: 5vh;margin-left: 5%">
            <span>① 带队人：{{ dataForm.totalDd }}</span>
            <br>
            <span
              v-if="dataForm.selectPersion !== '' && dataForm.selectPersion !== null && dataForm.selectPersion !== undefined">②  施工人员：{{
                dataForm.selectPersion
              }}</span>
          </div>
          <div v-else style="text-align: center;padding-top: 1%">
            <span>暂无数据</span>
          </div>
        </div>
        <div class="showDivDo" style="padding: 2%"></div>
      </div>

      <div v-if="num === 0" style="text-align: left;padding: 5% 0 1% 0;font: bold 16px 新宋体">二、施工内容</div>
      <div v-else style="text-align: left;padding: 5% 0 1% 0;font: bold 16px 新宋体">三、施工内容</div>
      <el-form-item label="施工内容" prop="totalSgSummary">
        <el-input :disabled="dataForm.approval===1 || num === 0" v-model="dataForm.totalSgSummary" placeholder="施工内容输入"></el-input>
      </el-form-item>
      <el-form-item label="工具/材料" prop="totalCl">
        <el-input :disabled="dataForm.approval===1 || num === 0" v-model="dataForm.totalCl" placeholder="工具/材料"></el-input>
      </el-form-item>
      <el-form-item label="机械" prop="totalJx">
        <el-input :disabled="dataForm.approval===1 || num === 0" v-model="dataForm.totalJx" placeholder="机械"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        num: 0, // 跳转标记
        isOneDayList: [],
        dicSgddList: [],
        dicGclbList: [],
        dicSgjdList: [],
        dicJsdwList: [],
        dicZFYList: [],
        dicSGDWList: [],
        dicJLDWList: [],
        dicSJDWList: [],
        ryList: [],
        dataProjectList: [],
        dataContractList: [],
        selectPersionIdList: [],
        dataForm: {
          id: '',
          totalDdId: '',
          totalDd: '',
          totalProjectId: '',
          totalProject: '',
          selectPersionId: '',
          selectPersion: '',
          totalPersonNum: 0,
          isOneDay: '',
          totalSgSummary: '',
          totalCl: '',
          totalJx: '',
          planTime: '',
          totalDate: '',
          status: '',
          approval: ''
        },
        dataOldForm: {
          id: '',
          totalProjectId: '',
          selectPersionId: '',
          totalPersonNum: '',
          isOneDay: '',
          totalSgSummary: '',
          totalCl: '',
          totalJx: '',
          planTime: '',
          totalDdId: '',
          totalDate: '',
          status: '',
          approval: ''
        },
        dataRule: {
          planTime: [
            { required: true, message: '施工时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, num) {
        console.log(id)
        this.dataForm.totalId = id || 0
        this.num = num
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.totalId) {
            this.$http({
              url: this.$http.adornUrl(`/business/businesstotal/infoNew/${this.dataForm.totalId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              // console.log(data)
              if (data && data.code === 0) {
                this.dataForm = {
                  ...data.businessTotal
                }
                this.dataOldForm = {
                  ...data.businessTotal
                }
                if (this.dataForm.projectNewName !== null) {
                  this.dataForm.project = this.dataForm.projectNewName
                  this.dataOldForm.project = this.dataForm.projectNewName
                }
                data.businessTotal.selectPersionId.split(',').forEach((item) => {
                  this.selectPersionIdList.push(parseInt(item))
                })
                this.ryList.forEach((item) => {
                  // console.log(item.userId === this.dataForm.totalDdId)
                  if (item.userId === this.dataForm.totalDdId) {
                    this.dataForm.totalDd = item.name
                  }
                })
                // 施工队员
                if (this.dataForm.selectPersionId !== null && this.dataForm.selectPersionId !== '' && this.dataForm.selectPersionId !== undefined) {
                  if (this.dataForm.selectPersionId.includes(',')) {
                    this.ryList.forEach((item) => {
                      this.dataForm.selectPersionId.toString().split(',').forEach((itemPlan) => {
                        console.log(itemPlan.toString() === item.userId.toString())
                        if (itemPlan.toString() === item.userId.toString()) {
                          if (this.dataForm.selectPersion === undefined) {
                            this.dataForm.selectPersion = item.name
                          } else {
                            this.dataForm.selectPersion += ',' + item.name
                          }
                        }
                      })
                    })
                  } else {
                    this.ryList.forEach((item) => {
                      if (item.userId.toString() === this.dataForm.selectPersionId.toString()) {
                        this.dataForm.selectPersion = item.name
                      }
                    })
                  }
                }
                this.getProjectContent()
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.approval === 1) {
              this.$message('待上次申请审批之后，在重新申请')
            } else if (this.dataForm.approval === 0 && JSON.stringify(this.dataForm) === JSON.stringify(this.dataOldForm)) {
              this.$message('请修改后在进行提交')
            } else {
              // 提交到审批表中
              this.$http.request({
                url: this.$http.adornUrl('/business/businessapproval/save'),
                method: 'post',
                data: this.$http.adornData({
                  'approvalId': this.dataForm.id,
                  'approvalName': '今日总结',
                  'approvalData': JSON.stringify(this.dataForm),
                  'approvalOldData': JSON.stringify(this.dataOldForm),
                  'approvalType': 5,
                  'approvalSuccess': 0
                })
              })
              // 更新总结内的信息
              this.$http.request({
                url: this.$http.adornUrl('/business/businesstotal/update'),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.id,
                  'approval': 1
                })
              }).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)
                }
              })
            }
          }
        })
      },
      // 获取立项和合同属性
      getProjectContent () {
        // 获取立项属性  合同属性 最新的明日计划信息
        this.dataProjectList = []
        this.dataContractList = []
        if (this.dataForm.totalProjectId !== '') {
          this.$http.request({
            url: this.$http.adornUrl('/business/businesstotal/projectInfo/' + this.dataForm.totalProjectId),
            method: 'get'
          }).then(({data}) => {
            // console.log(data);
            if (data && data.code === 0) {
              this.dataProjectList = data.businessProject
              // console.log(this.dataProjectList);
              if (data.businessProject[0].projectNewName !== null) {
                this.dataForm.totalProject = data.businessProject[0].projectNewName
              } else {
                this.dataForm.totalProject = data.businessProject[0].project
              }
            }
          })
          // 根据立项id获取已录入的合同信息
          this.$http.request({
            url: this.$http.adornUrl('/business/businesstotal/contractInfo/' + this.dataForm.totalProjectId),
            method: 'get'
          }).then(({data}) => {
            // console.log(data);
            if (data && data.code === 0) {
              this.dataContractList = data.contractList[this.dataForm.totalProjectId]
              // console.log(this.dataContractList[23][0]);
              // console.log(this.dataContractList);
            }
          })
        }
        this.$forceUpdate()
      },
      // 选择的带队人
      selectDd (item) {
        this.dataForm.totalDd = 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.request({
        url: this.$http.adornUrl('/business/businesstotal/dictList'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          // console.log(data);
          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/businesstotal/fpWlzList'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.ryList = data.page
          // console.log(this.ryList);
        }
      })
    }
  }
</script>
