<template>
  <el-dialog
    :title="'修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataFormWzj" :rules="dataRule" ref="dataFormWzj" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <div>
        <div style="text-align: left;padding-bottom: 1%;font: bold 16px 新宋体">一、内容选取</div>
        <el-form-item label="施工时间" prop="planTime">
          <el-date-picker
            v-model="dataFormWzj.planTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <div>
          <el-form-item label="立项名称" prop="project">
            <el-input disabled v-model="dataFormWzj.project" placeholder="立项名称"></el-input>
          </el-form-item>
        </div>
        <el-form-item label="负责工长" prop="fzgz">
          <el-input disabled v-model="dataFormWzj.fzgz" placeholder="负责工长"></el-input>
        </el-form-item>
        <el-form-item label="带队人" prop="planDdId">
          <el-select
            :disabled="dataFormWzj.approval===1"
            v-model="dataFormWzj.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="dataFormWzj.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="dataFormWzj.approval===1"
            v-model="dataFormWzj.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 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>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="dataFormWzj.planDdId !== null && dataFormWzj.planDdId !== '' && dataFormWzj.planDdId"
               style="min-height: 5vh;margin-left: 5%">
            <span>① 带队人：{{ dataFormWzj.planDd }}</span>
            <br>
            <span
              v-if="dataFormWzj.selectPersion !== '' && dataFormWzj.selectPersion !== null && dataFormWzj.selectPersion !== undefined">②   施工人员：{{
                dataFormWzj.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 style="text-align: left;padding: 5% 0 1% 0;font: bold 16px 新宋体">三、施工内容</div>
      <el-form-item label="施工内容" prop="planSgSummary">
        <el-input v-model="dataFormWzj.planSgSummary" placeholder="施工内容输入"></el-input>
      </el-form-item>
      <el-form-item label="工具/材料" prop="planCl">
        <el-input v-model="dataFormWzj.planCl" placeholder="工具/材料"></el-input>
      </el-form-item>
      <el-form-item label="机械" prop="planJx">
        <el-input v-model="dataFormWzj.planJx" placeholder="机械"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="dataFormSubmit()">保存</el-button>
        <el-button @click="dataFormUp()">提交</el-button>
    </span>
  </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: [], // 选择的立项名称
      dataFormWzj: {
        id: 0,
        planProjectId: '',
        planTime: '',
        planDdId: '',
        selectPersionId: '',
        planPersonNum: '',
        isOneDay: '',
        planSgSummary: '',
        planCl: '',
        planJx: '',
        type: 0,
        status: 0,
        approval: 0,
        planDd: '',
        planFzgz: '',
        selectPersion: '',
        isOne: '',
        isTotal: 0,
        project: '',
        projectNewName: '',
        fzgz: ''
      },
      selectDataForm: {

      },
      dataRule: {
        planTime: [
          { required: true, message: '施工时间不能为空', trigger: 'blur' }
        ],
        project: [
          { required: true, message: '立项名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      // console.log(id)
      this.dataFormWzj.id = id
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataFormWzj'].resetFields()
        if (this.dataFormWzj.id) {
          this.$http({
            url: this.$http.adornUrl(`/business/businessplan/infoNew/${this.dataFormWzj.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            // console.log(data)
            if (data && data.code === 0) {
              this.dataFormWzj = {
                ...data.businessPlan
              }
              data.businessPlan.selectPersionId.split(',').forEach((item) => {
                this.selectPersionIdList.push(parseInt(item))
              })
              this.ryList.forEach((item) => {
                if (item.userId === this.dataFormWzj.planDdId) {
                  this.dataFormWzj.planDd = item.name
                }
              })
              if (this.dataFormWzj.projectNewName !== null) {
                this.dataFormWzj.project = this.dataFormWzj.projectNewName
              }
              console.log(this.dataFormWzj.fzgz)
              this.getProjectContent()
            }
          })
        }
      })
    },
    //   保存表单数据
    dataFormSubmit () {
      this.$http.request({
        url: this.$http.adornUrl('/business/businessplan/update'),
        method: 'post',
        data: this.$http.adornData({
          'id': this.dataFormWzj.id,
          'planTime': this.dataFormWzj.planTime,
          'planProjectId': this.dataFormWzj.planProjectId,
          'planDdId': this.dataFormWzj.planDdId,
          'selectPersionId': this.dataFormWzj.selectPersionId,
          'planPersonNum': this.dataFormWzj.planPersonNum,
          'isOneDay': this.dataFormWzj.isOneDay,
          'planSgSummary': this.dataFormWzj.planSgSummary,
          'planCl': this.dataFormWzj.planCl,
          'planJx': this.dataFormWzj.planJx,
          'type': this.dataFormWzj.type,
          'status': this.dataFormWzj.status,
          'isTotal': this.dataFormWzj.isTotal
        })
      })
    },

    // 提交
    dataFormUp () {
      // console.log(this.dataFormWzj);
      this.$http.request({
        url: this.$http.adornUrl('/business/businessplan/update'),
        method: 'post',
        data: this.$http.adornData({
          'id': this.dataFormWzj.id,
          'planTime': this.dataFormWzj.planTime,
          'planProjectId': this.dataFormWzj.planProjectId,
          'planDdId': this.dataFormWzj.planDdId,
          'selectPersionId': this.dataFormWzj.selectPersionId,
          'planPersonNum': this.dataFormWzj.planPersonNum,
          'isOneDay': this.dataFormWzj.isOneDay,
          'planSgSummary': this.dataFormWzj.planSgSummary,
          'planCl': this.dataFormWzj.planCl,
          'planJx': this.dataFormWzj.planJx,
          'type': 1,
          'status': this.dataFormWzj.status,
          'isTotal': 1
        })
      })
      this.$http.request({
        url: this.$http.adornUrl('/business/businesstotal/save'),
        method: 'post',
        data: this.$http.adornData({
          'planTime': this.dataFormWzj.planTime,
          'totalProjectId': this.dataFormWzj.planProjectId,
          'totalDdId': this.dataFormWzj.planDdId,
          'selectPersionId': this.dataFormWzj.selectPersionId,
          'totalPersonNum': this.dataFormWzj.planPersonNum,
          'isOneDay': this.dataFormWzj.isOneDay,
          'totalSgSummary': this.dataFormWzj.planSgSummary,
          'totalCl': this.dataFormWzj.planCl,
          'totalJx': this.dataFormWzj.planJx,
          'status': 0,
          'approval': 0
        })
      }).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)
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getselectProject()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getselectProject()
    },
    // 获取立项和合同属性
    getProjectContent () {
      // 获取立项属性  合同属性 最新的明日计划信息
      this.dataProjectList = []
      this.dataContractList = []
      if (this.dataFormWzj.planProjectId !== undefined) {
        this.$http.request({
          url: this.$http.adornUrl('/business/businessplan/lxHtPlanInfo/' + this.dataFormWzj.planProjectId),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            let res = data.list
            // console.log(res);
            // 立项
            // 以防报错
            if (res.project.length > 0) {
              this.dataProjectList = res.project
              // console.log(this.dataProjectList)
              this.dataProjectList.forEach((item) => {
                this.dataFormWzj.planFzgz = item.fzgz
                if (item.projectNewName !== null) {
                  this.dataFormWzj.project = item.projectNewName
                } else {
                  this.dataFormWzj.project = item.project
                }
              })
              // console.log(this.dataFormWzj.project)
            }
            // 合同
            if (res.contract[this.dataFormWzj.planProjectId].length > 0) {
              this.dataContractList = res.contract[this.dataFormWzj.planProjectId]
              // console.log(this.dataContractList.length);
            }
            // 计划
            if (res.plan !== null && this.id === undefined) { // 新选中的立项是否有明日计划  如果有则显示信息
              this.dataFormWzj = {
                ...res.plan
              }
              this.dataFormWzj.id = ''
            }
          }
        })
      }
      this.$forceUpdate()
    },
    // 选择的带队人
    selectDd (item) {
      this.dataFormWzj.planDd = item.name
    },
    // 选择的施工人员
    selectSg () {
      this.dataFormWzj.selectPersion = ''
      this.selectPersionIdList.forEach((item) => {
        this.ryList.forEach((itemCh) => {
          if (item === itemCh.userId) {
            if (this.dataFormWzj.selectPersion === '') {
              this.dataFormWzj.selectPersion = itemCh.name
            } else {
              this.dataFormWzj.selectPersion += ',' + itemCh.name
            }
          }
        })
      })
      // console.log(this.dataFormWzj.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.dataListLoading = false
    })
    this.$http.request({
      url: this.$http.adornUrl('/business/businessplan/fpWlzList'),
      method: 'get'
    }).then(({data}) => {
      if (data && data.code === 0) {
        this.ryList = data.page
      }
    })
  }
}
</script>
