<template>
  <!--  1.1.1.1.3.2.	分包合同-->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label="分包合同名称"
        prop="subcontractName"
        label-width="96px"
      >
        <el-input
          clearable
          size="small"
          v-model="queryParams.data.subcontractName"
          placeholder="选择分包合同名称"
          ><el-button
            type="primary"
            slot="append"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
          ></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="分包类型" prop="subcontractingType">
        <el-select
          v-model="queryParams.data.subcontractingType"
          placeholder="请选择分包类型"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['build:subcontracts:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['build:subcontracts:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['build:subcontracts:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="subcontractsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center"
        fixed
      />

      <el-table-column
        label="分包约定"
        align="center"
        prop="subcontractingAgreement"
        min-width="120"
      />
      <el-table-column
        label="分包合同名称"
        align="center"
        prop="subcontractName"
        min-width="120"
      />

      <el-table-column
        label="分包类型"
        align="center"
        prop="subcontractingType"
        min-width="120"
      />
      <el-table-column
        label="分包金额"
        align="center"
        prop="subcontractingAmount"
        min-width="120"
      />
      <el-table-column
        label="承包公司"
        align="center"
        prop="contractingCompany"
        min-width="120"
      />
      <el-table-column
        label="分包时间"
        align="center"
        prop="subcontractingTime"
        min-width="120"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.subcontractingTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="工作内容"
        align="center"
        prop="work"
        min-width="120"
      />
      <el-table-column
        label="工作内容详情"
        align="center"
        prop="jobDetails"
        min-width="120"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['build:subcontracts:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['build:subcontracts:remove']"
            >删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分包约定" prop="subcontractingAgreement">
          <el-input
            v-model="form.subcontractingAgreement"
            placeholder="请输入分包约定"
          />
        </el-form-item>
        <el-form-item label="分包合同名称" prop="subcontractName">
          <el-input
            v-model="form.subcontractName"
            placeholder="请输入分包合同名称"
          />
        </el-form-item>
        <el-form-item label="分包类型" prop="subcontractingType">
          <el-select
            v-model="form.subcontractingType"
            placeholder="请选择分包类型"
            style="width: 100%"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="分包金额" prop="subcontractingAmount">
          <el-input
            v-model="form.subcontractingAmount"
            placeholder="请输入分包金额"
          />
        </el-form-item>
        <el-form-item label="承包公司" prop="contractingCompany">
          <el-input
            v-model="form.contractingCompany"
            placeholder="请输入承包公司"
          />
        </el-form-item>
        <el-form-item label="分包时间" prop="subcontractingTime">
          <!-- <el-input
            v-model="form.subcontractingTime"
            placeholder="请输入分包时间"
          /> -->
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.subcontractingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请输入分包时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作内容" prop="work">
          <el-input v-model="form.work" placeholder="请输入工作内容" />
        </el-form-item>
        <el-form-item label="工作内容详情" prop="jobDetails">
          <el-input
            v-model="form.jobDetails"
            placeholder="请输入工作内容详情"
          />
        </el-form-item>
        <el-form-item label="合同编号" prop="contractNumber">
          <el-input
            v-model="form.contractNumber"
            placeholder="请输入合同编号"
          />
        </el-form-item>
        <el-form-item label="创建人" prop="createUid">
          <el-input v-model="form.createUid" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateUid">
          <el-input v-model="form.updateUid" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="数源部门" prop="owerDept">
          <el-input v-model="form.owerDept" placeholder="请输入数源部门" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSubcontracts,
  getSubcontracts,
  delSubcontracts,
  addSubcontracts,
  updateSubcontracts,
  exportSubcontracts,
} from "@/api/build/subcontracts";

export default {
  name: "Subcontracts",
  props: ["contractNumber"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分包合同表格数据
      subcontractsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: null,
        data: {
          subcontractingAgreement: null,
          subcontractName: null,
          subcontractingType: null,
          subcontractingAmount: null,
          contractingCompany: null,
          subcontractingTime: null,
          work: null,
          jobDetails: null,
          contractNumber: null,
          createUid: null,
          createTime: null,
          updateUid: null,
          updateTime: null,
          owerDept: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sort: "desc",
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    // console.log("contractNumber", this.contractNumber);
  },
  // 父组件  利用axios请求得到的值  传送到子组件，要监听，否则初始渲染时为空
  watch: {
    contractNumber: function (n, o) {
      this.contractNumber = n;
      // console.log("contractNumber", this.contractNumber);
      this.getList();
    },
  },
  methods: {
    /** 查询分包合同列表 */
    getList() {
      if (this.$route.query.baseDataId) {
        this.queryParams.data.contractNumber = this.contractNumber;
      }
      this.loading = true;
      listSubcontracts(this.queryParams).then((response) => {
        this.subcontractsList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        subcontractingAgreement: null,
        subcontractName: null,
        subcontractingType: null,
        subcontractingAmount: null,
        contractingCompany: null,
        subcontractingTime: null,
        work: null,
        jobDetails: null,
        contractNumber: null,
        createUid: null,
        createTime: null,
        updateUid: null,
        updateTime: null,
        owerDept: null,
      };
      this.resetForm("form");
    },
    // 查询表单重置
    resetQueryForm() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        data: {
          id: null,
          subcontractingAgreement: null,
          subcontractName: null,
          subcontractingType: null,
          subcontractingAmount: null,
          contractingCompany: null,
          subcontractingTime: null,
          work: null,
          jobDetails: null,
          contractNumber: null,
          createUid: null,
          createTime: null,
          updateUid: null,
          updateTime: null,
          owerDept: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sort: "desc",
        },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryForm();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分包合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSubcontracts(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分包合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSubcontracts(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            this.form.contractNumber = this.contractNumber;
            addSubcontracts(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (ids) {
        this.$confirm("是否删除选中的数据？", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return delSubcontracts(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      } else {
        this.$message.warning("请选择要删除的数据！！");
      }
    },
  },
};
</script>
