<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="岗位安全责任书" prop="RESPONSIBILITYNAME">
              <el-input v-model="searchForm.RESPONSIBILITYNAME" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="完成状态" prop="COMPLETIONSTATUS">
              <el-select v-model="searchForm.COMPLETIONSTATUS">
                <el-option label="已上传" :value="1" />
                <el-option label="未上传" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="有效状态" prop="VALIDSTATUS">
              <el-select v-model="searchForm.VALIDSTATUS">
                <el-option label="正常" :value="1" />
                <el-option label="未生效" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        row-key="LABORCONTRACT_ID"
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="NEWCONTRACTNAME"
          label="岗位安全责任书"
          width="250"
        />
        <el-table-column prop="COMPLETIONSTATUS" label="完成状态" width="250">
          <template #default="{ row }">
            <el-tag v-if="row.COMPLETIONSTATUS === '1'">已上传</el-tag>
            <el-tag v-else>未上传</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="有效状态" prop="VALIDSTATUS">
          <template #default="{ row }">
            <el-tag v-if="row.VALIDSTATUS === '1'">正常</el-tag>
            <el-tag v-else>未生效</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="CORP_NAME" label="经营企业" width="150" />
        <el-table-column prop="OPERATIONTYPE" label="经营类型" width="150" />
        <el-table-column prop="EXPIRYDATE" label="到期时间" width="150" />
        <el-table-column prop="CREATETIME" label="创建时间" width="150" />
        <el-table-column prop="OPERATTIME" label="修改时间" width="150" />
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/safety_responsibility/details',
                  query: {
                    SAFETYRESPONSIBILITY_ID: row.SAFETYRESPONSIBILITY_ID,
                  },
                })
              "
            >
              查看
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnAddOrEdit(row.SAFETYRESPONSIBILITY_ID, '', 'edit')"
            >
              编辑
            </el-button>
            <el-button
              text
              link
              type="primary"
              @click="fnAddOrEdit('', row.USER_ID, 'add')"
            >
              新增
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import Add from "./components/add.vue";
import { nextTick, reactive, watchEffect } from "vue";
import router from "@/router/index.js";
import { getSafetyResponsibilityList } from "@/request/traffic_safety_responsibility.js";

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getSafetyResponsibilityList);
const data = reactive({
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {},
  },
});

watchEffect(() => {
  if (list.value.length > 0) {
    list.value.forEach((item) => {
      item.NEWCONTRACTNAME = `${item.NAME} 岗位安全责任书`;
    });
  }
});
const fnAddOrEdit = async (
  SAFETYRESPONSIBILITY_ID = "",
  USER_ID = "",
  type = "add"
) => {
  data.addOrEditDialog.visible = true;
  data.addOrEditDialog.type = type;
  data.addOrEditDialog.form = {};
  if (type === "edit" && SAFETYRESPONSIBILITY_ID) {
    data.addOrEditDialog.form.SAFETYRESPONSIBILITY_ID = SAFETYRESPONSIBILITY_ID;
  } else {
    data.addOrEditDialog.form = { USER_ID };
  }

  await nextTick();
};
</script>

<style scoped></style>
