<template>
  <div class="app-container" id="constructionSite">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择单位名称"  clearable>
          <el-option
            v-for="item in unitList"
            :key="item.unitId" filterable 
            :label="item.unitName"
            :value="item.unitId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工程名称" prop="projectId">
         <el-select v-model="queryParams.projectId" filterable placeholder="请选择工程名称"  clearable>
            <el-option
              v-for="item in projectList"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="cName">
        <el-input
          v-model="queryParams.cName"
          placeholder="请输入联系人姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人电话" prop="cPhone">
        <el-input
          v-model="queryParams.cPhone"
          placeholder="请输入联系人电话"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完成情况" prop="status">
        <el-input
          v-model="queryParams.status"
          placeholder="请选择完成情况"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >直转申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="dictId" />
      <el-table-column label="转让方单位名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="转让方工程名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="接收方单位名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="接收方工程名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="转让方联系人及电话" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="接收方联系人及电话" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="办理时间" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="不通过原因" align="center" prop="dictName" :show-overflow-tooltip="true" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >直转单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px"   append-to-body>
      <el-form :model="dialogParams" ref="dialogForm" :rules="rules" size="small" :inline="true" label-width="100px">
        <el-row>
          <el-form-item label="退料单位" prop="backUnitId">
            <el-select  v-model="dialogParams.backUnitId" filterable clearable @change="GetProData" style="width: 240px" placeholder="请选择">
              <el-option
                v-for="item in backUnitList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="退料工程" prop="backProId">
            <el-select  v-model="dialogParams.backProId" filterable clearable @change="GetUnitData" style="width: 240px" placeholder="请选择">
              <el-option
                v-for="item in backProjectList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="退料协议编号" prop="backAgreementCode">
            <el-input
              v-model="dialogParams.backAgreementCode"
              placeholder="请输入退料协议编号"
              clearable disabled
              style="width: 240px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="退料人" prop="backUserName">
            <el-input
              v-model="dialogParams.backUserName"
              placeholder="请输入退料人"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item label="退料人电话" prop="backPhone">
            <el-input
              v-model="dialogParams.backPhone"
              placeholder="请输入退料人电话"
              clearable
              style="width: 240px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="领料单位" prop="leaseUnitId">
            <el-select  v-model="dialogParams.leaseUnitId" filterable clearable @change="GetProData2" style="width: 240px" placeholder="请选择">
              <el-option
                v-for="item in leaseUnitList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="领料工程" prop="leaseProId">
            <el-select  v-model="dialogParams.leaseProId" filterable clearable @change="GetUnitData2" style="width: 240px" placeholder="请选择">
              <el-option
                v-for="item in leaseProjectList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="领料协议编号" prop="leaseAgreementCode">
            <el-input
              v-model="dialogParams.leaseAgreementCode"
              placeholder="请输入领料协议编号"
              clearable disabled
              style="width: 240px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="领料人" prop="leaseUserName">
            <el-input
              v-model="dialogParams.leaseUserName"
              placeholder="请输入领料人"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item label="领料人电话" prop="leasePhone">
            <el-input
              v-model="dialogParams.leasePhone"
              placeholder="请输入领料人电话"
              clearable
              style="width: 240px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="关键字" prop="keyWord">
            <el-input
              v-model="dialogParams.keyWord"
              placeholder="请输入关键字"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetDialogQuery">重置</el-button>
          </el-form-item>
        </el-row>

      </el-form>


      <el-table v-loading="dialogLoading" :data="deviceList" height="350px" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="dictId" />
        <el-table-column label="机具名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
        <el-table-column label="机具规格" align="center" prop="dictName" :show-overflow-tooltip="true" />
        <el-table-column label="编码" align="center" prop="dictName" :show-overflow-tooltip="true" />
        <el-table-column label="当前在用量" align="center" prop="dictName" :show-overflow-tooltip="true" />
        <el-table-column label="直转数量" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.dictName"
              placeholder="请输入直转数量"
              clearable
            />
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
<!--      <div slot="footer" class="dialog-footer" style="text-align: center">-->
<!--        <el-button type="primary" @click="submitForm">通过</el-button>-->
<!--        <el-button type="warning" @click="cancel">不通过</el-button>-->
<!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import { getUnitList, getProjectList, getProData, getUnitData, getAgreementInfoById,} from "@/api/claimAndRefund/receive";
import { getWorkSiteList,getUseringData} from "@/api/claimAndRefund/constructionSite";

export default {
  name: "ConstructionSite",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 单位数据
      unitList: [],
      // 工程数据
       projectList: [],
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        
        status: undefined
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogLoading: false,
      dialogParams:{},
      deviceList: [],//弹窗table数据
      backUnitList: [],//退料单位列表
      backProjectList: [],//退料项目列表
      leaseUnitList: [],//领料单位列表
      leaseProjectList: [],//领料项目列表
      // 表单参数
      form: {},
      // 表单校验
      rules: {//backUnitId backProId leaseUnitId leaseProId
        backUnitId: [
          { required: true, message: "请选择退料单位", trigger: "blur" }
        ],
        backProId: [
          { required: true, message: "请选择退料项目", trigger: "blur" }
        ],
        leaseUnitId: [
          { required: true, message: "请选择领料单位", trigger: "blur" }
        ],
        leaseProId: [
          { required: true, message: "请选择领料项目", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getUnitList();
    this.getProjectList();
    // this.getList();

    this.GetUnitData()
    this.GetProData()
    this.GetUnitData2()
    this.GetProData2()
  },
  methods: {
    //获取单位类型 ,getUnitList, getProjectList
    getUnitList(){
      getUnitList().then(response => {
        this.unitList =  response.data;
        })
    },
    getProjectList(){
      getProjectList().then(response => {
        this.projectList =  response.data;
        })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getWorkSiteList(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // dictId: undefined,
        // dictName: undefined,
        // dictType: undefined,
        // status: "0",
        // remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    // 获取 单位 列表数据
    async GetUnitData(){
      const params = {
        id:this.dialogParams.backProId
      }
      const res = await getUnitData(params)
      this.backUnitList = res.data

      this.GetAgreementInfoById()
    },
    // 获取 工程名称 列表数据
    async GetProData(){
      const params = {
        id:this.dialogParams.backUnitId
      }
      const res = await getProData(params)
      this.backProjectList = res.data

      this.GetAgreementInfoById()
    },
    // 获取 协议id
    async GetAgreementInfoById(){
      if(this.dialogParams.backUnitId && this.dialogParams.backProId){
        const params = {
          unitId: this.dialogParams.backUnitId,
          projectId: this.dialogParams.backProId
        }
        const res = await getAgreementInfoById(params)
        if( !(res.data && res.data.agreementId) ){
          this.$message.error('当前单位和工程未上传');
          this.dialogParams.backUnitId = null
          this.dialogParams.backProId = null
          this.GetUnitData()
          this.GetProData()
        }else{
          this.dialogParams.backAgreementId = res.data.agreementId;
          this.dialogParams.backAgreementCode = res.data.agreementCode;
          this.getDialogList()
        }
      }
    },
    /** 查询列表 */
    getDialogList() {
      this.dialogLoading = true;

      let param = {
        agreementId:this.dialogParams.backAgreementId
      }
      getUseringData(param).then(response => {
          this.deviceList = response.rows;
          this.dialogLoading = false;
        }
      );
    },


    // 获取 单位 列表数据
    async GetUnitData2(){
      const params = {
        id:this.dialogParams.leaseProId
      }
      const res = await getUnitData(params)
      this.leaseUnitList = res.data

      this.GetAgreementInfoById2()
    },
    // 获取 工程名称 列表数据
    async GetProData2(){
      const params = {
        id:this.dialogParams.leaseUnitId
      }
      const res = await getProData(params)
      this.leaseProjectList = res.data

      this.GetAgreementInfoById2()
    },
    // 获取 协议id
    async GetAgreementInfoById2(){
      if(this.dialogParams.leaseUnitId && this.dialogParams.leaseProId){
        const params = {
          unitId: this.dialogParams.leaseUnitId,
          projectId: this.dialogParams.leaseProId
        }
        const res = await getAgreementInfoById(params)
        if( !(res.data && res.data.agreementId) ){
          this.$message.error('当前单位和工程未上传');
          this.dialogParams.leaseUnitId = null
          this.dialogParams.leaseProId = null
          this.GetUnitData2()
          this.GetProData2()
        }else{
          this.dialogParams.leaseAgreementId = res.data.agreementId;
          this.dialogParams.leaseAgreementCode = res.data.agreementCode;
        }
      }
    },
    resetDialogQuery() {
      this.resetForm("dialogForm");
      this.getDialogList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm("dialogForm");
      this.open = true;
      this.title = "工地直转";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

 
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          
            // addType(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // }); 
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const dictIds = row.dictId || this.ids;
      // this.$modal.confirm('是否确认删除该数据项？').then(function() {
      //   return delType(dictIds);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
